node.mdx 9.8 KB
Newer Older
B
Bo Ding 已提交
1
---
D
dingbo 已提交
2 3
sidebar_position: 6
sidebar_label: Node.js
D
dingbo 已提交
4
title: Node.js Connector
B
Bo Ding 已提交
5 6
---

7 8 9 10 11
## 总体介绍

td2.0-connector 是 TDengine 的官方 Node.js 连接器。Node.js 开发人员可以通过它开发可以存取 TDengine 数据的应用软件。

## 支持的平台
B
Bo Ding 已提交
12 13 14 15 16 17 18 19

Node.js 连接器支持的系统有:

| **CPU 类型** | x64(64bit) |          |          | aarch64  | aarch32  |
| ------------ | ------------ | -------- | -------- | -------- | -------- |
| **OS 类型**  | Linux        | Win64    | Win32    | Linux    | Linux    |
| **支持与否** | **支持**     | **支持** | **支持** | **支持** | **支持** |

20
## 版本支持
B
Bo Ding 已提交
21

22 23 24 25 26 27 28 29
| td2.0-connector 版本 | TDengine 版本                                                    |
|--------------------|------------------------------------------------------------------|
| 2.0.7              | ver-2.5.0.x、ver-2.4.x.x、ver-2.3.x.x、ver-2.2.x.x、>ver-2.1.1.0 |
| 2.0.8              | ver-2.5.0.x、ver-2.4.x.x、ver-2.3.x.x、ver-2.2.x.x、>ver-2.1.4.1 |
| 2.0.9              | ver-2.5.0.x、ver-2.4.x.x、ver-2.3.x.x、ver-2.2.x.x、>ver-2.1.4.1 |
| 2.0.10             | ver-2.5.0.x、ver-2.4.x.x、ver-2.3.x.x                            |
| 2.0.11             | ver-2.5.0.x、ver-2.4.x.x、ver-2.3.x.x                            |
| 2.0.12             | ver-2.5.0.x、ver-2.4.x.x、ver-2.3.x.x                            |
B
Bo Ding 已提交
30

31
## 支持的特性
B
Bo Ding 已提交
32

33
### 本地连接
B
Bo Ding 已提交
34

35 36
"本地连接" 指连接器通过本地的客户端驱动程序 taosc 直接与服务端程序 taosd 建立连接。
本地连接支持的特新如下:
B
Bo Ding 已提交
37

38 39 40 41 42 43 44 45
1. 连接管理
2. 同步查询
3. 异步查询
4. 参数绑定
5. 获取系统信息
6. 错误信息
7. 订阅功能
8. Schemaless
B
Bo Ding 已提交
46

47 48 49 50 51 52 53 54 55 56 57 58 59
### RESTful 连接

1. 连接管理
2. 同步查询
3. 异步查询(暂不支持)
4. 参数绑定(暂不支持)
5. 获取系统信息
6. 错误信息
7. 订阅功能(暂不支持)
8. Schemaless
9. 批量拉取(暂不支持)

## 安装步骤
B
Bo Ding 已提交
60

61
### 安装前的准备
B
Bo Ding 已提交
62

