From 35cccd2ab9ece6923ce35fe27e8827478e814bb2 Mon Sep 17 00:00:00 2001 From: menshibin Date: Mon, 5 Aug 2024 12:08:17 +0800 Subject: [PATCH] add python example --- docs/examples/python/schemaless_ws.py | 6 +++--- docs/examples/python/stmt_native.py | 5 ++--- docs/examples/python/stmt_ws.py | 7 ++++--- docs/examples/python/tmq_websocket_example.py | 16 +++++++++++++--- 4 files changed, 22 insertions(+), 12 deletions(-) diff --git a/docs/examples/python/schemaless_ws.py b/docs/examples/python/schemaless_ws.py index 46ee303179..c08b9b574d 100644 --- a/docs/examples/python/schemaless_ws.py +++ b/docs/examples/python/schemaless_ws.py @@ -41,9 +41,9 @@ def schemaless_insert(): database=db) conn.schemaless_insert( - lines=lineDemo, - protocol=taosws.PySchemalessProtocol.Line, - precision=taosws.PySchemalessPrecision.Millisecond, + lines = lineDemo, + protocol = taosws.PySchemalessProtocol.Line, + precision = taosws.PySchemalessPrecision.Millisecond, ttl=1, req_id=1, ) diff --git a/docs/examples/python/stmt_native.py b/docs/examples/python/stmt_native.py index 8484b57e7e..5b7bc3f487 100644 --- a/docs/examples/python/stmt_native.py +++ b/docs/examples/python/stmt_native.py @@ -42,7 +42,7 @@ try: for j in range (numOfRow): timestamps.append(current + i) currents.append(random.random() * 30) - voltages.append(random.random(100, 300)) + voltages.append(random.randint(100, 300)) phases.append(random.random()) params = taos.new_bind_params(4) @@ -52,8 +52,7 @@ try: params[3].float(phases) stmt.bind_param_batch(params) stmt.execute() - affected = stmt.affected_rows() - print(f"table {tbname} insert {affected} rows.") + print(f"stmt insert successfully.") except Exception as err: print(f"Failed to insert to table meters using stmt, error: {err}") diff --git a/docs/examples/python/stmt_ws.py b/docs/examples/python/stmt_ws.py index c213be3566..9e5f34fc22 100644 --- a/docs/examples/python/stmt_ws.py +++ b/docs/examples/python/stmt_ws.py @@ -41,7 +41,7 @@ try: for j in range (numOfRow): timestamps.append(current + i) currents.append(random.random() * 30) - voltages.append(random.random(100, 300)) + voltages.append(random.randint(100, 300)) phases.append(random.random()) stmt.bind_param( @@ -54,8 +54,9 @@ try: ) stmt.add_batch() - rows = stmt.execute() - print(f"insert {rows} rows.") + stmt.execute() + + print(f"stmt insert successfully.") except Exception as err: print(f"Failed to insert to table meters using stmt, error: {err}") diff --git a/docs/examples/python/tmq_websocket_example.py b/docs/examples/python/tmq_websocket_example.py index 638a79eced..a0aff17c42 100644 --- a/docs/examples/python/tmq_websocket_example.py +++ b/docs/examples/python/tmq_websocket_example.py @@ -1,5 +1,8 @@ #!/usr/bin/python3 import taosws + +topic = "topic_meters" + def prepareMeta(): conn = None @@ -29,6 +32,13 @@ def prepareMeta(): "CREATE TABLE IF NOT EXISTS `d0` USING `meters` (groupid, location) TAGS(0, 'Los Angles')") assert rowsAffected == 0 + # ANCHOR: create_topic + # create topic + conn.execute( + f"CREATE TOPIC IF NOT EXISTS {topic} AS SELECT ts, current, voltage, phase, groupid, location FROM meters" + ) + # ANCHOR_END: create_topic + sql = """ INSERT INTO power.d1001 USING power.meters (groupid, location) TAGS(2, 'California.SanFrancisco') @@ -91,9 +101,9 @@ def seek_offset(consumer): # ANCHOR: subscribe def subscribe(consumer): try: - consumer.subscribe(["topic_meters"]) + consumer.subscribe([topic]) print("subscribe topics successfully") - for i in range(5): + for i in range(50): records = consumer.poll(timeout=1.0) if records: for block in records: @@ -110,7 +120,7 @@ def subscribe(consumer): # ANCHOR: commit_offset def commit_offset(consumer): try: - for i in range(5): + for i in range(50): records = consumer.poll(timeout=1.0) if records: for block in records: