今天和你們分享一個python入庫mongodb的腳本。。。python
涉及到python和mongodb,那麼安裝相應的模塊四必不可少的,最簡單的安裝方法,或者非pip不可了。android
# pip install pymongo==3.0.4
順便也記錄下源碼安裝的方式mongodb
# wget https://pypi.python.org/packages/source/p/pymongo/pymongo-2.8.tar.gz#md5=23100361c9af1904eb2d7722f2658114 --no-check-certificate # tar xf pymongo-2.8.tar.gz # cd pymongo-2.8 # python setup.py install
摘自一則日誌數據庫
35783 s100 android 47 5 192.168.1.100 2015-09-05 08:03:19 strengthenHeroByHeroes {"consume_gold":{"ogold":2893821,"cgold":1700,"gold":2892121,"tag":"strengthenHeroByHeroes"},"taskInfo":[{"id":2310033,"progress":2,"status":0}],"delHeroList":{"id":102014,"id":102014,"id":102014,"id":102010,"id":102010},"id":100026,"olevel":46,"oexp":1700,"cexp":1700,"level":46,"exp":3400} 865982021462182 XiaoMi
入庫mongodb的python腳本json
[root@localhost opt]# cat analytical.py #!/usr/bin/env python #coding:utf8 import os,sys,json from datetime import * from pymongo import MongoClient def ConMongo(host,port,cur_db,username,password): client = MongoClient(host,port) db = client[cur_db] db.authenticate(username,password) table = db.gamelogs return table def parseLog(logfile,table): dic = {} dl = [] with open(file_log) as fd: for line in fd: try: tokens = line.strip().split('\t') uid = tokens[0] server = tokens[1] system = tokens[2] level = int(tokens[3]) vip_level = tokens[4] ip = tokens[5] time = datetime.strptime(tokens[6], "%Y-%m-%d %H:%M:%S") #將時間字符串轉換成時間格式 action = tokens[7] result = json.loads(tokens[8]) #特殊字符串轉換成json格式 uuid = tokens[9] if len(tokens) == 12: channel = tokens[11] else: channel = '' dic = {'uid':uid,'server':server,'system':system,'level':level,'vip_level':vip_level,'ip':ip,'time':time,'action':action,'result':result,'uuid':uuid,'channel':channel} dl.append(dic) if len(dl) == 10000: table.insert_many(dl) dl = [] except Exception,e: print e, line if len(dl) > 0: table.insert_many(dl) if __name__ == '__main__': table = ConMongo('localhost',27017,'talefundb','talefun','123456') try: logfile = sys.argv[1] parseLog(logfile,table) except IndexError,e: print e
注意事項:bash
(1)insert_many參數是mongodb 3.0.4中新加的,容許你將一個大列表直接insert到mongodb數據庫中 (2)腳本中作了限制,若是字典中有2000個值,就向mongodb插入一次數據,這樣在效率上獲得了保證 (3)不建議直接複製腳本測試,不少粘貼出來後,不少製表符等會出現問題。我會吧腳本放在雲盤上你們能夠下載,測試用。
點擊可下載:http://pan.baidu.com/s/1qWtbgjqapp