博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python从MongoDB中按天读取数据并格式化日志
阅读量:7009 次
发布时间:2019-06-28

本文共 1805 字,大约阅读时间需要 6 分钟。

#$cat SpeechMongoHandle.py from pymongo import Connectionimport timeimport datetime# CTRL_A='\x01'# CTRL_B='\x02'CTRL_A='--'def getEveryDay(begin_date,end_date):    date_list = []    begin_date = datetime.datetime.strptime(begin_date, "%Y-%m-%d")    end_date = datetime.datetime.strptime(end_date,"%Y-%m-%d")    while begin_date < end_date:        # date_str = begin_date.strftime("%Y-%m-%d")        mid_date = begin_date + datetime.timedelta(days=1)        tup = (str(begin_date),str(mid_date))        date_list.append(tup)        begin_date = mid_date    return date_listdays = getEveryDay('2017-01-01','2017-01-02')# MongoDB Connectclient = Connection('syslog-1',27017)db_name = 'service'db = client[db_name]collection = db.speechfor day in days:    tmpArray0 = time.strptime(str(day[0]), "%Y-%m-%d %H:%M:%S")    tmpArray1 = time.strptime(str(day[1]), "%Y-%m-%d %H:%M:%S")    timestamp0 = int(time.mktime(tmpArray0)*1000)    timestamp1 = int(time.mktime(tmpArray1)*1000)    print day    print (timestamp0,timestamp1)    results = collection.find({
"time":{
'$gte':timestamp0,'$lt':timestamp1}}) for res in results: keys = res.keys() if "time" in keys: unixtimestamp = str(int(res["time"])) timetemp = time.localtime(int(unixtimestamp)/1000) thedate = time.strftime("%Y-%m-%d %H:%M:%S", timetemp) if "id" in keys: sn = res["id"] if "asr" in keys: asr = res["asr"] if "nlp" in keys: nlp = res["nlp"] if "domain" in keys: domain = res["domain"] if "intent" in keys: intent = res["intent"] print thedate + CTRL_A + sn + CTRL_A + asr + CTRL_A + nlp + CTRL_A + domain + CTRL_A + intent

应对场景为:MongoDB中历史数据巨多接近一年多,需要想办法将数据读出并且格式化上传到集群上。就想出了按天读取数据

或许还有最优解。。。

转载地址:http://qhttl.baihongyu.com/

你可能感兴趣的文章
吉林省将建东北林业大数据中心
查看>>
从互联网到物联网:下一个创新风口到来
查看>>
郭台铭:苹果亚马逊已提供资金 协助富士康收购东芝闪存
查看>>
美40家互联网巨头联合致信特朗普:今后你该这么做
查看>>
记一次磁盘性能测试
查看>>
运营商应对VoIP数据业务是突围之路
查看>>
光伏逆变器竞争格局再重塑?又一条“鲶鱼”出现
查看>>
CYQ.Data 轻量数据访问层(六) 构造数据表
查看>>
超融合的未来发展与使用案例
查看>>
这是数据中心最好的时代,也是最坏的时代
查看>>
QTP使用中的陷阱
查看>>
Cirrus Delaware公司数据中心计划因建设电厂再次受阻
查看>>
前Windows事业部总裁写给CEO和管理者:如何做决策?
查看>>
美国国防部最新报告:美军武器系统可能已经被植入后门
查看>>
Google产品管理副总裁:好产品要不断走出舒适区
查看>>
2016年中国大数据应用将发生质变
查看>>
回忆录:30岁那年,你成长了吗?(上篇)
查看>>
IDEA使用笔记
查看>>
大数据带动IDC业务需求 IDC市场将保持高速增长
查看>>
智能家居市场发展困境
查看>>