diff --git a/docs-cn/04-connect/_connect_node_native.mdx b/docs-cn/04-connect/_connect_node_native.mdx index 4ffeedd8d9fdc600f8f4b5ab1db41b2139a75039..7168d9268a10f23c66dcc7a85cc54b162ff5f80f 100644 --- a/docs-cn/04-connect/_connect_node_native.mdx +++ b/docs-cn/04-connect/_connect_node_native.mdx @@ -4,6 +4,11 @@ npm install td2.0-connector ``` +:::note +推荐使用 `node-v12.8.0` 或 `node-v12.20.0` + +::: + ```js {{#include docs-examples/node/nativeexample/connect.js}} ``` diff --git a/docs-cn/06-insert-data/_js_stmt.mdx b/docs-cn/06-insert-data/_js_stmt.mdx index 6ce9095590b0daad4595566ff3b9ece941ea7ca1..17a6c9785c7dc1e3c3fa6a59982913f1f139f9c2 100644 --- a/docs-cn/06-insert-data/_js_stmt.mdx +++ b/docs-cn/06-insert-data/_js_stmt.mdx @@ -1,3 +1,12 @@ -```js +```js title=一次绑定一行 {{#include docs-examples/node/nativeexample/param_bind_example.js}} ``` + +```js title=一次绑定多行 +{{#include docs-examples/node/nativeexample/multi_bind_example.js:insertData}} +``` + +:::info +一次绑定一行效率不如一次绑定多行,但支持非 INSERT 语句。一次绑定多行效率更高,但仅支持 INSERT 语句。 + +::: diff --git a/docs-cn/06-insert-data/_py_stmt.mdx b/docs-cn/06-insert-data/_py_stmt.mdx index 18880ce481bfeced2b0ec971195cd70015c2b230..8241ea86bc64ac64d842dc0a6cddc0eae0399503 100644 --- a/docs-cn/06-insert-data/_py_stmt.mdx +++ b/docs-cn/06-insert-data/_py_stmt.mdx @@ -5,3 +5,8 @@ ```py title=一次绑定多行 {{#include docs-examples/python/multi_bind_example.py:bind_batch}} ``` + +:::info +一次绑定一行效率不如一次绑定多行,但支持非 INSERT 语句。一次绑定多行效率更高,但仅支持 INSERT 语句。 + +::: \ No newline at end of file diff --git a/docs-examples/node/nativeexample/async_query_example.js b/docs-examples/node/nativeexample/async_query_example.js index a763832fd831c9cedd8b202a3d29cad69be3b8a6..25b78bc48a82454cc0f9db69b77323a00f3852b4 100644 --- a/docs-examples/node/nativeexample/async_query_example.js +++ b/docs-examples/node/nativeexample/async_query_example.js @@ -1,7 +1,21 @@ const taos = require("td2.0-connector"); +const conn = taos.connect({ host: "localhost", database: "power" }); +const cursor = conn.cursor(); -const conn = taos.connect({ - host: "localhost", -}); +function queryExample() { + cursor + .query("SELECT ts, current FROM meters LIMIT 2") + .execute_a() + .then((result) => { + result.pretty(); + }); +} -// 未完成, 等待 TD-14448 解决 +try { + queryExample(); +} finally { + setTimeout(() => { + conn.close(); + }, 2000); +} +// bug here: jira 14506 diff --git a/docs-examples/node/nativeexample/influxdb_line_example.js b/docs-examples/node/nativeexample/influxdb_line_example.js index a763832fd831c9cedd8b202a3d29cad69be3b8a6..858cd945d47a002628ba80c5ef3d329bc1857a88 100644 --- a/docs-examples/node/nativeexample/influxdb_line_example.js +++ b/docs-examples/node/nativeexample/influxdb_line_example.js @@ -4,4 +4,31 @@ const conn = taos.connect({ host: "localhost", }); -// 未完成, 等待 TD-14448 解决 +const cursor = conn.cursor(); + +function createDatabase() { + cursor.execute("CREATE DATABASE test"); + cursor.execute("USE test"); +} + +function insertData() { + const lines = [ + "meters,location=Beijing.Haidian,groupid=2 current=11.8,voltage=221,phase=0.28 1648432611249", + "meters,location=Beijing.Haidian,groupid=2 current=13.4,voltage=223,phase=0.29 1648432611250", + "meters,location=Beijing.Haidian,groupid=3 current=10.8,voltage=223,phase=0.29 1648432611249", + "meters,location=Beijing.Haidian,groupid=3 current=11.3,voltage=221,phase=0.35 1648432611250", + ]; + cursor.schemalessInsert( + lines, + taos.SCHEMALESS_PROTOCOL.TSDB_SML_LINE_PROTOCOL, + taos.SCHEMALESS_PRECISION.TSDB_SML_TIMESTAMP_MILLI_SECONDS + ); +} + +try { + createDatabase(); + insertData(); +} finally { + // cursor.close(); // 此处抛异常 + conn.close(); +} diff --git a/docs-examples/node/nativeexample/insert_example.js b/docs-examples/node/nativeexample/insert_example.js index 32c4c0f8e80180457050112a2ae2dc586cb8ce65..ef78fd41483ce62e946652b005a714cfd3026f00 100644 --- a/docs-examples/node/nativeexample/insert_example.js +++ b/docs-examples/node/nativeexample/insert_example.js @@ -5,17 +5,21 @@ const conn = taos.connect({ }); const cursor = conn.cursor(); -cursor.execute("CREATE DATABASE power"); -cursor.execute("USE power"); -cursor.execute( - "CREATE STABLE meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (location BINARY(64), groupId INT)" -); -var sql = `INSERT INTO power.d1001 USING power.meters TAGS(Beijing.Chaoyang, 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) +try { + cursor.execute("CREATE DATABASE power"); + cursor.execute("USE power"); + cursor.execute( + "CREATE STABLE meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (location BINARY(64), groupId INT)" + ); + var sql = `INSERT INTO power.d1001 USING power.meters TAGS(Beijing.Chaoyang, 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(Beijing.Chaoyang, 3) VALUES ('2018-10-03 14:38:16.650', 10.30000, 218, 0.25000) power.d1003 USING power.meters TAGS(Beijing.Haidian, 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(Beijing.Haidian, 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)`; -cursor.execute(sql); -conn.close(); + cursor.execute(sql); +} finally { + // cursor.close(); + conn.close(); +} // run with: node insert_example.js // stdout: diff --git a/docs-examples/node/nativeexample/multi_bind_example.js b/docs-examples/node/nativeexample/multi_bind_example.js new file mode 100644 index 0000000000000000000000000000000000000000..63d05461bb3af050acff148b2becc845482fbcae --- /dev/null +++ b/docs-examples/node/nativeexample/multi_bind_example.js @@ -0,0 +1,53 @@ +const taos = require("td2.0-connector"); + +const conn = taos.connect({ + host: "localhost", +}); + +const cursor = conn.cursor(); + +function prepareSTable() { + cursor.execute("CREATE DATABASE power"); + cursor.execute("USE power"); + cursor.execute( + "CREATE STABLE meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (location BINARY(64), groupId INT)" + ); +} + +//ANCHOR: insertData +function insertData() { + // init + cursor.stmtInit(); + // prepare + cursor.stmtPrepare( + "INSERT INTO ? USING meters TAGS(?, ?) VALUES(?, ?, ?, ?)" + ); + + // bind table name and tags + let tagBind = new taos.TaosBind(2); + tagBind.bindBinary("Beijing.Chaoyang"); + tagBind.bindInt(2); + cursor.stmtSetTbnameTags("d1001", tagBind.getBind()); + + // bind values + let valueBind = new taos.TaosMultiBindArr(4); + valueBind.multiBindTimestamp([1648432611249, 1648432611749]); + valueBind.multiBindFloat([10.3, 12.6]); + valueBind.multiBindInt([219, 218]); + valueBind.multiBindFloat([0.31, 0.33]); + cursor.stmtBindParamBatch(valueBind.getMultiBindArr()); + cursor.stmtAddBatch(); + + // execute + cursor.stmtExecute(); + cursor.stmtClose(); +} +//ANCHOR_END: insertData + +try { + prepareSTable(); + insertData(); +} finally { + // cursor.close(); // bug here + conn.close(); +} diff --git a/docs-examples/node/nativeexample/opentsdb_json_example.js b/docs-examples/node/nativeexample/opentsdb_json_example.js index a763832fd831c9cedd8b202a3d29cad69be3b8a6..7cafec403ff94bafce0abc850b2164575217865b 100644 --- a/docs-examples/node/nativeexample/opentsdb_json_example.js +++ b/docs-examples/node/nativeexample/opentsdb_json_example.js @@ -4,4 +4,52 @@ const conn = taos.connect({ host: "localhost", }); -// 未完成, 等待 TD-14448 解决 +const cursor = conn.cursor(); + +function createDatabase() { + cursor.execute("CREATE DATABASE test"); + cursor.execute("USE test"); +} + +function insertData() { + const lines = [ + { + metric: "meters.current", + timestamp: 1648432611249, + value: 10.3, + tags: { location: "Beijing.Chaoyang", groupid: 2 }, + }, + { + metric: "meters.voltage", + timestamp: 1648432611249, + value: 219, + tags: { location: "Beijing.Haidian", groupid: 1 }, + }, + { + metric: "meters.current", + timestamp: 1648432611250, + value: 12.6, + tags: { location: "Beijing.Chaoyang", groupid: 2 }, + }, + { + metric: "meters.voltage", + timestamp: 1648432611250, + value: 221, + tags: { location: "Beijing.Haidian", groupid: 1 }, + }, + ]; + + cursor.schemalessInsert( + [JSON.stringify(lines)], + taos.SCHEMALESS_PROTOCOL.TSDB_SML_JSON_PROTOCOL, + taos.SCHEMALESS_PRECISION.TSDB_SML_TIMESTAMP_NOT_CONFIGURED + ); +} + +try { + createDatabase(); + insertData(); +} finally { + // cursor.close(); // 此处抛异常 + conn.close(); +} diff --git a/docs-examples/node/nativeexample/opentsdb_telnet_example.js b/docs-examples/node/nativeexample/opentsdb_telnet_example.js index a763832fd831c9cedd8b202a3d29cad69be3b8a6..670f8f2be7875602c09a8c027ed755c636f66abb 100644 --- a/docs-examples/node/nativeexample/opentsdb_telnet_example.js +++ b/docs-examples/node/nativeexample/opentsdb_telnet_example.js @@ -4,4 +4,35 @@ const conn = taos.connect({ host: "localhost", }); -// 未完成, 等待 TD-14448 解决 +const cursor = conn.cursor(); + +function createDatabase() { + cursor.execute("CREATE DATABASE test"); + cursor.execute("USE test"); +} + +function insertData() { + const lines = [ + "meters.current 1648432611249 10.3 location=Beijing.Chaoyang groupid=2", + "meters.current 1648432611250 12.6 location=Beijing.Chaoyang groupid=2", + "meters.current 1648432611249 10.8 location=Beijing.Haidian groupid=3", + "meters.current 1648432611250 11.3 location=Beijing.Haidian groupid=3", + "meters.voltage 1648432611249 219 location=Beijing.Chaoyang groupid=2", + "meters.voltage 1648432611250 218 location=Beijing.Chaoyang groupid=2", + "meters.voltage 1648432611249 221 location=Beijing.Haidian groupid=3", + "meters.voltage 1648432611250 217 location=Beijing.Haidian groupid=3", + ]; + cursor.schemalessInsert( + lines, + taos.SCHEMALESS_PROTOCOL.TSDB_SML_TELNET_PROTOCOL, + taos.SCHEMALESS_PRECISION.TSDB_SML_TIMESTAMP_NOT_CONFIGURED + ); +} + +try { + createDatabase(); + insertData(); +} finally { + // cursor.close(); // 此处抛异常 + conn.close(); +} diff --git a/docs-examples/node/nativeexample/param_bind_example.js b/docs-examples/node/nativeexample/param_bind_example.js index a763832fd831c9cedd8b202a3d29cad69be3b8a6..939f8fdd052a1368876d16a03d35701033fe3ab5 100644 --- a/docs-examples/node/nativeexample/param_bind_example.js +++ b/docs-examples/node/nativeexample/param_bind_example.js @@ -4,4 +4,54 @@ const conn = taos.connect({ host: "localhost", }); -// 未完成, 等待 TD-14448 解决 +const cursor = conn.cursor(); + +function prepareSTable() { + cursor.execute("CREATE DATABASE power"); + cursor.execute("USE power"); + cursor.execute( + "CREATE STABLE meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (location BINARY(64), groupId INT)" + ); +} + +function insertData() { + // init + cursor.stmtInit(); + // prepare + cursor.stmtPrepare( + "INSERT INTO ? USING meters TAGS(?, ?) VALUES(?, ?, ?, ?)" + ); + + // bind table name and tags + let tagBind = new taos.TaosBind(2); + tagBind.bindBinary("Beijing.Chaoyang"); + tagBind.bindInt(2); + cursor.stmtSetTbnameTags("d1001", tagBind.getBind()); + + // bind values + let rows = [ + [1648432611249, 10.3, 219, 0.31], + [1648432611749, 12.6, 218, 0.33], + ]; + for (let row of rows) { + let valueBind = new taos.TaosBind(4); + valueBind.bindTimestamp(row[0]); + valueBind.bindFloat(row[1]); + valueBind.bindInt(row[2]); + valueBind.bindFloat(row[3]); + cursor.stmtBindParam(valueBind.getBind()); + cursor.stmtAddBatch(); + } + + // execute + cursor.stmtExecute(); + cursor.stmtClose(); +} + +try { + prepareSTable(); + insertData(); +} finally { + // cursor.close(); // bug here, jira: 14505 + conn.close(); +} diff --git a/docs-examples/node/nativeexample/query_example.js b/docs-examples/node/nativeexample/query_example.js index 57e9225074ee387103c01d76324cc52056b46256..34fd05bbb331a7c177b928a2f8b7878ec880ddf8 100644 --- a/docs-examples/node/nativeexample/query_example.js +++ b/docs-examples/node/nativeexample/query_example.js @@ -6,4 +6,12 @@ const query = cursor.query("SELECT ts, current FROM meters LIMIT 2"); query.execute().then(function (result) { result.pretty(); }); -// 目前执行失败,原因不明: TD-14448 + +// stdout: +// Successfully connected to TDengine +// Query OK, 2 row(s) in set (0.00317767s) + +// ts | current | +// ======================================================= +// 2018-10-03 14:38:05.000 | 10.3 | +// 2018-10-03 14:38:15.000 | 12.6 | diff --git a/docs-examples/node/package.json b/docs-examples/node/package.json index 25cb23798d36e35b59db79bc7d122daf137754e4..5f235bbe34b1b068c35ef877e14138a6498a415c 100644 --- a/docs-examples/node/package.json +++ b/docs-examples/node/package.json @@ -4,6 +4,6 @@ "main": "index.js", "license": "MIT", "dependencies": { - "td2.0-connector": "^2.0.10" + "td2.0-connector": "^2.0.11" } } diff --git a/docs-examples/rust/nativeexample/examples/stmt_example.rs b/docs-examples/rust/nativeexample/examples/stmt_example.rs index 3294c0ff4bb4fc629df7703e0308b876b7485521..a791a4135984a33dded145e8175d7ade57de8d77 100644 --- a/docs-examples/rust/nativeexample/examples/stmt_example.rs +++ b/docs-examples/rust/nativeexample/examples/stmt_example.rs @@ -10,7 +10,7 @@ async fn main() -> Result<(), Error> { let mut stmt = taos.stmt("INSERT INTO ? USING meters TAGS(?, ?) VALUES(?, ?, ?, ?)")?; // bind table name and tags stmt.set_tbname_tags( - "tb0", + "d1001", [ Field::Binary(BString::from("Beijing.Chaoyang")), Field::Int(2),