|
[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 |
|
|