从mysql数据转移存至influxdb数据库

本文介绍如何使用Python从MySQL数据库迁移数据到InfluxDB,实现每秒约10万条记录的高效写入。代码示例展示了连接配置、数据查询及转换等关键步骤。

从mysql将数据转移存为influxdb数据库,使用python实现,实测写入每秒10W左右...

 

import datetime
import random
from influxdb import InfluxDBClient
import pymysql
import time
#键接influxdb
client = InfluxDBClient('localhost', 8086, 'root', '******', 'mytestdb',timeout=10)

#链接myslq
conn = pymysql.connect(host="localhost", port=3306, user="root",passwd="******",db="demo")
cursor = pymysql.cursors.Cursor(conn)

#查询mysql列表
sql = "select distinct symbol,exchange from dbbardata"
cursor.execute(sql)

#将mysql标的信息写入列表供循环写入influx用
symbol_list= []

#处理mysql获取的信息供influx写入使用.
def to_influx(info):
    info_influx = {"measurement": "bar_data","tags": {"interval": "1m","vt_symbol": info[0]+"."+info[1]},"time": info[-1],"fields": {"open_price": info[2],"high_price": info[3],"low_price": info[4],"close_price": info[5],"open_interest":info[6],"volume": info[7]}}
    return info_influx


#获取所有商品列表
while True:
    row = cursor.fetchone()
    if not row:
        break
    # print(r
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

tmlige

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值