从mysql读取数据写入mongo

# coding:utf-8

# Created by qinlin.liu at 2017/3/14

import pymysql

import datetime

#pymongo说明文档  : http://api.mongodb.com/python/current/api/pymongo/collection.html

#from pymongo import MongoClient

config = {

"mysql_110": {

"host": "172.18.28.110",

"port":3306,

"user": "reader",

"password": "reader",

"db": "pgenius",

"charset":"utf8"

},

"mongodb_nomal":{

"host": "172.18.135.30",

"replicaset": "athena",

}

}

#连接mysql

mysql_conn=pymysql.connect(**config["mysql_110"])

sql="""

SELECT

SEQ

,STOCKCODE

,CTIME

,MTIME

,TRADEDATE

FROM PGENIUS.QW_STK_TRD_STAT

WHERE TRADEDATE=(SELECT MAX(TRADEDATE) FROM PGENIUS.QW_STK_TRD_STAT)

LIMIT 41

"""

#执行sql

cursor=mysql_conn.cursor()

cursor.execute(sql)

#获取所有查询结果

datas=cursor.fetchall()

print(datas)

ds=[]

for i in datas:

#print(list(i))

#print({"SEQ":i[0],"STOCKCODE":i[1],"CTIME":str(i[2]),"MTIME":str(i[3]),"TRADEDATE":i[4]})

ds.append({"SEQ":i[0],"STOCKCODE":i[1],"CTIME":str(i[2]),"MTIME":str(i[3]),"TRADEDATE":i[4]})

#连接mongo

from pymongo import MongoClient

#mongo_conn=MongoClient(host="172.18.3.130",port=27017,tz_aware=False,uri="mongodb://z3dbusadmin:z3dbusadmin@117.121.98.91:27017/z3dbus?authMechanism=SCRAM-SHA-1")

mongo_conn =MongoClient(**config["mongodb_nomal"])

db=mongo_conn["base"]

account=db["TEST_TIME_WL"]

#print(list(coll.find({"FUNDCODE":"510050"})))

#写入数据

for i in datas:

print(list(i))

print({"SEQ":i[0],"STOCKCODE":i[1],"CTIME":str(i[2]),"MTIME":str(i[3]),"TRADEDATE":i[4]})

account.insert({"SEQ":i[0],"STOCKCODE":i[1],"CTIME":i[2],"MTIME":i[3],"TRADEDATE":i[4]})

'''((22430821, 'QWIN11', datetime.datetime(2017, 6, 1, 10, 8, 41), datetime.datetime(2017, 6, 1, 10, 8, 41), 20170601)

, (22434141, 'QWIN11', datetime.datetime(2017, 6, 1, 10, 34, 43), datetime.datetime(2017, 6, 1, 10, 34, 43), 20170601), (22437461, 'QWIN11', datetime.datetime(2017, 6, 1, 11, 5, 31), datetime.datetime(2017, 6, 1, 11, 5, 31), 20170601), (22440781, 'QWIN11', datetime.datetime(2017, 6, 1, 11, 38, 22), datetime.datetime(2017, 6, 1, 11, 38, 22), 20170601), (22427579, '600000', datetime.datetime(2017, 6, 1, 10, 8, 41), datetime.datetime(2017, 6, 1, 10, 8, 41), 20170601), (22430899, '600000', datetime.datetime(2017, 6, 1, 10, 34, 43), datetime.datetime(2017, 6, 1, 10, 34, 43), 20170601), (22434219, '600000', datetime.datetime(2017, 6, 1, 11, 5, 31), datetime.datetime(2017, 6, 1, 11, 5, 31), 20170601), (22437539, '600000', datetime.datetime(2017, 6, 1, 11, 38, 22), datetime.datetime(2017, 6, 1, 11, 38, 22), 20170601), (22427580, '600004',

'''

上一篇:第四篇 - 爬取前程无忧python相关工作


下一篇:html 加上readonly后在各种浏览器的差异。