63 64
- 应用驱动安装请参考[安装连接器驱动步骤](/reference/connector/#安装客户端驱动)。
我们使用[node-gyp](https://github.com/nodejs/node-gyp)和 TDengine 服务端进行交互。安装 Node.js 本地驱动连接器之前,还需要根据具体操作系统来安装下文提到的一些依赖工具。如果要安装的是 RESTful 连接器则可以跳过以下步骤,直接只用 npm 安装。
B
Bo Ding 已提交
65 66 67 68

### Linux

- `python` (建议`v2.7` , `v3.x.x` 目前还不支持)
69
- `node` 2.0.6 支持 >=v12.8.0 <= v12.9.1 || >=v10.20.0 <= v10.9.0 ;2.0.5 及更早版本支持 v10.x 版本,其他版本可能存在包兼容性的问题。
B
Bo Ding 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
- `make`
- c 语言编译器比如[GCC](https://gcc.gnu.org)

### 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`

87
如果以上步骤不能成功执行,可以参考微软的 Node.js 用户手册[Microsoft's Node.js Guidelines for Windows](https://github.com/Microsoft/nodejs-guidelines/blob/master/windows-environment.md#compiling-native-addon-modules)。
B
Bo Ding 已提交
88 89 90

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

91
### 使用 npm 安装
B
Bo Ding 已提交
92

93
- 安装本地客户端驱动连接器
B
Bo Ding 已提交
94

95 96 97 98 99 100 101 102 103
``` bash
npm install td2.0-connector
```

- 安装 RESTful 连接器

```bash
npm i td2.0-rest-connector
```
B
Bo Ding 已提交
104 105 106

### 安装验证

107
在安装好 TDengine 客户端后,使用 nodejsChecker.js 程序能够验证当前环境是否支持 Node.js 方式访问 TDengine。
B
Bo Ding 已提交
108 109 110

验证方法:

111
- 新建安装验证目录,例如:`~/tdengine-test`,拷贝 github 上 [nodejsChecker.js 源程序](https://github.com/taosdata/TDengine/tree/develop/examples/nodejs/nodejsChecker.js)。
B
Bo Ding 已提交
112

113
- 在命令行中执行以下命令。
B
Bo Ding 已提交
114 115 116 117 118 119 120

```bash
npm init -y
npm install td2.0-connector
node nodejsChecker.js host=localhost
```

121
- 执行以上步骤后,在命令行会输出 nodejs 连接 TDengine 实例,并执行简答插入和查询的结果。
B
Bo Ding 已提交
122

123
## 建立连接
B
Bo Ding 已提交
124

125
### 建立本地连接
B
Bo Ding 已提交
126

127
安装并引用 `td2.0-connector` 包。
B
Bo Ding 已提交
128 129

```javascript
130 131 132
//A cursor also needs to be initialized in order to interact with TDengine from Node.js.
const taos = require('td2.0-connector');
var conn = taos.connect({host:"127.0.0.1", user:"root", password:"taosdata", config:"/etc/taos",port:0})
B
Bo Ding 已提交
133 134
var cursor = conn.cursor(); // Initializing a new cursor

135 136
//Close a connection
conn.close();
B
Bo Ding 已提交
137 138
```

139
### 建立RESTful连接
B
Bo Ding 已提交
140

141
安装并引用 `td2.0-rest-connector` 包。
B
Bo Ding 已提交
142

143 144 145 146 147 148 149
``` javascript
//A cursor also needs to be initialized in order to interact with TDengine from Node.js.
import { options, connect } from 'td2.0-rest-connector'
options.path='/rest/sqlt';
// set host
options.host='localhost';
// set other options like user/passwd
B
Bo Ding 已提交
150

151 152
let conn = connect(options);
let cursor = conn.cursor();
B
Bo Ding 已提交
153 154 155

```

156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
## 使用示例
| 示例程序                                                                                                                                      | 示例程序描述                         |
|-----------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------|
| [connection](https://github.com/taosdata/TDengine/blob/develop/src/connector/nodejs/examples/cursorClose.js)                                  | 建立本地连接的示例。                 |
| [stmtBindBatch](https://github.com/taosdata/TDengine/blob/develop/src/connector/nodejs/examples/stmtBindParamBatchSample.js)                  | 绑定多行参数插入的示例。             |
| [stmtBind](https://github.com/taosdata/TDengine/blob/develop/src/connector/nodejs/examples/stmtBindParamSample.js)                            | 一行一行绑定参数插入的示例。         |
| [stmtBindSingleParamBatch](https://github.com/taosdata/TDengine/blob/develop/src/connector/nodejs/examples/stmtBindSingleParamBatchSample.js) | 按列绑定参数插入的示例。             |
| [stmtUseResult](https://github.com/taosdata/TDengine/blob/develop/src/connector/nodejs/examples/stmtUseResultSample.js)                       | 绑定参数查询的示例。                 |
| [json   tag](https://github.com/taosdata/TDengine/blob/develop/src/connector/nodejs/examples/testJsonTag.js)                                  | Json tag 的使用示例。                |
| [Nanosecond](https://github.com/taosdata/TDengine/blob/develop/src/connector/nodejs/examples/testNanoseconds.js)                              | 时间戳为纳秒精度的使用的示例。       |
| [Microsecond](https://github.com/taosdata/TDengine/blob/develop/src/connector/nodejs/examples/testMicroseconds.js)                            | 时间戳为微秒精度的使用的示例。       |
| [schemless   insert](https://github.com/taosdata/TDengine/blob/develop/src/connector/nodejs/examples/testSchemalessInsert.js)                 | schemless 插入的示例。               |
| [subscribe](https://github.com/taosdata/TDengine/blob/develop/src/connector/nodejs/examples/testSubscribe.js)                                 | 订阅的使用示例。                     |
| [asyncQuery](https://github.com/taosdata/TDengine/blob/develop/src/connector/nodejs/examples/tset.js)                                         | 异步查询的使用示例。                 |
| [RESTful](https://github.com/taosdata/TDengine/tree/develop/src/connector/TypeScript-REST/example)                                            | RESTful 连接的 TypeScript 使用示例。 |
B
Bo Ding 已提交
171

172
## 使用限制
B
Bo Ding 已提交
173

174
Node.js 的本地连接器 >= v2.0.6 目前支持 node 的版本为: 支持 >=v12.8.0 <= v12.9.1 || >=v10.20.0 <= v10.9.0 ;2.0.5 及更早版本支持 v10.x 版本,其他版本可能存在包兼容性的问题。
D
dingbo 已提交
175

176
## 重要更新记录
177

178 179 180 181 182
| td2.0-connector 版本 | 说明                                                                 |
|--------------------|----------------------------------------------------------------------|
| 2.0.10             | 支持连接管理,同步查询、异步查询、获取系统信息、错误信息、订阅功能。 |
| 2.0.11             | 支持绑定参数、json tag、schemaless insert。                          |
| 2.0.12             | 修复 cursor.close() 报错的问题。                                     |
183

184 185 186
| td2.0-rest-connector 版本 | 说明                                                         |
|--------------------|--------------------------------------------------------------|
| 1.0.3              | 支持连接管理、同步查询、获取系统信息、错误信息、schemeless。 |
187

188
## 其他说明(optional)
189

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

192
## 常见问题(optional)
193

194
1. 使用 RESTful 连接需要启动 taosadapter。
195

196 197 198
   ``` bash
   sudo systemctl start taosadapter
   ```
199

200
2. 本地客户端驱动连接器 >v2.0.6 目前兼容的 node 为:>=v12.8.0 <= v12.9.1 || >=v10.20.0 <= v10.9.0。
201

202
3. "Unable to establish connection","Unable to resolve FQDN", 一般都是应为为配置 FQDN 可以参考[如何彻底搞懂 TDengine 的 FQDN](https://www.taosdata.com/blog/2021/07/29/2741.html) 。
203

204
## API 参考
205

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