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..12c303acf4 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);`;
@@ -32,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})`);
@@ -43,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) {
@@ -85,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_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_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/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 1446bdf120..3d93634bcc 100644
--- a/docs/examples/python/connect_websocket_examples.py
+++ b/docs/examples/python/connect_websocket_examples.py
@@ -1,8 +1,8 @@
+# ANCHOR: connect
import taosws
def create_connection():
conn = None
-# ANCHOR: connect
try:
conn = taosws.connect(
user="root",
@@ -11,9 +11,10 @@ def create_connection():
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
@@ -21,12 +22,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 +41,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()
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/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/01-connect/index.md b/docs/zh/08-develop/01-connect/index.md
index 89c3dd7f69..21d11df23c 100644
--- a/docs/zh/08-develop/01-connect/index.md
+++ b/docs/zh/08-develop/01-connect/index.md
@@ -536,19 +536,19 @@ DSN 的详细说明和如何使用详见 [连接功能](../../reference/connecto
```python
-{{#include docs/examples/python/connect_rest_examples.py:connect}}
+{{#include docs/examples/python/connect_rest_example.py:connect}}
```
- ```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..8871dee786 100644
--- a/docs/zh/08-develop/02-sql.md
+++ b/docs/zh/08-develop/02-sql.md
@@ -33,12 +33,22 @@ REST API:通过 `curl` 命令进行数据写入和查询操作。
-- 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}}
+```
```go
-{{#include docs/examples/go/queryreqid/main.go:query_id}}
+{{#include docs/examples/go/sqlquery/main.go:create_db_and_table}}
```
@@ -47,6 +57,11 @@ REST API:通过 `curl` 命令进行数据写入和查询操作。
{{#include docs/examples/rust/nativeexample/examples/query.rs:create_db_and_table}}
```
+
+
+```js
+{{#include docs/examples/node/websocketexample/sql_example.js:create_db_and_table}}
+```
```csharp
@@ -98,10 +113,23 @@ 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}}
+```
+
```go
-{{#include docs/examples/go/sqlquery/main.go:create_db_and_table}}
+{{#include docs/examples/go/sqlquery/main.go:insert_data}}
```
@@ -110,6 +138,11 @@ NOW 为系统内部函数,默认为客户端所在计算机当前时间。 NOW
{{#include docs/examples/rust/nativeexample/examples/query.rs:insert_data}}
```
+
+
+```js
+{{#include docs/examples/node/websocketexample/sql_example.js:insertData}}
+```
```csharp
@@ -154,10 +187,22 @@ curl --location -uroot:taosdata 'http://127.0.0.1:6041/rest/sql' \
+
+```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}}
+```
```go
-{{#include docs/examples/go/sqlquery/main.go:insert_data}}
+{{#include docs/examples/go/sqlquery/main.go:select_data}}
```
@@ -166,6 +211,11 @@ curl --location -uroot:taosdata 'http://127.0.0.1:6041/rest/sql' \
{{#include docs/examples/rust/nativeexample/examples/query.rs:query_data}}
```
+
+
+```js
+{{#include docs/examples/node/websocketexample/sql_example.js:queryData}}
+```
```csharp
@@ -218,7 +268,7 @@ reqId 可用于请求链路追踪,reqId 就像分布式系统中的 traceId
```go
-{{#include docs/examples/go/sqlquery/main.go:select_data}}
+{{#include docs/examples/go/queryreqid/main.go:query_id}}
```
@@ -227,6 +277,11 @@ reqId 可用于请求链路追踪,reqId 就像分布式系统中的 traceId
{{#include docs/examples/rust/nativeexample/examples/query.rs:query_with_req_id}}
```
+
+
+```js
+{{#include docs/examples/node/websocketexample/sql_example.js:sqlWithReqid}}
+```
```csharp
diff --git a/docs/zh/08-develop/04-schemaless.md b/docs/zh/08-develop/04-schemaless.md
index 71c9a5e1bd..611878960d 100644
--- a/docs/zh/08-develop/04-schemaless.md
+++ b/docs/zh/08-develop/04-schemaless.md
@@ -189,6 +189,11 @@ writer.write(lineDemo, SchemalessProtocolType.LINE, SchemalessTimestampType.NANO
{{#include docs/examples/rust/restexample/examples/schemaless.rs}}
```
+
+
+```js
+{{#include docs/examples/node/websocketexample/line_example.js}}
+```
```csharp
diff --git a/docs/zh/08-develop/05-stmt.md b/docs/zh/08-develop/05-stmt.md
index b8c3c60d47..d3e71eed63 100644
--- a/docs/zh/08-develop/05-stmt.md
+++ b/docs/zh/08-develop/05-stmt.md
@@ -52,9 +52,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}}
+```
```csharp
diff --git a/docs/zh/08-develop/07-tmq.md b/docs/zh/08-develop/07-tmq.md
index 36ff09d0f4..d2b759bfd7 100644
--- a/docs/zh/08-develop/07-tmq.md
+++ b/docs/zh/08-develop/07-tmq.md
@@ -142,6 +142,9 @@ Rust 连接器创建消费者的参数为 DSN, 可以设置的参数列表请
+```js
+ {{#include docs/examples/node/websocketexample/tmq_example.js:create_consumer}}
+```
@@ -262,6 +265,9 @@ TMQ 消息队列是一个 [futures::Stream](https://docs.rs/futures/latest/futur
+```js
+ {{#include docs/examples/node/websocketexample/tmq_seek_example.js:subscribe}}
+```
@@ -373,6 +379,9 @@ TMQ 消息队列是一个 [futures::Stream](https://docs.rs/futures/latest/futur
+```js
+ {{#include docs/examples/node/websocketexample/tmq_seek_example.js:offset}}
+```
@@ -479,6 +488,9 @@ TMQ 消息队列是一个 [futures::Stream](https://docs.rs/futures/latest/futur
+```js
+ {{#include docs/examples/node/websocketexample/tmq_example.js:commit}}
+```
@@ -588,6 +600,9 @@ TMQ 消息队列是一个 [futures::Stream](https://docs.rs/futures/latest/futur
+```js
+ {{#include docs/examples/node/websocketexample/tmq_example.js:unsubscribe}}
+```
@@ -693,6 +708,9 @@ TMQ 消息队列是一个 [futures::Stream](https://docs.rs/futures/latest/futur
+```js
+ {{#include docs/examples/node/websocketexample/tmq_example.js}}
+```