From 6f828bfe4f4b57ad07a1e8210dbd7b60dba45f08 Mon Sep 17 00:00:00 2001 From: menshibin Date: Mon, 5 Aug 2024 11:26:59 +0800 Subject: [PATCH] add python example --- docs/examples/python/connect_example.py | 2 +- docs/examples/python/create_db_native.py | 3 +- docs/examples/python/schemaless_ws.py | 97 +++++++++++++++--------- 3 files changed, 63 insertions(+), 39 deletions(-) diff --git a/docs/examples/python/connect_example.py b/docs/examples/python/connect_example.py index 72d17020c9..095696f9a5 100644 --- a/docs/examples/python/connect_example.py +++ b/docs/examples/python/connect_example.py @@ -21,4 +21,4 @@ def create_connection(): if __name__ == "__main__": - create_connection() + create_connection() \ No newline at end of file diff --git a/docs/examples/python/create_db_native.py b/docs/examples/python/create_db_native.py index 46f33dbc6e..df29c730b5 100644 --- a/docs/examples/python/create_db_native.py +++ b/docs/examples/python/create_db_native.py @@ -15,8 +15,7 @@ try: assert rowsAffected == 0 # change database. same as execute "USE db" - rowsAffected = conn.select_db(db) - assert rowsAffected == 0 + conn.select_db(db) # create super table rowsAffected = conn.execute( diff --git a/docs/examples/python/schemaless_ws.py b/docs/examples/python/schemaless_ws.py index effa3e851e..46ee303179 100644 --- a/docs/examples/python/schemaless_ws.py +++ b/docs/examples/python/schemaless_ws.py @@ -1,47 +1,72 @@ import taosws -conn = None +db = "power" +def prepare(): + conn = None + try: + conn = taosws.connect(user="root", + password="taosdata", + host="localhost", + port=6041) -lineDemo = [ - "meters,groupid=2,location=California.SanFrancisco current=10.3000002f64,voltage=219i32,phase=0.31f64 1626006833639" -] + # create database + rowsAffected = conn.execute(f"CREATE DATABASE IF NOT EXISTS {db}") + assert rowsAffected == 0 -telnetDemo = ["metric_telnet 1707095283260 4 host=host0 interface=eth0"] + except Exception as err: + print(f"Failed to create db and table, err:{err}") + finally: + if conn: + conn.close() -jsonDemo = [ - '{"metric": "metric_json","timestamp": 1626846400,"value": 10.3, "tags": {"groupid": 2, "location": "California.SanFrancisco", "id": "d1001"}}' -] +def schemaless_insert(): -try: - conn = taosws.connect(user="root", - password="taosdata", - host="localhost", - port=6041) + conn = None - conn.execute("CREATE DATABASE IF NOT EXISTS power") - conn = conn.execute("USE power") + lineDemo = [ + "meters,groupid=2,location=California.SanFrancisco current=10.3000002f64,voltage=219i32,phase=0.31f64 1626006833639" + ] - conn.schemaless_insert( - lines=lineDemo, - protocol=taosws.PySchemalessProtocol.Line, - precision=taosws.PySchemalessPrecision.Millisecond - ) + telnetDemo = ["metric_telnet 1707095283260 4 host=host0 interface=eth0"] - conn.schemaless_insert( - lines=telnetDemo, - protocol=taosws.PySchemalessProtocol.Telnet, - precision=taosws.PySchemalessPrecision.Microsecond - ) + jsonDemo = [ + '{"metric": "metric_json","timestamp": 1626846400,"value": 10.3, "tags": {"groupid": 2, "location": "California.SanFrancisco", "id": "d1001"}}' + ] - conn.schemaless_insert( - lines=jsonDemo, - protocol=taosws.PySchemalessProtocol.Json, - precision=taosws.PySchemalessPrecision.Millisecond - ) -except Exception as err: - print(f"Failed to insert data with schemaless, err:{err}") - -finally: - if conn: - conn.close() + try: + conn = taosws.connect(user="root", + password="taosdata", + host="localhost", + port=6041, + database=db) + conn.schemaless_insert( + lines=lineDemo, + protocol=taosws.PySchemalessProtocol.Line, + precision=taosws.PySchemalessPrecision.Millisecond, + ttl=1, + req_id=1, + ) + + conn.schemaless_insert( + lines=telnetDemo, + protocol=taosws.PySchemalessProtocol.Telnet, + precision=taosws.PySchemalessPrecision.Microsecond, + ttl=1, + req_id=2, + ) + + conn.schemaless_insert( + lines=jsonDemo, + protocol=taosws.PySchemalessProtocol.Json, + precision=taosws.PySchemalessPrecision.Millisecond, + ttl=1, + req_id=3, + ) + + except Exception as err: + print(f"Failed to insert data with schemaless, err:{err}") + + finally: + if conn: + conn.close() \ No newline at end of file