使用python将mongoDB中的数据写入MySQL中

    技术2025-09-17  83

    记录一次尝试: 首先导入模块:

    import pymysql import pymongo from utils import utils from pymongo import MongoClient from web3 import Web3, HTTPProvider from bson import objectid # 用在mongoDB的find_one()方法上,否则会报错

    执行以下代码(部分信息略去):

    class DailyStatisticSum: def __init__(self): self.__get_config() self.start_mongodb() self.start_mysql() self.write_to_mysql() def __get_config(self): """ 读取配置 :return: """ self.config = utils.config_params() def start_mongodb(self): """ 链接mongoDB数据库 :return: """ db_config = self.config['mongodb'] client = MongoClient(db_config['host'], db_config['port'], username=db_config['username'], password=str(db_config['password']), authSource=db_config['authSource']) self.mongodb = client.mod client = pymongo.MongoClient('mongodb://localhost:27017/') db = client['mod'] # 获取名为mod的数据库 return db def start_mysql(self): db = pymysql.connect(host='xxx', user='xxx', password='xxx', port=3306, database='aaa') #xxx处填入本地mysql相应信息,aaa为要写入的mysql数据库的库名 cursor = db.cursor() mysql_list = [db, cursor] return mysql_list def write_to_mysql(self): db = self.start_mongodb() mysqlconn_list = self.start_mysql() conn = mysqlconn_list[0] cursor = mysqlconn_list[1] sql = "INSERT INTO tp_data (" \ "amount,fee,gas_sum)" \ " VALUES (%s,%s,%s);" w3 = Web3(HTTPProvider("https://api.x'x'x.xxx.xxx/xxx", {'auth': ('name', 'password')} )) #处理数据 i = db.order.find_one({"_id": objectid.ObjectId('id对应的数据')}) print(i) amount = i['amount'] fee = i['fee'] toTxid = i['toTxid'] gas_statistic = w3.eth.getTransaction(toTxid) gas = w3.eth.getTransactionReceipt(toTxid) gas_sum1 = gas['gasUsed'] * gas_statistic['gasPrice'] gas_sum = gas_sum1 / 10**18 par = (amount, fee, gas_sum) print(par) cursor.execute(sql, par) conn.commit() if __name__ == '__main__': print("写入尝试") ds = DailyStatisticSum()

    要处理的mongoDB里的数据: 运行代码,写入Mysql成功:

    Processed: 0.009, SQL: 9