未验证 提交 e541528a 编写于 作者: X xiaolei li 提交者: GitHub

docs: update node-rest doc add insert sample (#20008)

* docs: update node-rest doc, add insert sample

* remove cn character in examples
上级 db2e05b4
......@@ -32,7 +32,9 @@ Please refer to [version support list](/reference/connector#version-support)
## Supported features
### Native connectors
<Tabs defaultValue="native">
<TabItem value="native" label="Native connector">
1. Connection Management
2. General Query
......@@ -41,12 +43,16 @@ Please refer to [version support list](/reference/connector#version-support)
5. Subscription
6. Schemaless
### REST Connector
</TabItem>
<TabItem value="rest" label="REST connector">
1. Connection Management
2. General Query
3. Continuous Query
</TabItem>
</Tabs>
## Installation Steps
### Pre-installation preparation
......@@ -115,6 +121,9 @@ npm install @tdengine/rest
### Verify
<Tabs defaultValue="native">
<TabItem value="native" label="Native connector">
After installing the TDengine client, use the `nodejsChecker.js` program to verify that the current environment supports Node.js access to TDengine.
Verification in details:
......@@ -131,6 +140,28 @@ node nodejsChecker.js host=localhost
- After executing the above steps, the command-line will output the result of `nodejsChecker.js` connecting to the TDengine instance and performing a simple insert and query.
</TabItem>
<TabItem value="rest" label="REST connector">
After installing the TDengine client, use the `restChecker.js` program to verify that the current environment supports Node.js access to TDengine.
Verification in details:
- Create an installation test folder such as `~/tdengine-test`. Download the [restChecker.js source code](https://github.com/taosdata/TDengine/tree/3.0/docs/examples/node/restexample/restChecker.js) to your local.
- Execute the following command from the command-line.
```bash
npm init -y
npm install @tdengine/rest
node restChecker.js
```
- After executing the above steps, the command-line will output the result of `restChecker.js` connecting to the TDengine instance and performing a simple insert and query.
</TabItem>
</Tabs>
## Establishing a connection
Please choose to use one of the connectors.
......@@ -182,24 +213,61 @@ let cursor = conn.cursor();
#### SQL Write
<Tabs defaultValue="native">
<TabItem value="native" label="native connection">
<NodeInsert />
</TabItem>
<TabItem value="rest" label="REST connection">
```js
{{#include docs/examples/node/restexample/insert_example.js}}
```
</TabItem>
</Tabs>
#### InfluxDB line protocol write
<Tabs defaultValue="native">
<TabItem value="native" label="native connection">
<NodeInfluxLine />
</TabItem>
</Tabs>
#### OpenTSDB Telnet line protocol write
<Tabs defaultValue="native">
<TabItem value="native" label="native connection">
<NodeOpenTSDBTelnet />
</TabItem>
</Tabs>
#### OpenTSDB JSON line protocol write
<Tabs defaultValue="native">
<TabItem value="native" label="native connection">
<NodeOpenTSDBJson />
</TabItem>
</Tabs>
### Querying data
<Tabs defaultValue="native">
<TabItem value="native" label="native connection">
<NodeQuery />
</TabItem>
</Tabs>
## More sample programs
......
const { options, connect } = require("@tdengine/rest");
async function sqlInsert() {
options.path = "/rest/sql";
options.host = "localhost";
options.port = 6041;
let conn = connect(options);
let cursor = conn.cursor();
try {
let res = await cursor.query('CREATE DATABASE power');
res = await cursor.query('CREATE STABLE power.meters (ts timestamp, current float, voltage int, phase float) TAGS (location binary(64), groupId int)');
res = await cursor.query('INSERT INTO power.d1001 USING power.meters TAGS ("California.SanFrancisco", 2) VALUES (NOW, 10.2, 219, 0.32)');
console.log("res.getResult()", res.getResult());
} catch (err) {
console.log(err);
}
}
sqlInsert();
const { options, connect } = require("@tdengine/rest");
options.path = '/rest/sql/'
options.host = 'localhost';
options.port = 6041;
options.user = "root";
options.passwd = "taosdata";
//optional
// options.url = "http://127.0.0.1:6041";
const db = 'rest_ts_db';
const table = 'rest'
const createDB = `create database if not exists ${db} keep 3650`;
const dropDB = `drop database if exists ${db}`;
const createTB = `create table if not exists ${db}.${table}(ts timestamp,i8 tinyint,i16 smallint,i32 int,i64 bigint,bnr binary(40),nchr nchar(40))`;
const addColumn = `alter table ${db}.${table} add column new_column nchar(40) `;
const dropColumn = `alter table ${db}.${table} drop column new_column`;
const insertSql = `insert into ${db}.${table} values('2022-03-30 18:30:51.567',1,2,3,4,'binary1','nchar1')` +
`('2022-03-30 18:30:51.568',5,6,7,8,'binary2','nchar2')` +
`('2022-03-30 18:30:51.569',9,0,1,2,'binary3','nchar3')`;
const querySql = `select * from ${db}.${table}`;
const errorSql = 'show database';
let conn = connect(options);
let cursor = conn.cursor();
async function execute(sql, pure = true) {
let result = await cursor.query(sql, pure);
// print query result as taos shell
// Get Result object, return Result object.
console.log("result.getResult()",result.getResult());
// Get Meta data, return Meta[]|undefined(when execute failed this is undefined).
console.log("result.getMeta()",result.getMeta());
// Get data,return Array<Array<any>>|undefined(when execute failed this is undefined).
console.log("result.getData()",result.getData());
// Get affect rows,return number|undefined(when execute failed this is undefined).
console.log("result.getAffectRows()",result.getAffectRows());
// Get command,return SQL send to server(need to `query(sql,false)`,set 'pure=false',default true).
console.log("result.getCommand()",result.getCommand());
// Get error code ,return number|undefined(when execute failed this is undefined).
console.log("result.getErrCode()",result.getErrCode());
// Get error string,return string|undefined(when execute failed this is undefined).
console.log("result.getErrStr()",result.getErrStr());
}
(async () => {
// start execute time
let start = new Date().getTime();
await execute(createDB);
console.log("-----------------------------------")
await execute(createTB);
console.log("-----------------------------------")
await execute(addColumn);
console.log("----------------------------------")
await execute(dropColumn);
console.log("-----------------------------------")
await execute(insertSql);
console.log("-----------------------------------")
await execute(querySql);
console.log("-----------------------------------")
await execute(errorSql);
console.log("-----------------------------------")
await execute(dropDB);
// finish time
let end = new Date().getTime();
console.log("total spend time:%d ms",end - start);
})()
......@@ -13,6 +13,8 @@ import NodeInfluxLine from "../07-develop/03-insert-data/_js_line.mdx";
import NodeOpenTSDBTelnet from "../07-develop/03-insert-data/_js_opts_telnet.mdx";
import NodeOpenTSDBJson from "../07-develop/03-insert-data/_js_opts_json.mdx";
import NodeQuery from "../07-develop/04-query-data/_js.mdx";
import RESTQuery from "../07-develop/04-query-data/_js.mdx";
import RESTSQLInsert from "../07-develop/03-insert-data/_js_sql.mdx";
`@tdengine/client` 和 `@tdengine/rest` 是 TDengine 的官方 Node.js 语言连接器。 Node.js 开发人员可以通过它开发可以存取 TDengine 集群数据的应用软件。注意:从 TDengine 3.0 开始 Node.js 原生连接器的包名由 `td2.0-connector` 改名为 `@tdengine/client` 而 rest 连接器的包名由 `td2.0-rest-connector` 改为 `@tdengine/rest`。并且不与 TDengine 2.x 兼容。
......@@ -31,7 +33,8 @@ REST 连接器支持所有能运行 Node.js 的平台。
## 支持的功能特性
### 原生连接器
<Tabs defaultValue="native">
<TabItem value="native" label="原生连接器">
1. 连接管理
2. 普通查询
......@@ -40,12 +43,17 @@ REST 连接器支持所有能运行 Node.js 的平台。
5. 订阅功能
6. Schemaless
### REST 连接器
</TabItem>
<TabItem value="rest" label="REST 连接器">
1. 连接管理
2. 普通查询
3. 连续查询
</TabItem>
</Tabs>
## 安装步骤
### 安装前准备
......@@ -114,6 +122,9 @@ npm install @tdengine/rest
### 安装验证
<Tabs defaultValue="native">
<TabItem value="native" label="原生连接器">
在安装好 TDengine 客户端后,使用 nodejsChecker.js 程序能够验证当前环境是否支持 Node.js 方式访问 TDengine。
验证方法:
......@@ -130,11 +141,35 @@ node nodejsChecker.js host=localhost
- 执行以上步骤后,在命令行会输出 nodejsChecker.js 连接 TDengine 实例,并执行简单插入和查询的结果。
</TabItem>
<TabItem value="rest" label="REST 连接器">
在安装好 TDengine 客户端后,使用 nodejsChecker.js 程序能够验证当前环境是否支持 Node.js 方式访问 TDengine。
验证方法:
- 新建安装验证目录,例如:`~/tdengine-test`,下载 GitHub 上 [restChecker.js 源代码](https://github.com/taosdata/TDengine/tree/3.0/docs/examples/node/restexample/restChecker.js)到本地。
- 在命令行中执行以下命令。
```bash
npm init -y
npm install @tdengine/rest
node restChecker.js
```
- 执行以上步骤后,在命令行会输出 restChecker.js 连接 TDengine 实例,并执行简单插入和查询的结果。
</TabItem>
</Tabs>
## 建立连接
请选择使用一种连接器。
<Tabs defaultValue="rest">
<Tabs defaultValue="native">
<TabItem value="native" label="原生连接">
安装并引用 `@tdengine/client` 包。
......@@ -181,24 +216,63 @@ let cursor = conn.cursor();
#### SQL 写入
<Tabs defaultValue="native">
<TabItem value="native" label="原生连接">
<NodeInsert />
</TabItem>
<TabItem value="rest" label="REST 连接">
```js
{{#include docs/examples/node/restexample/insert_example.js}}
```
</TabItem>
</Tabs>
#### InfluxDB 行协议写入
<Tabs defaultValue="native">
<TabItem value="native" label="原生连接">
<NodeInfluxLine />
</TabItem>
</Tabs>
#### OpenTSDB Telnet 行协议写入
<Tabs defaultValue="native">
<TabItem value="native" label="原生连接">
<NodeOpenTSDBTelnet />
</TabItem>
</Tabs>
#### OpenTSDB JSON 行协议写入
<Tabs defaultValue="native">
<TabItem value="native" label="原生连接">
<NodeOpenTSDBJson />
</TabItem>
</Tabs>
### 查询数据
<Tabs defaultValue="native">
<TabItem value="native" label="原生连接">
<NodeQuery />
</TabItem>
</Tabs>
## 更多示例程序
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册