Python Log Inbound MongoDB

Source: Internet
Author: User
Tags uuid python script

Today and everyone to share a python into the library MongoDB script ...


When it comes to Python and MongoDB, it is necessary to install the appropriate module, the simplest installation method, or non-PIP.

# pip Install pymongo==3.0.4

By the way also record the source of the installation method

# 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


Excerpt from a log

35783s100android475192.168.1.1002015-09-05 08:03:19strengthenherobyheroes{"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": "Oexp" : 1700, "Cexp": 1700, "Level": $, "exp": 3400}865982021462182xiaomi


Python Script for inbound MongoDB

#!/usr/bin/env python#coding:utf8import osimport sysimport jsonfrom pymongo  Import mongoclientfrom datetime import datetime, date, timedef conmongo ( Host,port,cur_db,username,password): #链接MongoDB     client = mongoclient (Host,port )     db = client[cur_db]    db.authenticate (username, Password)     table = db.gamelogs    return tabledef  parselog (file_log,connection):    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 uid ==  ' undefined ':     if result["game_user_id"]:uid =  result["game_user_id"]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)  == 20000:    connection.insert_many (DL)     dl =  []    except exception,e:print e, lineif len (DL)  >  0:    connection.insert_many (DL) if __name__ ==  ' __main__ ':     conn = conmongo (' localhost ', 27017, ' talefundb ', ' talefun ', ' 123456 ')      try:        parselog (Sys.argv[1],conn)      except indexerror,e:print  './%s path_logfile '  % os.path.basename (__file__)


This article is from the "Zheng" blog, make sure to keep this source http://467754239.blog.51cto.com/4878013/1692082

Python Log Inbound MongoDB

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.