Advertisement
Guest User

Untitled

a guest
Dec 1st, 2016
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. # @Author: xiewenqian <int>
  2. # @Date: 2016-11-28T20:35:09+08:00
  3. # @Email: wixb50@gmail.com
  4. # @Last modified by: int
  5. # @Last modified time: 2016-12-01T19:32:48+08:00
  6.  
  7.  
  8. import pandas as pd
  9. from pymongo import MongoClient
  10.  
  11.  
  12. def _connect_mongo(host, port, username, password, db):
  13. """ A util for making a connection to mongo """
  14.  
  15. if username and password:
  16. mongo_uri = 'mongodb://%s:%s@%s:%s/%s' % (username, password, host, port, db)
  17. conn = MongoClient(mongo_uri)
  18. else:
  19. conn = MongoClient(host, port)
  20. return conn[db]
  21.  
  22.  
  23. def read_mongo(db, collection, query={}, host='localhost', port=27017, username=None, password=None, no_id=True):
  24. """ Read from Mongo and Store into DataFrame """
  25.  
  26. # Connect to MongoDB
  27. db = _connect_mongo(host=host, port=port, username=username, password=password, db=db)
  28.  
  29. # Make a query to the specific DB and Collection
  30. cursor = db[collection].find(query)
  31.  
  32. # Expand the cursor and construct the DataFrame
  33. df = pd.DataFrame(list(cursor))
  34.  
  35. # Delete the _id
  36. if no_id and '_id' in df:
  37. del df['_id']
  38.  
  39. return df
  40.  
  41. if __name__ == '__main__':
  42. df = read_mongo('db_test', 'db_collection', {}, '172.168.203.174', 10800)
  43. df.to_csv('1.csv', index=False)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement