line_protocol_example.py 1.1 KB
Newer Older
D
dingbo 已提交
1 2 3
import taos
from taos import SmlProtocol, SmlPrecision

B
Bo Ding 已提交
4
lines = ["meters,location=Beijing.Haidian,groupid=2 current=11.8,voltage=221,phase=0.28 1648432611249000",
D
dingbo 已提交
5 6 7 8 9 10
         "meters,location=Beijing.Haidian,groupid=2 current=13.4,voltage=223,phase=0.29 1648432611249500",
         "meters,location=Beijing.Haidian,groupid=3 current=10.8,voltage=223,phase=0.29 1648432611249300",
         "meters,location=Beijing.Haidian,groupid=3 current=11.3,voltage=221,phase=0.35 1648432611249800",
         ]


D
dingbo 已提交
11 12 13
def get_connection():
    # create connection use firstEP in taos.cfg.
    return taos.connect()
D
dingbo 已提交
14

D
dingbo 已提交
15 16

def create_database(conn):
D
dingbo 已提交
17
    # the default precision is ms (microsecond), but we use us(microsecond) here.
B
Bo Ding 已提交
18 19
    conn.execute("CREATE DATABASE test precision 'us'")
    conn.execute("USE test")
D
dingbo 已提交
20 21


D
dingbo 已提交
22
def insert_lines(conn):
B
Bo Ding 已提交
23 24
    affected_rows = conn.schemaless_insert(
        lines, SmlProtocol.LINE_PROTOCOL, SmlPrecision.MICRO_SECONDS)
D
dingbo 已提交
25 26 27 28
    print(affected_rows)  # 8


if __name__ == '__main__':
D
dingbo 已提交
29
    connection = get_connection()
30 31 32 33 34
    try:
        create_database(connection)
        insert_lines(connection)
    finally:
        connection.close()