telnet_line_protocol_example.py 1.3 KB
Newer Older
D
dingbo 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
import taos
from taos import SmlProtocol, SmlPrecision

# format: <metric> <timestamp> <value> <tagk_1>=<tagv_1>[ <tagk_n>=<tagv_n>]
lines = ["meters.current 1648432611249 10.3 location=Beijing.Chaoyang groupid=2",
         "meters.current 1648432611250 12.6 location=Beijing.Chaoyang groupid=2",
         "meters.current 1648432611249 10.8 location=Beijing.Haidian groupid=3",
         "meters.current 1648432611250 11.3 location=Beijing.Haidian groupid=3",
         "meters.voltage 1648432611249 219 location=Beijing.Chaoyang groupid=2",
         "meters.voltage 1648432611250 218 location=Beijing.Chaoyang groupid=2",
         "meters.voltage 1648432611249 221 location=Beijing.Haidian groupid=3",
         "meters.voltage 1648432611250 217 location=Beijing.Haidian groupid=3",
         ]


D
dingbo 已提交
16 17 18
# create connection use firstEp in taos.cfg.
def get_connection():
    return taos.connect()
D
dingbo 已提交
19

D
dingbo 已提交
20 21

def create_database(conn):
D
dingbo 已提交
22 23 24 25
    conn.execute("create database test")
    conn.execute("use test")


D
dingbo 已提交
26
def insert_lines(conn):
D
dingbo 已提交
27 28 29 30 31
    affected_rows = conn.schemaless_insert(lines, SmlProtocol.TELNET_PROTOCOL, SmlPrecision.NOT_CONFIGURED)
    print(affected_rows)  # 8


if __name__ == '__main__':
D
dingbo 已提交
32 33 34 35
    connection = get_connection()
    create_database(connection)
    insert_lines(connection)
    connection.close()