From 5fc0f32257e08d41e058832d95604df2895ea693 Mon Sep 17 00:00:00 2001 From: menshibin Date: Thu, 1 Aug 2024 21:30:34 +0800 Subject: [PATCH 1/4] modify python api --- .../python/connect_websocket_examples.py | 37 ++++++++++++------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/docs/examples/python/connect_websocket_examples.py b/docs/examples/python/connect_websocket_examples.py index 1446bdf120..69c3900187 100644 --- a/docs/examples/python/connect_websocket_examples.py +++ b/docs/examples/python/connect_websocket_examples.py @@ -7,7 +7,7 @@ def create_connection(): conn = taosws.connect( user="root", password="taosdata", - host="localhost", + host="192.168.1.98", port=6041, ) except Exception as err: @@ -21,12 +21,13 @@ def create_db_table(conn): conn.execute("CREATE DATABASE IF NOT EXISTS power") conn.execute("USE power") conn.execute("CREATE STABLE IF NOT EXISTS meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (groupId INT, location BINARY(24))") - conn.execute("CREATE TABLE `d0` USING `meters` TAGS(0, 'Los Angles')") + conn.execute("CREATE TABLE IF NOT EXISTS `d0` USING `meters` (groupId, location) TAGS(0, 'Los Angles')") except Exception as err: print(f'Exception {err}') # ANCHOR_END: create_db -def insert(conn): +def insert(conn): +# ANCHOR: insert sql = """ INSERT INTO power.d1001 USING power.meters TAGS('California.SanFrancisco', 2) @@ -39,18 +40,26 @@ def insert(conn): inserted = conn.execute(sql) assert inserted == 8 except Exception as err: - print(f'Exception {err}') + print(f'Exception111 {err}') +# ANCHOR_END: insert def query(conn): - result = conn.query("select * from stb") - num_of_fields = result.field_count - print(num_of_fields) +# ANCHOR: query + try: + result = conn.query("select * from meters") + num_of_fields = result.field_count + print(num_of_fields) - for row in result: - print(row) + for row in result: + print(row) + except Exception as err: + print(f'Exception {err}') +# ANCHOR_END: query -# output: -# 3 -# ('2023-02-28 15:56:13.329 +08:00', 1, 1) -# ('2023-02-28 15:56:13.333 +08:00', 2, 1) -# ('2023-02-28 15:56:13.337 +08:00', 3, 1) +if __name__ == "__main__": + conn = create_connection() + create_db_table(conn) + insert(conn) + query(conn) + if conn: + conn.close() From d86043fde0a59739a6443a8fb308036c437a6f66 Mon Sep 17 00:00:00 2001 From: menshibin Date: Fri, 2 Aug 2024 15:14:08 +0800 Subject: [PATCH 2/4] nodejs example modify --- .../node/websocketexample/stmt_example.js | 60 +++++++----- .../node/websocketexample/tmq_example.js | 18 ++-- docs/examples/python/connect_example.py | 6 +- .../connect_rest_with_req_id_examples.py | 9 +- .../python/connect_websocket_examples.py | 9 +- docs/examples/python/create_db_native.py | 47 ++++++---- docs/examples/python/create_db_rest.py | 36 +++++--- docs/examples/python/create_db_ws.py | 42 ++++++--- docs/examples/python/insert_native.py | 92 +++++-------------- docs/examples/python/insert_rest.py | 64 +++++-------- docs/examples/python/insert_ws.py | 87 +++++------------- docs/zh/08-develop/01-connect/index.md | 2 +- docs/zh/08-develop/02-sql.md | 57 +++++++++++- docs/zh/08-develop/04-schemaless.md | 5 + docs/zh/08-develop/05-stmt.md | 6 +- docs/zh/08-develop/07-tmq.md | 3 + 16 files changed, 269 insertions(+), 274 deletions(-) diff --git a/docs/examples/node/websocketexample/stmt_example.js b/docs/examples/node/websocketexample/stmt_example.js index 842a8a4c3d..1b78e4bccd 100644 --- a/docs/examples/node/websocketexample/stmt_example.js +++ b/docs/examples/node/websocketexample/stmt_example.js @@ -2,13 +2,13 @@ const taos = require("@tdengine/websocket"); let db = 'power'; let stable = 'meters'; -let tags = ['California.SanFrancisco', 3]; -let values = [ - [1706786044994, 1706786044995, 1706786044996], - [10.2, 10.3, 10.4], - [292, 293, 294], - [0.32, 0.33, 0.34], -]; +let numOfSubTable = 10; +let numOfRow = 10; +function getRandomInt(min, max) { + min = Math.ceil(min); + max = Math.floor(max); + return Math.floor(Math.random() * (max - min + 1)) + min; +} async function prepare() { let dsn = 'ws://localhost:6041' @@ -29,24 +29,36 @@ async function prepare() { connector = await prepare(); stmt = await connector.stmtInit(); await stmt.prepare(`INSERT INTO ? USING ${db}.${stable} (location, groupId) TAGS (?, ?) VALUES (?, ?, ?, ?)`); - await stmt.setTableName('d1001'); - let tagParams = stmt.newStmtParam(); - tagParams.setVarchar([tags[0]]); - tagParams.setInt([tags[1]]); - await stmt.setTags(tagParams); - - let bindParams = stmt.newStmtParam(); - bindParams.setTimestamp(values[0]); - bindParams.setFloat(values[1]); - bindParams.setInt(values[2]); - bindParams.setFloat(values[3]); - await stmt.bind(bindParams); - await stmt.batch(); - await stmt.exec(); - console.log(stmt.getLastAffected()); + for (let i = 0; i < numOfSubTable; i++) { + await stmt.setTableName(`d_bind_${i}`); + let tagParams = stmt.newStmtParam(); + tagParams.setVarchar([`location_${i}`]); + tagParams.setInt([i]); + await stmt.setTags(tagParams); + let timestampParams = []; + let currentParams = []; + let voltageParams = []; + let phaseParams = []; + const currentMillis = new Date().getTime(); + for (let j = 0; j < numOfRow; j++) { + timestampParams.push(currentMillis + j); + currentParams.push(Math.random() * 30); + voltageParams.push(getRandomInt(100, 300)); + phaseParams.push(Math.random()); + } + let bindParams = stmt.newStmtParam(); + bindParams.setTimestamp(timestampParams); + bindParams.setFloat(currentParams); + bindParams.setInt(voltageParams); + bindParams.setFloat(phaseParams); + await stmt.bind(bindParams); + await stmt.batch(); + await stmt.exec(); + console.log(`d_bind_${i} insert ` + stmt.getLastAffected() + " rows."); + } } - catch (err) { - console.error(err.code, err.message); + catch (e) { + console.error(e); } finally { if (stmt) { diff --git a/docs/examples/node/websocketexample/tmq_example.js b/docs/examples/node/websocketexample/tmq_example.js index 9b77abe4d0..b7a8d13809 100644 --- a/docs/examples/node/websocketexample/tmq_example.js +++ b/docs/examples/node/websocketexample/tmq_example.js @@ -7,24 +7,30 @@ const topics = ['power_meters_topic']; // ANCHOR: create_consumer async function createConsumer() { let configMap = new Map([ - [taos.TMQConstants.GROUP_ID, "gId"], + [taos.TMQConstants.GROUP_ID, "group1"], + [taos.TMQConstants.CLIENT_ID, 'client1'], [taos.TMQConstants.CONNECT_USER, "root"], [taos.TMQConstants.CONNECT_PASS, "taosdata"], [taos.TMQConstants.AUTO_OFFSET_RESET, "latest"], - [taos.TMQConstants.CLIENT_ID, 'test_tmq_client'], [taos.TMQConstants.WS_URL, 'ws://localhost:6041'], [taos.TMQConstants.ENABLE_AUTO_COMMIT, 'true'], [taos.TMQConstants.AUTO_COMMIT_INTERVAL_MS, '1000'] ]); - return await taos.tmqConnect(configMap); + try { + return await taos.tmqConnect(configMap); + }catch (err) { + console.log(err); + throw err; + } + } // ANCHOR_END: create_consumer async function prepare() { let conf = new taos.WSConfig('ws://localhost:6041'); - conf.setUser('root') - conf.setPwd('taosdata') - conf.setDb('power') + conf.setUser('root'); + conf.setPwd('taosdata'); + conf.setDb('power'); const createDB = `CREATE DATABASE IF NOT EXISTS POWER ${db} KEEP 3650 DURATION 10 BUFFER 16 WAL_LEVEL 1;`; const createStable = `CREATE STABLE IF NOT EXISTS ${db}.${stable} (ts timestamp, current float, voltage int, phase float) TAGS (location binary(64), groupId int);`; diff --git a/docs/examples/python/connect_example.py b/docs/examples/python/connect_example.py index 493cd512ee..744f2aa49e 100644 --- a/docs/examples/python/connect_example.py +++ b/docs/examples/python/connect_example.py @@ -4,14 +4,14 @@ def create_connection(): # all parameters are optional. conn = None try: - conn = taosws.connect( + conn = taos.connect( user="root", password="taosdata", - host="192.168.1.98", + host="localhost", port=6041, ) except Exception as err: - print(f'Exception {err}') + print(err) finally: if conn: conn.close() diff --git a/docs/examples/python/connect_rest_with_req_id_examples.py b/docs/examples/python/connect_rest_with_req_id_examples.py index 568cbea168..7d90bef158 100644 --- a/docs/examples/python/connect_rest_with_req_id_examples.py +++ b/docs/examples/python/connect_rest_with_req_id_examples.py @@ -1,11 +1,12 @@ # ANCHOR: connect from taosrest import connect, TaosRestConnection, TaosRestCursor -conn = connect(url="http://localhost:6041", - user="root", - password="taosdata", - timeout=30) +conn = connect(url="http://localhost:6041", + user="root", + password="taosdata", + timeout=30) + # ANCHOR_END: connect # ANCHOR: basic # create STable diff --git a/docs/examples/python/connect_websocket_examples.py b/docs/examples/python/connect_websocket_examples.py index 69c3900187..3d93634bcc 100644 --- a/docs/examples/python/connect_websocket_examples.py +++ b/docs/examples/python/connect_websocket_examples.py @@ -1,19 +1,20 @@ +# ANCHOR: connect import taosws def create_connection(): conn = None -# ANCHOR: connect try: conn = taosws.connect( user="root", password="taosdata", - host="192.168.1.98", + host="localhost", port=6041, ) except Exception as err: - print(f'Exception {err}') -# ANCHOR_END: connect + print(err) + return conn + # ANCHOR_END: connect def create_db_table(conn): # ANCHOR: create_db diff --git a/docs/examples/python/create_db_native.py b/docs/examples/python/create_db_native.py index 6eeb94b879..3404363dc3 100644 --- a/docs/examples/python/create_db_native.py +++ b/docs/examples/python/create_db_native.py @@ -1,26 +1,33 @@ import taos -conn = taos.connect( - host="localhost", - user="root", - password="taosdata", - port=6030, -) +conn = None +try: + conn = taos.connect(host="localhost", + user="root", + password="taosdata", + port=6030) -db = "power" + db = "power" + # create database + rowsAffected = conn.execute(f"CREATE DATABASE IF NOT EXISTS {db}") + assert rowsAffected == 0 -conn.execute(f"DROP DATABASE IF EXISTS {db}") -conn.execute(f"CREATE DATABASE {db}") + # change database. same as execute "USE db" + rowsAffected = conn.select_db(db) + assert rowsAffected == 0 + + # create super table + rowsAffected = conn.execute( + "CREATE TABLE IF NOT EXISTS `meters` (`ts` TIMESTAMP, `current` FLOAT, `voltage` INT, `phase` FLOAT) TAGS (`groupid` INT, `location` BINARY(16))" + ) + assert rowsAffected == 0 -# change database. same as execute "USE db" -conn.select_db(db) + # create table + rowsAffected = conn.execute("CREATE TABLE IF NOT EXISTS `d0` USING `meters` (groupid, location) TAGS(0, 'Los Angles')") + assert rowsAffected == 0 -# create super table -conn.execute( - "CREATE TABLE `meters` (`ts` TIMESTAMP, `current` FLOAT, `voltage` INT, `phase` FLOAT) TAGS (`groupid` INT, `location` BINARY(16))" -) - -# create table -conn.execute("CREATE TABLE `d0` USING `meters` TAGS(0, 'Los Angles')") - -conn.close() +except Exception as err: + print(err) +finally: + if conn: + conn.close() diff --git a/docs/examples/python/create_db_rest.py b/docs/examples/python/create_db_rest.py index fc262b30f8..818fa748ad 100644 --- a/docs/examples/python/create_db_rest.py +++ b/docs/examples/python/create_db_rest.py @@ -1,18 +1,28 @@ import taosrest -conn = taosrest.connect(url="http://localhost:6041") +conn = None +try: + conn = taosrest.connect(url="http://localhost:6041", + user="root", + password="taosdata", + timeout=30) -db = "power" + db = "power" + # create database + rowsAffected = conn.execute(f"CREATE DATABASE IF NOT EXISTS {db}") + assert rowsAffected == 0 -conn.execute(f"DROP DATABASE IF EXISTS {db}") -conn.execute(f"CREATE DATABASE {db}") + # create super table + rowsAffected = conn.execute( + f"CREATE TABLE IF NOT EXISTS `{db}`.`meters` (`ts` TIMESTAMP, `current` FLOAT, `voltage` INT, `phase` FLOAT) TAGS (`groupid` INT, `location` BINARY(16))" + ) + assert rowsAffected == 0 + # create table + rowsAffected = conn.execute(f"CREATE TABLE IF NOT EXISTS `{db}`.`d0` USING `{db}`.`meters` (groupid, location) TAGS(0, 'Los Angles')") + assert rowsAffected == 0 -# create super table -conn.execute( - f"CREATE TABLE `{db}`.`meters` (`ts` TIMESTAMP, `current` FLOAT, `voltage` INT, `phase` FLOAT) TAGS (`groupid` INT, `location` BINARY(16))" -) - -# create table -conn.execute(f"CREATE TABLE `{db}`.`d0` USING `{db}`.`meters` TAGS(0, 'Los Angles')") - -conn.close() +except Exception as err: + print(err) +finally: + if conn: + conn.close() diff --git a/docs/examples/python/create_db_ws.py b/docs/examples/python/create_db_ws.py index 844f7a56e3..8c43af8db2 100644 --- a/docs/examples/python/create_db_ws.py +++ b/docs/examples/python/create_db_ws.py @@ -1,22 +1,34 @@ import taosws -dsn = "taosws://root:taosdata@localhost:6041" -conn = taosws.connect(dsn) +conn = None -db = "power" +try: + conn = taosws.connect(user="root", + password="taosdata", + host="localhost", + port=6041) -conn.execute(f"DROP DATABASE IF EXISTS {db}") -conn.execute(f"CREATE DATABASE {db}") + db = "power" + # create database + rowsAffected = conn.execute(f"CREATE DATABASE IF NOT EXISTS {db}") + assert rowsAffected == 0 -# change database. -conn.execute(f"USE {db}") + # change database. + rowsAffected = conn.execute(f"USE {db}") + assert rowsAffected == 0 + + # create super table + rowsAffected = conn.execute( + "CREATE TABLE IF NOT EXISTS `meters` (`ts` TIMESTAMP, `current` FLOAT, `voltage` INT, `phase` FLOAT) TAGS (`groupid` INT, `location` BINARY(16))" + ) + assert rowsAffected == 0 -# create super table -conn.execute( - "CREATE TABLE `meters` (`ts` TIMESTAMP, `current` FLOAT, `voltage` INT, `phase` FLOAT) TAGS (`groupid` INT, `location` BINARY(16))" -) + # create table + rowsAffected = conn.execute("CREATE TABLE IF NOT EXISTS `d0` USING `meters` (groupid, location) TAGS(0, 'Los Angles')") + assert rowsAffected == 0 -# create table -conn.execute("CREATE TABLE `d0` USING `meters` TAGS(0, 'Los Angles')") - -conn.close() +except Exception as err: + print(err) +finally: + if conn: + conn.close() diff --git a/docs/examples/python/insert_native.py b/docs/examples/python/insert_native.py index 37bbb807ec..faf4554437 100644 --- a/docs/examples/python/insert_native.py +++ b/docs/examples/python/insert_native.py @@ -1,76 +1,26 @@ import taos -conn = taos.connect( - host="localhost", - user="root", - password="taosdata", - port=6030, -) +conn = None -db = "power" +try: + conn = taos.connect(user="root", + password="taosdata", + host="localhost", + port=6041) -conn.execute(f"DROP DATABASE IF EXISTS {db}") -conn.execute(f"CREATE DATABASE {db}") + sql = """ + INSERT INTO + power.d1001 USING power.meters (groupid, location) TAGS(2, 'California.SanFrancisco') + VALUES (NOW + 1a, 10.30000, 219, 0.31000) + (NOW + 2a, 12.60000, 218, 0.33000) (NOW + 3a, 12.30000, 221, 0.31000) + power.d1002 USING power.meters (groupid, location) TAGS(3, 'California.SanFrancisco') + VALUES (NOW + 1a, 10.30000, 218, 0.25000) + """ + inserted = conn.execute(sql) + print("inserted into {affectedRows} rows to power.meters successfully.") -# change database. same as execute "USE db" -conn.select_db(db) - -# create super table -conn.execute( - "CREATE STABLE power.meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (location BINARY(64), groupId INT)" -) - -# ANCHOR: insert -# insert data -sql = """ -INSERT INTO -power.d1001 USING power.meters TAGS('California.SanFrancisco', 2) - VALUES ('2018-10-03 14:38:05.000', 10.30000, 219, 0.31000) - ('2018-10-03 14:38:15.000', 12.60000, 218, 0.33000) ('2018-10-03 14:38:16.800', 12.30000, 221, 0.31000) -power.d1002 USING power.meters TAGS('California.SanFrancisco', 3) - VALUES ('2018-10-03 14:38:16.650', 10.30000, 218, 0.25000) -power.d1003 USING power.meters TAGS('California.LosAngeles', 2) - VALUES ('2018-10-03 14:38:05.500', 11.80000, 221, 0.28000) ('2018-10-03 14:38:16.600', 13.40000, 223, 0.29000) -power.d1004 USING power.meters TAGS('California.LosAngeles', 3) - VALUES ('2018-10-03 14:38:05.000', 10.80000, 223, 0.29000) ('2018-10-03 14:38:06.500', 11.50000, 221, 0.35000) -""" - -inserted = conn.execute(sql) -assert inserted == 8 -# ANCHOR_END: insert - -# ANCHOR: query -# Execute a sql and get its result set. It's useful for SELECT statement -result = conn.query("SELECT * from meters") - -# Get fields from result -fields = result.fields -for field in fields: - print(field) - -""" -output: -{name: ts, type: 9, bytes: 8} -{name: current, type: 6, bytes: 4} -{name: voltage, type: 4, bytes: 4} -{name: phase, type: 6, bytes: 4} -{name: location, type: 8, bytes: 64} -{name: groupid, type: 4, bytes: 4} -""" - -# Get data from result as list of tuple -data = result.fetch_all() -for row in data: - print(row) - -""" -output: -(datetime.datetime(2018, 10, 3, 14, 38, 16, 650000), 10.300000190734863, 218, 0.25, 'California.SanFrancisco', 3) -... -""" -# ANCHOR_END: query - -# ANCHOR: req_id -result = conn.query("SELECT * from meters", req_id=1) -# ANCHOR_END: req_id -conn.close() +except Exception as err: + print(err) +finally: + if conn: + conn.close() diff --git a/docs/examples/python/insert_rest.py b/docs/examples/python/insert_rest.py index 16b8fe669c..c43a6a6d17 100644 --- a/docs/examples/python/insert_rest.py +++ b/docs/examples/python/insert_rest.py @@ -1,48 +1,26 @@ import taosrest -conn = taosrest.connect(url="http://localhost:6041") +conn = None -db = "power" +try: + conn = taosrest.connect(url="http://localhost:6041", + user="root", + password="taosdata", + timeout=30) -conn.execute(f"DROP DATABASE IF EXISTS {db}") -conn.execute(f"CREATE DATABASE {db}") + sql = """ + INSERT INTO + power.d1001 USING power.meters (groupid, location) TAGS(2, 'California.SanFrancisco') + VALUES (NOW + 1a, 10.30000, 219, 0.31000) + (NOW + 2a, 12.60000, 218, 0.33000) (NOW + 3a, 12.30000, 221, 0.31000) + power.d1002 USING power.meters (groupid, location) TAGS(3, 'California.SanFrancisco') + VALUES (NOW + 1a, 10.30000, 218, 0.25000) + """ + affectedRows = conn.execute(sql) + print(f"inserted into {affectedRows} rows to power.meters successfully.") -# create super table -conn.execute( - "CREATE STABLE power.meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (location BINARY(64), groupId INT)" -) - -# ANCHOR: insert -# rest insert data -sql = """ -INSERT INTO -power.d1001 USING power.meters TAGS('California.SanFrancisco', 2) - VALUES ('2018-10-03 14:38:05.000', 10.30000, 219, 0.31000) - ('2018-10-03 14:38:15.000', 12.60000, 218, 0.33000) ('2018-10-03 14:38:16.800', 12.30000, 221, 0.31000) -power.d1002 USING power.meters TAGS('California.SanFrancisco', 3) - VALUES ('2018-10-03 14:38:16.650', 10.30000, 218, 0.25000) -power.d1003 USING power.meters TAGS('California.LosAngeles', 2) - VALUES ('2018-10-03 14:38:05.500', 11.80000, 221, 0.28000) ('2018-10-03 14:38:16.600', 13.40000, 223, 0.29000) -power.d1004 USING power.meters TAGS('California.LosAngeles', 3) - VALUES ('2018-10-03 14:38:05.000', 10.80000, 223, 0.29000) ('2018-10-03 14:38:06.500', 11.50000, 221, 0.35000) -""" - -inserted = conn.execute(sql) -assert inserted == 8 -# ANCHOR_END: insert - -# ANCHOR: query -client = taosrest.RestClient("http://localhost:6041") -result = client.sql(f"SELECT * from {db}.meters") -print(result) - -""" -output: -{'code': 0, 'column_meta': [['ts', 'TIMESTAMP', 8], ['current', 'FLOAT', 4], ['voltage', 'INT', 4], ['phase', 'FLOAT', 4], ['location', 'VARCHAR', 64], ['groupid', 'INT', 4]], 'data': [[datetime.datetime(2018, 10, 3, 14, 38, 5), 10.3, 219, 0.31, 'California.SanFrancisco', 2], [datetime.datetime(2018, 10, 3, 14, 38, 15), 12.6, 218, 0.33, 'California.SanFrancisco', 2], [datetime.datetime(2018, 10, 3, 14, 38, 16, 800000), 12.3, 221, 0.31, 'California.SanFrancisco', 2], [datetime.datetime(2018, 10, 3, 14, 38, 16, 650000), 10.3, 218, 0.25, 'California.SanFrancisco', 3], [datetime.datetime(2018, 10, 3, 14, 38, 5, 500000), 11.8, 221, 0.28, 'California.LosAngeles', 2], [datetime.datetime(2018, 10, 3, 14, 38, 16, 600000), 13.4, 223, 0.29, 'California.LosAngeles', 2], [datetime.datetime(2018, 10, 3, 14, 38, 5), 10.8, 223, 0.29, 'California.LosAngeles', 3], [datetime.datetime(2018, 10, 3, 14, 38, 6, 500000), 11.5, 221, 0.35, 'California.LosAngeles', 3]], 'rows': 8} -""" -# ANCHOR_END: query - -# ANCHOR: req_id -result = client.sql(f"SELECT * from {db}.meters", req_id=1) -# ANCHOR_END: req_id -conn.close() +except Exception as err: + print(err) +finally: + if conn: + conn.close() diff --git a/docs/examples/python/insert_ws.py b/docs/examples/python/insert_ws.py index ef3ae3ea99..1d7aa7fc5d 100644 --- a/docs/examples/python/insert_ws.py +++ b/docs/examples/python/insert_ws.py @@ -1,71 +1,26 @@ import taosws -dsn = "taosws://root:taosdata@localhost:6041" -conn = taosws.connect(dsn) +conn = None -db = "power" +try: + conn = taosws.connect(user="root", + password="taosdata", + host="localhost", + port=6041) -conn.execute(f"DROP DATABASE IF EXISTS {db}") -conn.execute(f"CREATE DATABASE {db}") + sql = """ + INSERT INTO + power.d1001 USING power.meters (groupid, location) TAGS(2, 'California.SanFrancisco') + VALUES (NOW + 1a, 10.30000, 219, 0.31000) + (NOW + 2a, 12.60000, 218, 0.33000) (NOW + 3a, 12.30000, 221, 0.31000) + power.d1002 USING power.meters (groupid, location) TAGS(3, 'California.SanFrancisco') + VALUES (NOW + 1a, 10.30000, 218, 0.25000) + """ + inserted = conn.execute(sql) + print("inserted into {affectedRows} rows to power.meters successfully.") -# change database. -conn.execute(f"USE {db}") - -# create super table -conn.execute( - "CREATE STABLE power.meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (location BINARY(64), groupId INT)" -) - -# ANCHOR: insert -# ws insert data -sql = """ -INSERT INTO -power.d1001 USING power.meters TAGS('California.SanFrancisco', 2) - VALUES ('2018-10-03 14:38:05.000', 10.30000, 219, 0.31000) - ('2018-10-03 14:38:15.000', 12.60000, 218, 0.33000) ('2018-10-03 14:38:16.800', 12.30000, 221, 0.31000) -power.d1002 USING power.meters TAGS('California.SanFrancisco', 3) - VALUES ('2018-10-03 14:38:16.650', 10.30000, 218, 0.25000) -power.d1003 USING power.meters TAGS('California.LosAngeles', 2) - VALUES ('2018-10-03 14:38:05.500', 11.80000, 221, 0.28000) ('2018-10-03 14:38:16.600', 13.40000, 223, 0.29000) -power.d1004 USING power.meters TAGS('California.LosAngeles', 3) - VALUES ('2018-10-03 14:38:05.000', 10.80000, 223, 0.29000) ('2018-10-03 14:38:06.500', 11.50000, 221, 0.35000) -""" - -inserted = conn.execute(sql) -assert inserted == 8 -# ANCHOR_END: insert - -# ANCHOR: query -# Execute a sql and get its result set. It's useful for SELECT statement -result = conn.query("SELECT * from meters") - -# Get fields from result -fields = result.fields -for field in fields: - print(field) - -""" -output: -{name: ts, type: TIMESTAMP, bytes: 8} -{name: current, type: FLOAT, bytes: 4} -{name: voltage, type: INT, bytes: 4} -{name: phase, type: FLOAT, bytes: 4} -{name: location, type: BINARY, bytes: 64} -{name: groupid, type: INT, bytes: 4} -""" - -# Get rows from result -for row in result: - print(row) - -""" -output: -('2018-10-03 14:38:05 +08:00', 10.300000190734863, 219, 0.3100000023841858, 'California.SanFrancisco', 2) -... -""" -# ANCHOR_END: query - -# ANCHOR: req_id -result = conn.query_with_req_id("SELECT * from meters", req_id=1) -# ANCHOR_END: req_id -conn.close() +except Exception as err: + print(err) +finally: + if conn: + conn.close() diff --git a/docs/zh/08-develop/01-connect/index.md b/docs/zh/08-develop/01-connect/index.md index ac2787a4cc..43b9e91b15 100644 --- a/docs/zh/08-develop/01-connect/index.md +++ b/docs/zh/08-develop/01-connect/index.md @@ -424,7 +424,7 @@ URL 和 Properties 的详细参数说明和如何使用详见 [API 说明](../.. ```python -{{#include docs/examples/python/connect_rest_examples.py:connect}} +{{#include docs/examples/python/connect_rest_example.py:connect}} ``` diff --git a/docs/zh/08-develop/02-sql.md b/docs/zh/08-develop/02-sql.md index 7b4c6e5d98..fa9285d55c 100644 --- a/docs/zh/08-develop/02-sql.md +++ b/docs/zh/08-develop/02-sql.md @@ -25,13 +25,28 @@ TDengine 对 SQL 语言提供了全面的支持,允许用户以熟悉的 SQL -- Websocket 连接 +```python title="Websocket 连接" +{{#include docs/examples/python/create_db_ws.py}} +``` + +```python title="原生连接" +{{#include docs/examples/python/create_db_native.py}} +``` + +```python title="Rest 连接" +{{#include docs/examples/python/create_db_rest.py}} +``` + +```js +{{#include docs/examples/node/websocketexample/sql_example.js:create_db_and_table}} +``` + @@ -57,11 +72,29 @@ NOW 为系统内部函数,默认为客户端所在计算机当前时间。 NOW + +```python title="Websocket 连接" +{{#include docs/examples/python/insert_ws.py}} +``` + +```python title="原生连接" +{{#include docs/examples/python/insert_native.py}} +``` + +```python title="Rest 连接" +{{#include docs/examples/python/insert_rest.py}} +``` + + +```js +{{#include docs/examples/node/websocketexample/sql_example.js:insertData}} +``` + @@ -86,11 +119,28 @@ NOW 为系统内部函数,默认为客户端所在计算机当前时间。 NOW + +```python title="Websocket 连接" +{{#include docs/examples/python/query_ws.py}} +``` + +```python title="原生连接" +{{#include docs/examples/python/query_native.py}} +``` + +```python title="Rest 连接" +{{#include docs/examples/python/query_rest.py}} +``` + +```js +{{#include docs/examples/node/websocketexample/sql_example.js:queryData}} +``` + @@ -128,6 +178,11 @@ reqId 可用于请求链路追踪,reqId 就像分布式系统中的 traceId + +```js +{{#include docs/examples/node/websocketexample/sql_example.js:sqlWithReqid}} +``` + diff --git a/docs/zh/08-develop/04-schemaless.md b/docs/zh/08-develop/04-schemaless.md index 734e9558a2..7c7cb34187 100644 --- a/docs/zh/08-develop/04-schemaless.md +++ b/docs/zh/08-develop/04-schemaless.md @@ -182,6 +182,11 @@ writer.write(lineDemo, SchemalessProtocolType.LINE, SchemalessTimestampType.NANO + +```js +{{#include docs/examples/node/websocketexample/line_example.js}} +``` + diff --git a/docs/zh/08-develop/05-stmt.md b/docs/zh/08-develop/05-stmt.md index 132a1b8850..6e6ae050b1 100644 --- a/docs/zh/08-develop/05-stmt.md +++ b/docs/zh/08-develop/05-stmt.md @@ -46,9 +46,9 @@ import TabItem from "@theme/TabItem"; - ```js - {{#include docs/examples/node/websocketexample/sql_example.js:createConnect}} - ``` +```js + {{#include docs/examples/node/websocketexample/stmt_example.js:createConnect}} +``` diff --git a/docs/zh/08-develop/07-tmq.md b/docs/zh/08-develop/07-tmq.md index 16a538757f..cb01bc28e0 100644 --- a/docs/zh/08-develop/07-tmq.md +++ b/docs/zh/08-develop/07-tmq.md @@ -106,6 +106,9 @@ Java 连接器创建消费者的参数为 Properties, 可以设置的参数列 +```js + {{#include docs/examples/node/websocketexample/tmq_example.js:create_consumer}} +``` From 870d285f2bb265a1208a3905582b84951a61d201 Mon Sep 17 00:00:00 2001 From: t_max <1172915550@qq.com> Date: Fri, 2 Aug 2024 15:28:32 +0800 Subject: [PATCH 3/4] docs: fix go example anchor --- docs/zh/08-develop/01-connect/index.md | 10 +++++----- docs/zh/08-develop/02-sql.md | 8 ++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/docs/zh/08-develop/01-connect/index.md b/docs/zh/08-develop/01-connect/index.md index 89c3dd7f69..5a0732f92b 100644 --- a/docs/zh/08-develop/01-connect/index.md +++ b/docs/zh/08-develop/01-connect/index.md @@ -540,15 +540,15 @@ DSN 的详细说明和如何使用详见 [连接功能](../../reference/connecto ``` - ```go - {{#include docs/examples/go/connect/restexample/main.go}} - ``` +```go +{{#include docs/examples/go/connect/restexample/main.go}} +``` -不支持 + 不支持 - C# 只支持 WebSocket 连接与原生连接 + 不支持 diff --git a/docs/zh/08-develop/02-sql.md b/docs/zh/08-develop/02-sql.md index 5476154158..2bc428bebf 100644 --- a/docs/zh/08-develop/02-sql.md +++ b/docs/zh/08-develop/02-sql.md @@ -38,7 +38,7 @@ REST API:通过 `curl` 命令进行数据写入和查询操作。 ```go -{{#include docs/examples/go/queryreqid/main.go:query_id}} +{{#include docs/examples/go/sqlquery/main.go:create_db_and_table}} ``` @@ -101,7 +101,7 @@ NOW 为系统内部函数,默认为客户端所在计算机当前时间。 NOW ```go -{{#include docs/examples/go/sqlquery/main.go:create_db_and_table}} +{{#include docs/examples/go/sqlquery/main.go:insert_data}} ``` @@ -157,7 +157,7 @@ curl --location -uroot:taosdata 'http://127.0.0.1:6041/rest/sql' \ ```go -{{#include docs/examples/go/sqlquery/main.go:insert_data}} +{{#include docs/examples/go/sqlquery/main.go:select_data}} ``` @@ -218,7 +218,7 @@ reqId 可用于请求链路追踪,reqId 就像分布式系统中的 traceId ```go -{{#include docs/examples/go/sqlquery/main.go:select_data}} +{{#include docs/examples/go/queryreqid/main.go:query_id}} ``` From 6693b2cc58af66fe67cf2aa9931e98d8b6754a74 Mon Sep 17 00:00:00 2001 From: menshibin Date: Fri, 2 Aug 2024 16:20:50 +0800 Subject: [PATCH 4/4] add node.js example --- .../node/websocketexample/tmq_example.js | 46 ++++---- .../node/websocketexample/tmq_seek_example.js | 104 ++++++++++++++++++ docs/examples/python/connect_rest_example.py | 20 ++++ docs/examples/python/query_native.py | 26 +++++ docs/examples/python/query_rest.py | 15 +++ docs/examples/python/query_ws.py | 22 ++++ docs/zh/08-develop/07-tmq.md | 15 +++ 7 files changed, 222 insertions(+), 26 deletions(-) create mode 100644 docs/examples/node/websocketexample/tmq_seek_example.js create mode 100644 docs/examples/python/connect_rest_example.py create mode 100644 docs/examples/python/query_native.py create mode 100644 docs/examples/python/query_rest.py create mode 100644 docs/examples/python/query_ws.py diff --git a/docs/examples/node/websocketexample/tmq_example.js b/docs/examples/node/websocketexample/tmq_example.js index b7a8d13809..12c303acf4 100644 --- a/docs/examples/node/websocketexample/tmq_example.js +++ b/docs/examples/node/websocketexample/tmq_example.js @@ -38,10 +38,9 @@ async function prepare() { await wsSql.exec(createDB); await wsSql.exec(createStable); -// ANCHOR: create_topic -let createTopic = `CREATE TOPIC IF NOT EXISTS ${topics[0]} AS SELECT * FROM ${db}.${stable}`; -await wsSql.exec(createTopic); -// ANCHOR_END: create_topic + let createTopic = `CREATE TOPIC IF NOT EXISTS ${topics[0]} AS SELECT * FROM ${db}.${stable}`; + await wsSql.exec(createTopic); + for (let i = 0; i < 10; i++) { await wsSql.exec(`INSERT INTO d1001 USING ${stable} (location, groupId) TAGS ("California.SanFrancisco", 3) VALUES (NOW, ${10 + i}, ${200 + i}, ${0.32 + i})`); @@ -49,37 +48,31 @@ await wsSql.exec(createTopic); wsSql.Close(); } -// ANCHOR: subscribe async function subscribe(consumer) { - await consumer.subscribe(topics); - for (let i = 0; i < 5; i++) { - let res = await consumer.poll(500); - for (let [key, value] of res) { - console.log(key, value); - } - if (res.size == 0) { - break; - } - await consumer.commit(); + // ANCHOR: commit + try { + await consumer.subscribe(['topic_meters']); + for (let i = 0; i < 50; i++) { + let res = await consumer.poll(100); + for (let [key, value] of res) { + console.log(key, value); + } + consumer.commit(); + } + } catch (err) { + console.error(err.code, err.message); + throw err; } + // ANCHOR_END: commit } -// ANCHOR_END: subscribe async function test() { + // ANCHOR: unsubscribe let consumer = null; try { await prepare(); let consumer = await createConsumer() - await subscribe(consumer) - // ANCHOR: assignment - let assignment = await consumer.assignment(); - console.log(assignment); - - assignment = await consumer.seekToBeginning(assignment); - for(let i in assignment) { - console.log("seek after:", assignment[i]) - } - // ANCHOR_END: assignment + await subscribe(consumer) await consumer.unsubscribe(); } catch (err) { @@ -91,6 +84,7 @@ async function test() { } taos.destroy(); } + // ANCHOR_END: unsubscribe } test() diff --git a/docs/examples/node/websocketexample/tmq_seek_example.js b/docs/examples/node/websocketexample/tmq_seek_example.js new file mode 100644 index 0000000000..17242dc870 --- /dev/null +++ b/docs/examples/node/websocketexample/tmq_seek_example.js @@ -0,0 +1,104 @@ +const taos = require("@tdengine/websocket"); + +const db = 'power'; +const stable = 'meters'; +const topics = ['power_meters_topic']; + +// ANCHOR: create_consumer +async function createConsumer() { + let configMap = new Map([ + [taos.TMQConstants.GROUP_ID, "group1"], + [taos.TMQConstants.CLIENT_ID, 'client1'], + [taos.TMQConstants.CONNECT_USER, "root"], + [taos.TMQConstants.CONNECT_PASS, "taosdata"], + [taos.TMQConstants.AUTO_OFFSET_RESET, "latest"], + [taos.TMQConstants.WS_URL, 'ws://localhost:6041'], + [taos.TMQConstants.ENABLE_AUTO_COMMIT, 'true'], + [taos.TMQConstants.AUTO_COMMIT_INTERVAL_MS, '1000'] + ]); + try { + return await taos.tmqConnect(configMap); + }catch (err) { + console.log(err); + throw err; + } + +} +// ANCHOR_END: create_consumer + +async function prepare() { + let conf = new taos.WSConfig('ws://localhost:6041'); + conf.setUser('root'); + conf.setPwd('taosdata'); + conf.setDb('power'); + const createDB = `CREATE DATABASE IF NOT EXISTS POWER ${db} KEEP 3650 DURATION 10 BUFFER 16 WAL_LEVEL 1;`; + const createStable = `CREATE STABLE IF NOT EXISTS ${db}.${stable} (ts timestamp, current float, voltage int, phase float) TAGS (location binary(64), groupId int);`; + + let wsSql = await taos.sqlConnect(conf); + await wsSql.exec(createDB); + await wsSql.exec(createStable); + + let createTopic = `CREATE TOPIC IF NOT EXISTS ${topics[0]} AS SELECT * FROM ${db}.${stable}`; + await wsSql.exec(createTopic); + + + for (let i = 0; i < 10; i++) { + await wsSql.exec(`INSERT INTO d1001 USING ${stable} (location, groupId) TAGS ("California.SanFrancisco", 3) VALUES (NOW, ${10 + i}, ${200 + i}, ${0.32 + i})`); + } + wsSql.Close(); +} + +// ANCHOR: subscribe +async function subscribe(consumer) { + try { + await consumer.subscribe(['topic_meters']); + for (let i = 0; i < 50; i++) { + let res = await consumer.poll(100); + for (let [key, value] of res) { + console.log(key, value); + } + } + }catch (err) { + console.error(err.code, err.message); + throw err; + } + +} +// ANCHOR_END: subscribe + +// ANCHOR: offset +async function test() { + let consumer = null; + try { + await prepare(); + let consumer = await createConsumer() + await consumer.subscribe(['topic_meters']); + let res = new Map(); + while (res.size == 0) { + res = await consumer.poll(100); + } + + let assignment = await consumer.assignment(); + for (let i in assignment) { + console.log("seek before:", assignment[i]); + } + + await consumer.seekToBeginning(assignment); + assignment = await consumer.assignment(); + for (let i in assignment) { + console.log("seek after:", assignment[i]); + } + await consumer.unsubscribe(); + } + catch (err) { + console.error(err.code, err.message); + } + finally { + if (consumer) { + await consumer.close(); + } + taos.destroy(); + } +} +// ANCHOR_END: offset +test() diff --git a/docs/examples/python/connect_rest_example.py b/docs/examples/python/connect_rest_example.py new file mode 100644 index 0000000000..c2b8f38431 --- /dev/null +++ b/docs/examples/python/connect_rest_example.py @@ -0,0 +1,20 @@ +# ANCHOR: connect +import taosrest + +def create_connection(): + conn = None + try: + conn = taosrest.connect(url="http://localhost:6041", + user="root", + password="taosdata", + timeout=30) + except Exception as err: + print(err) + finally: + if conn: + conn.close() +# ANCHOR_END: connect + +if __name__ == "__main__": + create_connection() + diff --git a/docs/examples/python/query_native.py b/docs/examples/python/query_native.py new file mode 100644 index 0000000000..6e361405e3 --- /dev/null +++ b/docs/examples/python/query_native.py @@ -0,0 +1,26 @@ +import taos + +conn = None +try: + conn = taos.connect(host="localhost", + port=6030, + user="root", + password="taosdata") + + result = conn.query("SELECT ts, current, location FROM power.meters limit 100") + print(result) + # Get fields from result + fields = result.fields + for field in fields: + print(field) + + # Get data from result as list of tuple + data = result.fetch_all() + for row in data: + print(row) + +except Exception as err: + print(err) +finally: + if conn: + conn.close() \ No newline at end of file diff --git a/docs/examples/python/query_rest.py b/docs/examples/python/query_rest.py new file mode 100644 index 0000000000..fc31e9db33 --- /dev/null +++ b/docs/examples/python/query_rest.py @@ -0,0 +1,15 @@ +import taosrest + +client = None + +try: + client = taosrest.RestClient(url="http://localhost:6041", + user="root", + password="taosdata", + timeout=30) + + result = client.sql(f"SELECT ts, current, location FROM power.meters limit 100") + print(result) + +except Exception as err: + print(err) diff --git a/docs/examples/python/query_ws.py b/docs/examples/python/query_ws.py new file mode 100644 index 0000000000..ee0d40167b --- /dev/null +++ b/docs/examples/python/query_ws.py @@ -0,0 +1,22 @@ +import taosws + +conn = None + +try: + conn = taosws.connect(user="root", + password="taosdata", + host="localhost", + port=6041) + + result = conn.query("SELECT ts, current, location FROM power.meters limit 100") + num_of_fields = result.field_count + print(num_of_fields) + + for row in result: + print(row) + +except Exception as err: + print(err) +finally: + if conn: + conn.close() diff --git a/docs/zh/08-develop/07-tmq.md b/docs/zh/08-develop/07-tmq.md index cf88f5fc2e..3f379b52e6 100644 --- a/docs/zh/08-develop/07-tmq.md +++ b/docs/zh/08-develop/07-tmq.md @@ -249,6 +249,9 @@ Rust 连接器创建消费者的参数为 DSN, 可以设置的参数列表请 +```js + {{#include docs/examples/node/websocketexample/tmq_seek_example.js:subscribe}} +``` @@ -346,6 +349,9 @@ Rust 连接器创建消费者的参数为 DSN, 可以设置的参数列表请 +```js + {{#include docs/examples/node/websocketexample/tmq_seek_example.js:offset}} +``` @@ -445,6 +451,9 @@ Rust 连接器创建消费者的参数为 DSN, 可以设置的参数列表请 +```js + {{#include docs/examples/node/websocketexample/tmq_example.js:commit}} +``` @@ -550,6 +559,9 @@ Rust 连接器创建消费者的参数为 DSN, 可以设置的参数列表请 +```js + {{#include docs/examples/node/websocketexample/tmq_example.js:unsubscribe}} +``` @@ -655,6 +667,9 @@ Rust 连接器创建消费者的参数为 DSN, 可以设置的参数列表请 +```js + {{#include docs/examples/node/websocketexample/tmq_example.js}} +```