35-node.mdx 10.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
---
toc_max_heading_level: 4
sidebar_label: Node.js
title: TDengine Node.js Connector
---

import Tabs from "@theme/Tabs";
import TabItem from "@theme/TabItem";

import Preparition from "./_preparition.mdx";
G
gccgdb1234 已提交
11 12 13 14 15
import NodeInsert from "../07-develop/03-insert-data/_js_sql.mdx";
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";
16

17
`@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 兼容。
18

19
`@tdengine/client` 是**原生连接器**,它通过 TDengine 客户端驱动程序(taosc)连接 TDengine 运行实例,支持数据写入、查询、订阅、schemaless 接口和参数绑定接口等功能。`@tdengine/rest` 是 **REST 连接器**,它通过 taosAdapter 提供的 REST 接口连接 TDengine 的运行实例。REST 连接器可以在任何平台运行,但性能略为下降,接口实现的功能特性集合和原生接口有少量不同。
20

21
Node.js 连接器源码托管在 [GitHub](https://github.com/taosdata/taos-connector-node/tree/3.0)。
22 23 24 25 26 27 28 29

## 支持的平台

原生连接器支持的平台和 TDengine 客户端驱动支持的平台一致。
REST 连接器支持所有能运行 Node.js 的平台。

## 版本支持

G
gccgdb1234 已提交
30
请参考[版本支持列表](../#版本支持)
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

## 支持的功能特性

### 原生连接器

1. 连接管理
2. 普通查询
3. 连续查询
4. 参数绑定
5. 订阅功能
6. Schemaless

### REST 连接器

1. 连接管理
2. 普通查询
3. 连续查询

## 安装步骤

### 安装前准备

- 安装 Node.js 开发环境
G
gccgdb1234 已提交
54
- 如果使用 REST 连接器,跳过此步。但如果使用原生连接器,请安装 TDengine 客户端驱动,具体步骤请参考[安装客户端驱动](../#安装客户端驱动)。我们使用 [node-gyp](https://github.com/nodejs/node-gyp) 和 TDengine 实例进行交互,还需要根据具体操作系统来安装下文提到的一些依赖工具。
55 56 57 58 59

<Tabs defaultValue="Linux">
<TabItem value="Linux" label="Linux 系统安装依赖工具">

- `python` (建议`v2.7` , `v3.x.x` 目前还不支持)
60
- `@tdengine/client` 3.0.0 支持 Node.js LTS v10.9.0 或更高版本, Node.js LTS v12.8.0 或更高版本;其他版本可能存在包兼容性的问题
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
- `make`
- C 语言编译器,[GCC](https://gcc.gnu.org) v4.8.5 或更高版本

</TabItem>
<TabItem value="Windows" label="Windows 系统安装依赖工具">

- 安装方法 1

使用微软的[ windows-build-tools ](https://github.com/felixrieseberg/windows-build-tools)在`cmd` 命令行界面执行`npm install --global --production windows-build-tools` 即可安装所有的必备工具。

- 安装方法 2

手动安装以下工具:

- 安装 Visual Studio 相关:[Visual Studio Build 工具](https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=BuildTools) 或者 [Visual Studio 2017 Community](https://visualstudio.microsoft.com/pl/thank-you-downloading-visual-studio/?sku=Community)
- 安装 [Python](https://www.python.org/downloads/) 2.7(`v3.x.x` 暂不支持) 并执行 `npm config set python python2.7`
- 进入`cmd`命令行界面,`npm config set msvs_version 2017`

参考微软的 Node.js 用户手册[ Microsoft's Node.js Guidelines for Windows](https://github.com/Microsoft/nodejs-guidelines/blob/master/windows-environment.md#compiling-native-addon-modules)。

如果在 Windows 10 ARM 上使用 ARM64 Node.js,还需添加 "Visual C++ compilers and libraries for ARM64" 和 "Visual C++ ATL for ARM64"。

</TabItem>
</Tabs>

### 使用 npm 安装

<Tabs defaultValue="install_native">
<TabItem value="install_native" label="安装原生连接器">

```bash
92
npm install @tdengine/client
93 94 95 96 97 98
```

</TabItem>
<TabItem value="install_rest" label="安装 REST 连接器">

```bash
99
npm install @tdengine/rest
100 101 102 103 104 105 106 107 108 109 110
```

</TabItem>
</Tabs>

### 安装验证

在安装好 TDengine 客户端后,使用 nodejsChecker.js 程序能够验证当前环境是否支持 Node.js 方式访问 TDengine。

验证方法:

111
- 新建安装验证目录,例如:`~/tdengine-test`,下载 GitHub 上 [nodejsChecker.js 源代码](https://github.com/taosdata/taos-connector-node/blob/3.0/nodejs/examples/nodejsChecker.js)到本地。
112 113 114 115 116

- 在命令行中执行以下命令。

```bash
npm init -y
117
npm install @tdengine/client
118 119 120 121 122 123 124 125 126 127 128 129
node nodejsChecker.js host=localhost
```

- 执行以上步骤后,在命令行会输出 nodejsChecker.js 连接 TDengine 实例,并执行简单插入和查询的结果。

## 建立连接

请选择使用一种连接器。

<Tabs defaultValue="native">
<TabItem value="native" label="原生连接">

130
安装并引用 `@tdengine/client` 包。
131 132 133

```javascript
//A cursor also needs to be initialized in order to interact with TDengine from Node.js.
134
const taos = require("@tdengine/client");
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
var conn = taos.connect({
  host: "127.0.0.1",
  user: "root",
  password: "taosdata",
  config: "/etc/taos",
  port: 0,
});
var cursor = conn.cursor(); // Initializing a new cursor

