taostd是TDengine(https://github.com/taosdata/TDengine) 时序数据库的python connector简单封装, 会自动打开和释放数据库连接。本项目主要是希望在使用TDengine过程中,能方便的高效批量插入数据。此外实现了一个简单的连接池来共享连接,避免重复创建、关闭数据库连接。
pip install taostd
from taostd import td
td.init_db(database="test", pool_size=2, tz_offset=8)
td.execute("CREATE STABLE IF NOT EXISTS meters(ts timestamp, current float, voltage float, phase int) TAGS(location nchar(20), groupId tinyint)")
td.insert_one_with_stable(table='meter_01', stable='meters', ts=datetime.now(), current=0.2550, voltage=0.3542, phase=0, location='北京', groupId=0)
td.insert_one(table='meter_01', ts=datetime.now()-timedelta(minutes=10), current=0.3550, voltage=0.5542, phase=1)
meters = [
{"ts": datetime.now() - timedelta(minutes=1), "current": 0.3550, "voltage": 0.5542, "phase": 2},
{"ts": datetime.now() + timedelta(hours=1), "current": 0.3550, "voltage": 0.5542, "phase": 3},
]
td.insert_many(table='meter_01', args=meters)
meters = [
{"ts": "2021-11-19 15:30:44.123445", "current": 0.3550, "voltage": 0.5542, "phase": 0, "location": "上海", "groupId": 1},
{"ts": datetime.now() - timedelta(hours=1), "current": 0.3550, "voltage": 0.5542, "phase": 1, "location": "上海", "groupId": 1},
]
td.insert_many_with_stable(table='meter_02', stable="meters", args=meters)
meters = [
{"table": "meter_01", "ts": "2021-11-19 17:30:43.1234", "current": 0.3550, "voltage": 0.5542, "phase": 4, "location": "北京", "groupId": 0},
{"table": "meter_02", "ts": "2021-11-19 17:30:43.1234", "current": 0.3550, "voltage": 0.5542, "phase": 2, "location": "上海", "groupId": 1},
{"table": "meter_03", "stable": "meters", "ts": datetime.now(), "current": 0.3550, "voltage": 0.5542, "phase": 0, "location": "天津", "groupId": 2},
]
td.insert_many_tables(args=meters)
count = td.get("select count(1) from meters")
last_current = td.get("select last(current) from meters")
last_row = td.select_one("select last_row(*) from meters")
bj_rows = td.select("select * from meters where location = '北京'")