tfse-etl-api-v0.2/DBInitial/DBMigrator.py

54 lines
1.4 KiB
Python

import pymongo
O_HOST = "116.63.130.34"
O_PASS = "sromitdTW569kC#M"
O_USER = "root"
O_PORT = "27018"
T_HOST = "116.63.130.34"
T_PASS = "UTlC9cCoglD1cI1*"
T_USER = "root"
T_PORT = "27021"
origin_client = pymongo.MongoClient('mongodb://{}:{}@{}:{}'.format(O_USER, O_PASS, O_HOST, O_PORT))
target_client = pymongo.MongoClient('mongodb://{}:{}@{}:{}'.format(T_USER, T_PASS, T_HOST, T_PORT))
def data_migrator(origin, target):
def find_all_data():
collection = origin_client[origin['DB']][origin['Collection']]
data = collection.find({}, {'_id': False})
return list(data)
def save_data_to_target(data):
collection = target_client[target['DB']][target['Collection']]
collection.insert_many(data)
return True
def drop_scripts(origin_data):
target_list = []
for data in origin_data:
###
rid = list(origin_client['评价']['评价记录'].find({"企业ID": data['企业ID'], "评价项目": "综合信用评价"}, {'_id': False}))[0]['评价ID']
data['评价ID'] = rid
###
target_list.append(data)
return target_list
def process():
data1 = find_all_data()
data2 = drop_scripts(data1)
save_data_to_target(data2)
process()
if __name__ == '__main__':
o = {"DB": "企业", "Collection": "风险要素分析"}
t = {"DB": "企业数据", "Collection": "经营风险分析"}
data_migrator(o, t)