-
Notifications
You must be signed in to change notification settings - Fork 1
/
MongoManager.py
59 lines (50 loc) · 1.98 KB
/
MongoManager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
'''
Created on Jan 30, 2017
@author: Subhasis
'''
from pymongo.mongo_client import MongoClient
class MongoManager(object):
'''
This class takes care of setting up the connection to Mongo DB and pushing data into database.
'''
def __init__(self, schema, table, batch_size, db_config):
self.username = db_config['username'] # User name to connect to database
self.password = db_config['password'] # password to connect to database
self.schema = schema # database
self.resultTable = table # collection
self.batchSize = batch_size # This is the no of documents to be pushed in one go
self.port = int(db_config['port']) # Port to connect to database
cluster_nodes = []
cluster_nodes.append(db_config['host'])
self.client = MongoClient(cluster_nodes[0], self.port)
self.db = self.client[self.schema]
if len(self.username) > 1 and len(self.password) > 1:
self.db.authenticate(self.username, self.password, mechanism='MONGODB-CR')
self.collection = self.db[self.resultTable]
self.insert_batch = []
self.batch_count = 0
def pushRecords(self, data_list):
return self.batchQuery(data_list)
def batchQuery(self, data_obj):
if self.batch_count < self.batchSize:
data_dict = data_obj
self.insert_batch.append(data_dict)
self.batch_count += 1
else:
data_dict = data_obj
self.insert_batch.append(data_dict)
self.collection.insert_many(self.insert_batch)
self.batch_count = 0
self.insert_batch = []
return True
def flushBatch(self):
print "Flushed"
self.collection.insert_many(self.insert_batch)
self.batch_count = 0
self.insert_batch = []
return True
def drop_collection(self):
self.collection.drop()
print "Clean Up Complete"
def get_collection(self):
return self.collection