TDengine/docs-examples/python/json_protocol_example.py

36 lines
1.2 KiB
Python
Raw Normal View History

2022-03-28 09:09:08 +00:00
import json
import taos
from taos import SmlProtocol, SmlPrecision
lines = [[{"metric": "meters.current", "timestamp": 1648432611249, "value": 10.3, "tags": {"location": "Beijing.Chaoyang", "groupid": 2}},
{"metric": "meters.voltage", "timestamp": 1648432611249, "value": 219, "tags": {"location": "Beijing.Haidian", "groupid": 1}}],
[{"metric": "meters.current", "timestamp": 1648432611250, "value": 12.6, "tags": {"location": "Beijing.Chaoyang", "groupid": 2}},
{"metric": "meters.voltage", "timestamp": 1648432611250, "value": 221, "tags": {"location": "Beijing.Haidian", "groupid": 1}}]
]
2022-03-28 11:00:25 +00:00
def get_connection():
# create connection use firstEp in taos.cfg.
return taos.connect()
2022-03-28 09:09:08 +00:00
2022-03-28 11:00:25 +00:00
def create_database(conn):
2022-03-28 09:09:08 +00:00
conn.execute("create database test")
conn.execute("use test")
2022-03-28 11:00:25 +00:00
def insert_lines(conn):
2022-03-28 09:09:08 +00:00
global lines
lines = [json.dumps(line) for line in lines]
print(lines)
affected_rows = conn.schemaless_insert(lines, SmlProtocol.JSON_PROTOCOL, SmlPrecision.NOT_CONFIGURED)
print(affected_rows) # 这里有 bug, 4 条数据只写入 2 条。
if __name__ == '__main__':
2022-03-28 11:00:25 +00:00
connection = get_connection()
create_database(connection)
insert_lines(connection)
connection.close()