from pymongo import MongoClient
import psycopg2
import datetime
import random
import os
os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'
import sys
from decimal import *
def get_date(days):
return datetime.datetime.now() - datetime.timedelta(days=days)
########################################
def main(arg=sys.argv):# 获取运行时参数
for i in arg:
if 'ID' in i:
ID = i.split("=")[1]
# 连接postgreSql
connPg = psycopg2.connect(database="haiyoung", user="haiyoung", password="xxxxxx", host="127.0.0.1", port="2333")
cur = connPg.cursor()
# 从 table 中取出 id = 008459aea6a04110b3e7b070ca3d0087 的所有值
cur.execute("SELECT * FROM tableName where id = '008459aea6a04110b3e7b070ca3d0087';")
rows = cur.fetchall() # 取出所有满足条件的数据
# 连接Mongodb
connMongo = MongoClient('127.0.0.1', 20000)
db = connMongo.mydb #连接mydb数据库,没有则自动创建
db.authenticate("haiyoung", "xxxxxx") #用户校验
my_set = db.hy_collection #使用 hy_collection 集合,没有则自动创建
#插入数据 Mongo pg
# my_set.insert({"runtime_id":"123","date":date,"value":18})
# my_set.insert({"id":id, "date":rows[i].date, "test_id":rows[i].test_id, "weight":rows[i].weight})
# bson 暂不支持 datetime 以及 decimal 类型自动转换,需另行处理 rows[i][1].strftime('%Y-%m-%d') "weight":float(rows[i][3])
for i in range(len(rows)):
my_set.insert({"id":ID, "date":rows[i][1].strftime('%Y-%m-%d'), "test_id":rows[i][2], "weight":float(rows[i][3])})
cur.execute("INSERT INTO tableName(id,date,test_id,weight)VALUES(%s,%s,%s,%s)",(ID,rows[i][1],rows[i][2],rows[i][3]))
connPg.commit()
cur.close()
connPg.close()
main(sys.argv)
from pymongo import MongoClient
import psycopg2
i