mongo.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. """
  2. Data Transport - 1.0
  3. Steve L. Nyemba, The Phi Technology LLC
  4. This file is a wrapper around mongodb for reading/writing content against a mongodb server and executing views (mapreduce)
  5. """
  6. from pymongo import MongoClient
  7. from bson.objectid import ObjectId
  8. from bson.binary import Binary
  9. # import nujson as json
  10. from datetime import datetime
  11. import pandas as pd
  12. import numpy as np
  13. import gridfs
  14. # from transport import Reader,Writer
  15. import sys
  16. if sys.version_info[0] > 2 :
  17. from transport.common import Reader, Writer
  18. else:
  19. from common import Reader, Writer
  20. import json
  21. import re
  22. from multiprocessing import Lock, RLock
  23. class Mongo :
  24. lock = RLock()
  25. """
  26. Basic mongodb functions are captured here
  27. """
  28. def __init__(self,**args):
  29. """
  30. :dbname database name/identifier
  31. :host host and port of the database by default localhost:27017
  32. :username username for authentication
  33. :password password for current user
  34. """
  35. self.mechanism= 'SCRAM-SHA-256' if 'mechanism' not in args else args['mechanism']
  36. # authSource=(args['authSource'] if 'authSource' in args else self.dbname)
  37. self._lock = False if 'lock' not in args else args['lock']
  38. self.dbname = None
  39. username = password = None
  40. if 'auth_file' in args :
  41. _info = json.loads((open(args['auth_file'])).read())
  42. else:
  43. _info = {}
  44. _args = dict(args,**_info)
  45. _map = {'dbname':'db','database':'db','table':'uid','collection':'uid','col':'uid','doc':'uid'}
  46. for key in _args :
  47. if key in ['username','password'] :
  48. username = _args['username'] if key=='username' else username
  49. password = _args['password'] if key == 'password' else password
  50. continue
  51. value = _args[key]
  52. if key in _map :
  53. key = _map[key]
  54. self.setattr(key,value)
  55. #
  56. # Let us perform aliasing in order to remain backwards compatible
  57. self.dbname = self.db if hasattr(self,'db')else self.dbname
  58. self.uid = _args['table'] if 'table' in _args else (_args['doc'] if 'doc' in _args else (_args['collection'] if 'collection' in _args else None))
  59. if username and password :
  60. self.client = MongoClient(self.host,
  61. username=username,
  62. password=password ,
  63. authSource=self.authSource,
  64. authMechanism=self.mechanism)
  65. else:
  66. self.client = MongoClient(self.host,maxPoolSize=10000)
  67. self.db = self.client[self.dbname]
  68. def isready(self):
  69. p = self.dbname in self.client.list_database_names()
  70. q = self.uid in self.client[self.dbname].list_collection_names()
  71. return p and q
  72. def setattr(self,key,value):
  73. _allowed = ['host','port','db','doc','authSource','mechanism']
  74. if key in _allowed :
  75. setattr(self,key,value)
  76. pass
  77. def close(self):
  78. self.client.close()
  79. def meta(self,**_args):
  80. return []
  81. class MongoReader(Mongo,Reader):
  82. """
  83. This class will read from a mongodb data store and return the content of a document (not a collection)
  84. """
  85. def __init__(self,**args):
  86. Mongo.__init__(self,**args)
  87. def read(self,**args):
  88. if 'mongo' in args or 'cmd' in args:
  89. #
  90. # @TODO:
  91. cmd = args['mongo'] if 'mongo' in args else args['cmd']
  92. if "aggregate" in cmd :
  93. if "allowDiskUse" not in cmd :
  94. cmd["allowDiskUse"] = True
  95. if "cursor" not in cmd :
  96. cmd["cursor"] = {}
  97. r = []
  98. out = self.db.command(cmd)
  99. #@TODO: consider using a yield (generator) works wonders
  100. while True :
  101. if 'values' in out :
  102. r += out['values']
  103. if 'cursor' in out :
  104. key = 'firstBatch' if 'firstBatch' in out['cursor'] else 'nextBatch'
  105. else:
  106. key = 'n'
  107. if 'cursor' in out and out['cursor'][key] :
  108. r += list(out['cursor'][key])
  109. elif key in out and out[key]:
  110. r.append (out[key])
  111. # yield out['cursor'][key]
  112. if key not in ['firstBatch','nextBatch'] or ('cursor' in out and out['cursor']['id'] == 0) :
  113. break
  114. else:
  115. out = self.db.command({"getMore":out['cursor']['id'],"collection":out['cursor']['ns'].split(".")[-1]})
  116. return pd.DataFrame(r)
  117. else:
  118. if 'table' in args or 'collection' in args :
  119. if 'table' in args:
  120. _uid = args['table']
  121. elif 'collection' in args :
  122. _uid = args['collection']
  123. else:
  124. _uid = self.uid
  125. collection = self.db[_uid]
  126. _filter = args['filter'] if 'filter' in args else {}
  127. _df = pd.DataFrame(collection.find(_filter))
  128. columns = _df.columns.tolist()[1:]
  129. return _df[columns]
  130. def view(self,**args):
  131. """
  132. This function is designed to execute a view (map/reduce) operation
  133. """
  134. pass
  135. class MongoWriter(Mongo,Writer):
  136. """
  137. This class is designed to write to a mongodb collection within a database
  138. """
  139. def __init__(self,**args):
  140. Mongo.__init__(self,**args)
  141. def upload(self,**args) :
  142. """
  143. This function will upload a file to the current database (using GridFS)
  144. :param data binary stream/text to be stored
  145. :param filename filename to be used
  146. :param encoding content_encoding (default utf-8)
  147. """
  148. if 'encoding' not in args :
  149. args['encoding'] = 'utf-8'
  150. gfs = GridFS(self.db)
  151. gfs.put(**args)
  152. def archive(self):
  153. """
  154. This function will archive documents to the
  155. """
  156. collection = self.db[self.uid]
  157. rows = list(collection.find())
  158. for row in rows :
  159. if type(row['_id']) == ObjectId :
  160. row['_id'] = str(row['_id'])
  161. stream = Binary(json.dumps(collection).encode())
  162. collection.delete_many({})
  163. now = "-".join([str(datetime.now().year()),str(datetime.now().month), str(datetime.now().day)])
  164. name = ".".join([self.uid,'archive',now])+".json"
  165. description = " ".join([self.uid,'archive',str(len(rows))])
  166. self.upload(filename=name,data=stream,description=description,content_type='application/json')
  167. # gfs = GridFS(self.db)
  168. # gfs.put(filename=name,description=description,data=stream,encoding='utf-8')
  169. # self.write({{"filename":name,"file":stream,"description":descriptions}})
  170. pass
  171. def write(self,info,**_args):
  172. """
  173. This function will write to a given collection i.e add a record to a collection (no updates)
  174. @param info new record in the collection to be added
  175. """
  176. # document = self.db[self.uid].find()
  177. #collection = self.db[self.uid]
  178. # if type(info) == list :
  179. # self.db[self.uid].insert_many(info)
  180. # else:
  181. try:
  182. if 'table' in _args or 'collection' in _args :
  183. _uid = _args['table'] if 'table' in _args else _args['collection']
  184. else:
  185. _uid = self.uid if 'doc' not in _args else _args['doc']
  186. if self._lock :
  187. Mongo.lock.acquire()
  188. if type(info) == list or type(info) == pd.DataFrame :
  189. self.db[_uid].insert_many(info if type(info) == list else info.to_dict(orient='records'))
  190. else:
  191. self.db[_uid].insert_one(info)
  192. finally:
  193. if self._lock :
  194. Mongo.lock.release()
  195. def set(self,document):
  196. """
  197. if no identifier is provided the function will delete the entire collection and set the new document.
  198. Please use this function with great care (archive the content first before using it... for safety)
  199. """
  200. collection = self.db[self.uid]
  201. if collection.count_document() > 0 and '_id' in document:
  202. id = document['_id']
  203. del document['_id']
  204. collection.find_one_and_replace({'_id':id},document)
  205. else:
  206. collection.delete_many({})
  207. self.write(info)
  208. def close(self):
  209. Mongo.close(self)
  210. # collecton.update_one({"_id":self.uid},document,True)