mongodb数据定时增量存mysql

本文介绍了作者在整理代码时发现的MongoDB到MySQL的同步问题,通过Python脚本实现增量数据同步,并设置了每45分钟执行一次的Crontab定时任务。
摘要由CSDN通过智能技术生成

概要

代码统一管理是我加入公司时才逐步规范管理的,在整理是否存在遗漏代码时,检查一个延保api查询服务代码,重点关注ods延保数据表,排除发现kettle同步商城mongodb数据出现问题,特此写了一个同步脚本分享如下

技术细节

简单一点就不写方法了

  • 实现脚本,增量方式t-1,连接方式等信息我就去掉了

from pymongo import MongoClient
import pymysql
import datetime


client = MongoClient(host="172.24.xx.xxx", port=29997)
db = client["orderSyns"]
db.authenticate(name='xxxxl', password='xxxxxx')
collection = db["insuranceOrder"]

now = datetime.datetime.now()
yesterday = now - datetime.timedelta(days=1)
# 查询数据
data = collection.find({"opeTime": {"$gt": yesterday.strftime("%Y-%m-%d %H:%M:%S")}})

# 连接MySQL数据库
mydb = pymysql.connect(
    host="172.19.xx.xx",
    user="xxx",
    password="xxx#",
    database="ods"
)

cursor = mydb.cursor()
# 插入数据
for document in data:
    cursor.execute(" DELETE FROM ods_gd_insurancemsg WHERE orderId=%s", (document["orderId"],))
    sql = ("INSERT INTO ods_gd_insurancemsg (insuranceOrderId, orderId, insuranceType, brandNo, brandName, orderTime, "
           "customerName, mobile, mainStartTime, mainEndTime, notMainStartTime, notMainEndTime, modelNo, buyTime, "
           "customerUuid, storeUuid, state, buyPrice, insurancePrice, identityNum, opeTime, createOpeTime, modelType, "
           "fakeFlag) VALUES (%s, %s, %s, %s,  %s,  %s,  %s,  %s,  %s,  %s,  %s,  %s,  %s,  %s,  %s,  %s,  %s,  %s,  "
           "%s,  %s,  %s,  %s,  %s,  %s)")
    values = (document.get("insuranceOrderId"), document.get("orderId"), document.get("insuranceType"), document.get("brandNo"),
              document.get("brandName"), document.get("orderTime"), document.get("customerName"), document.get("mobile"),
              document.get("mainStartTime"), document.get("mainEndTime"),
              document.get("notMainStartTime"), document.get("notMainEndTime"), document.get("modelNo"), document.get("buyTime"),
              document.get("customerUuid"),
              document.get("storeUuid"), document.get("state"), document.get("buyPrice"), document.get("insurancePrice"),
              document.get("identityNum"), document.get("opeTime"), document.get("createOpeTime"), document.get("modelType"),
              document.get("fakeFlag"))
    cursor.execute(sql, values)

# 提交事务
mydb.commit()

# 打印受影响的行数
print(cursor.rowcount, "records inserted.")

  • 设置crontab加入定时任务,45分钟执行一次
crontab -e
*/45 * * * * python3 /data/mongodb2mysql/mongodb2mysql.py

小结

愿百花生长安时达的沃土上,齐放在我们流程IT部里面

  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值