Optymalizacja: zrzucanie JSON z Streaming API do Mongo

Tło: mampython moduł skonfigurowany do pobierania obiektów JSON z API strumieniowego i zapisywania ich (jednorazowe wstawianie 25) w MongoDB przy użyciu pymongo. Dla porównania mam także polecenie bashcurl z tego samego API strumieniowego ipipe to domongoimport. Oba te podejścia przechowują dane w oddzielnych kolekcjach.

Okresowo monitorujęcount() kolekcji, aby sprawdzić, jak się mają opłaty.

Do tej pory widzępython moduł opóźniający o około 1000 obiektów JSON zacurl | mongoimport podejście.

Problem: Jak mogę zoptymalizować mojepython moduł do synchronizacji zcurl | mongoimport?

Nie mogę użyćtweetstream ponieważ nie korzystam z Twitter API, ale z usługi streamingu trzeciej strony.

Czy ktoś mógłby mi pomóc tutaj?

Python moduł:


class StreamReader:
    def __init__(self):
        try:
            self.buff = ""
            self.tweet = ""
            self.chunk_count = 0
            self.tweet_list = []
            self.string_buffer = cStringIO.StringIO()
            self.mongo = pymongo.Connection(DB_HOST)
            self.db = self.mongo[DB_NAME]
            self.raw_tweets = self.db["raw_tweets_gnip"]
            self.conn = pycurl.Curl()
            self.conn.setopt(pycurl.ENCODING, 'gzip')
            self.conn.setopt(pycurl.URL, STREAM_URL)
            self.conn.setopt(pycurl.USERPWD, AUTH)
            self.conn.setopt(pycurl.WRITEFUNCTION, self.handle_data)
            self.conn.perform()
        except Exception as ex:
            print "error ocurred : %s" % str(ex)

    def handle_data(self, data):
        try:
            self.string_buffer = cStringIO.StringIO(data)
            for line in self.string_buffer:
                try:
                    self.tweet = json.loads(line)
                except Exception as json_ex:
                    print "JSON Exception occurred: %s" % str(json_ex)
                    continue

                if self.tweet:
                    try:
                        self.tweet_list.append(self.tweet)
                        self.chunk_count += 1
                        if self.chunk_count % 1000 == 0
                            self.raw_tweets.insert(self.tweet_list)
                            self.chunk_count = 0
                            self.tweet_list = []

                    except Exception as insert_ex:
                        print "Error inserting tweet: %s" % str(insert_ex)
                        continue
        except Exception as ex:
            print "Exception occurred: %s" % str(ex)
            print repr(self.buff)

    def __del__(self):
        self.string_buffer.close()

Dziękuje za przeczytanie.

questionAnswers(2)

yourAnswerToTheQuestion