归档写过的python进行mongo数据库操作的code
# -*- coding: utf-8 -*- #!/usr/bin/python """ Spyder Editor This is a temporary script file. """ import os import re import sys import json import pymongo import time import multiprocessing #from datetime import * count=0 def getMongoClientInstance(): if 'mongoClientSingletonInstance' not in globals(): globals()['mongoClientSingletonInstance'] = pymongo.MongoClient('114.119.9.27', 27017) return globals()['mongoClientSingletonInstance'] def insert_mongodb(db_name, collection_name, documents): mongo_client = getMongoClientInstance() try: mongo_client[db_name][collection_name].insert_many(documents) except pymongo.errors.AutoReconnect as e: pass def find_files(dir, wildcard, recursion): result = [] exts = wildcard.split(" ") for root, subdirs, files in os.walk(dir): for name in files: for ext in exts: if (name.endswith(ext)): full_name = os.path.join(root, name) result.append(full_name) break if (not recursion): break return result collect_my=[] def Adaptor(): filelist=find_files("/home/toupiao/", 'log', False) for logf in filelist: print logf #fd_r=open(file) fd_r=open(logf,'r') line_list=fd_r.read().splitlines() for line in line_list: pot = line.find('{') dbline=line[pot:] #print dbline global collect_my collect_my.append(dbline) #os.write(fd_w,info) global count count += 1 collect_my=list(json.loads(x) for x in collect_my) write_db("work1") collect_my=[] fd_r.close() def write_db(pid_name): global collect_my #print pid_name,time.time() insert_mongodb('admin', 'deviceinfo', collect_my) #print pid_name,time.time() if __name__ == "__main__": Adaptor() #p1 = multiprocessing.Process(target=write_db,args=('work1',)) #p1.start() print count