//Close a connection
conn.close();
```

</TabItem>
<TabItem value="rest" label="REST 连接">

151
安装并引用 `@tdengine/rest` 包。
152 153 154

```javascript
//A cursor also needs to be initialized in order to interact with TDengine from Node.js.
155 156
import { options, connect } from "@tdengine/rest";
options.path = "/rest/sql";
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
// set host
options.host = "localhost";
// set other options like user/passwd

let conn = connect(options);
let cursor = conn.cursor();
```

</TabItem>
</Tabs>

## 使用示例

### 写入数据

#### SQL 写入

<NodeInsert />

#### InfluxDB 行协议写入

<NodeInfluxLine />

#### OpenTSDB Telnet 行协议写入

<NodeOpenTSDBTelnet />

#### OpenTSDB JSON 行协议写入

<NodeOpenTSDBJson />

### 查询数据

<NodeQuery />

192

193 194 195 196
## 更多示例程序

| 示例程序                                                                                                                                   | 示例程序描述                           |
| ------------------------------------------------------------------------------------------------------------------------------------------ | -------------------------------------- |
197 198 199 200 201 202 203 204
| [basicUse](https://github.com/taosdata/taos-connector-node/blob/3.0/nodejs/examples/queryExample.js)                                  | 基本的使用如如建立连接,执行 SQL 等操作。                       |
| [stmtBindBatch](https://github.com/taosdata/taos-connector-node/blob/3.0/nodejs/examples/bindParamBatch.js)                  | 绑定多行参数插入的示例。               | |
| [stmtBindSingleParamBatch](https://github.com/taosdata/taos-connector-node/blob/3.0/nodejs/examples/bindSingleParamBatch.js) | 按列绑定参数插入的示例。               |
| [stmtQuery](https://github.com/taosdata/taos-connector-node/blob/3.0/nodejs/examples/stmtQuery.js)                       | 绑定参数查询的示例。                   |
| [schemless insert](https://github.com/taosdata/taos-connector-node/blob/3.0/nodejs/examples/schemaless.js)                   | schemless 插入的示例。                 |
| [TMQ](https://github.com/taosdata/taos-connector-node/blob/3.0/nodejs/examples/tmq.js)                                 | 订阅的使用示例。                       |
| [asyncQuery](https://github.com/taosdata/taos-connector-node/blob/3.0/nodejs/examples/asyncQueryExample.js)                                         | 异步查询的使用示例。                   |
| [REST](https://github.com/taosdata/taos-connector-node/blob/3.0/typescript-rest/example/example.ts)                                    | 使用 REST 连接的 TypeScript 使用示例。 |
205 206 207

## 使用限制

208
native 连接器(`@tdengine/client`) >= v3.0.0 目前支持 node 的版本为:支持 >=v12.8.0 <= v12.9.1 || >=v10.20.0 <= v10.9.0 ;2.0.5 及更早版本支持 v10.x 版本,其他版本可能存在包兼容性的问题。
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223

## 其他说明

Node.js 连接器的使用参见[视频教程](https://www.taosdata.com/blog/2020/11/11/1957.html)。

## 常见问题

1. 使用 REST 连接需要启动 taosadapter。

   ```bash
   sudo systemctl start taosadapter
   ```

2. Node.js 版本

224
   原生连接器 `@tdengine/client` 目前兼容的 Node.js 版本为:>=v10.20.0 <= v10.9.0 || >=v12.8.0 <= v12.9.1
225 226 227 228 229 230 231 232 233

3. "Unable to establish connection","Unable to resolve FQDN"

  一般都是因为配置 FQDN 不正确。 可以参考[如何彻底搞懂 TDengine 的 FQDN](https://www.taosdata.com/blog/2021/07/29/2741.html) 。

## 重要更新记录

### 原生连接器

234 235 236 237 238 239
| package name     | version | TDengine version    | 说明                                                             |
|------------------|---------|---------------------|------------------------------------------------------------------|
| @tdengine/client | 3.0.0   | 3.0.0               | 支持TDengine 3.0 且不与2.x 兼容。                                                          |
| td2.0-connector  | 2.0.12  | 2.4.x;2.5.x;2.6.x | 修复 cursor.close() 报错的 bug。                                 |
| td2.0-connector  | 2.0.11  | 2.4.x;2.5.x;2.6.x | 支持绑定参数、json tag、schemaless 接口等功能。                  |
| td2.0-connector  | 2.0.10  | 2.4.x;2.5.x;2.6.x | 支持连接管理,普通查询、连续查询、获取系统信息、订阅功能等功能。 |
240 241
### REST 连接器

242 243 244 245 246 247 248
| package name         | version | TDengine version    | 说明                                                                      |
|----------------------|---------|---------------------|---------------------------------------------------------------------------|
| @tdengine/rest       | 3.0.0   | 3.0.0               | 支持 TDegnine 3.0,且不与2.x 兼容。                                               |
| td2.0-rest-connector | 1.0.7   | 2.4.x;2.5.x;2.6.x | 移除默认端口 6041。                                                       |
| td2.0-rest-connector | 1.0.6   | 2.4.x;2.5.x;2.6.x | 修复create,insert,update,alter 等SQL 执行返回的 affectRows 错误的bug。 |
| td2.0-rest-connector | 1.0.5   | 2.4.x;2.5.x;2.6.x | 支持云服务 cloud Token;                                                  |
| td2.0-rest-connector | 1.0.3   | 2.4.x;2.5.x;2.6.x | 支持连接管理、普通查询、获取系统信息、错误信息、连续查询等功能。          |
249 250 251

## API 参考

252
[API 参考](https://docs.taosdata.com/api/td2.0-connector/)