From 5a00db8e427de6c4dff3e1fa4cd0bd10324e1149 Mon Sep 17 00:00:00 2001 From: fang Date: Wed, 4 Dec 2019 09:04:57 +0800 Subject: [PATCH 1/2] Chinese version for node.js connector --- .../webdocs/markdowndocs/Connector.md | 8 +- .../webdocs/markdowndocs/connector-ch.md | 136 ++++++++++++++++++ 2 files changed, 140 insertions(+), 4 deletions(-) diff --git a/documentation/webdocs/markdowndocs/Connector.md b/documentation/webdocs/markdowndocs/Connector.md index 2ecb14c710..a979c0158e 100644 --- a/documentation/webdocs/markdowndocs/Connector.md +++ b/documentation/webdocs/markdowndocs/Connector.md @@ -533,15 +533,15 @@ To interact with TDengine, we make use of the [node-gyp](https://github.com/node in your Terminal) - - This step will install `gcc` and the related toolchain containing `make` + - 安装`gcc` 和`make` -### On Windows +### Windows -#### Option 1 +#### 安装方法1 Install all the required tools and configurations using Microsoft's [windows-build-tools](https://github.com/felixrieseberg/windows-build-tools) using `npm install --global --production windows-build-tools` from an elevated PowerShell or CMD.exe (run as Administrator). -#### Option 2 +#### 安装方法2 Install tools and configuration manually: diff --git a/documentation/webdocs/markdowndocs/connector-ch.md b/documentation/webdocs/markdowndocs/connector-ch.md index 4266ddffc4..1cc03703be 100644 --- a/documentation/webdocs/markdowndocs/connector-ch.md +++ b/documentation/webdocs/markdowndocs/connector-ch.md @@ -524,3 +524,139 @@ taosSql驱动包内采用cgo模式,调用了TDengine的C/C++同步接口,与 3. 创建表、写入和查询数据 在创建好了数据库后,就可以开始创建表和写入查询数据了。这些操作的基本思路都是首先组装SQL语句,然后调用db.Exec执行,并检查错误信息和执行相应的处理。可以参考上面的样例代码 + +## Node.js Connector + +TDengine 同时也提供了node.js 的连接器。用户可以通过[npm](https://www.npmjs.com/)来进行安装,也可以通过源代码*src/connector/nodejs/* 来进行安装。[具体安装步骤如下](https://github.com/taosdata/tdengine/tree/master/src/connector/nodejs): + +首先,通过[npm](https://www.npmjs.com/)安装node.js 连接器. + +```cmd +npm install td-connector +``` +我们建议用户使用npm 安装node.js连接器。如果您没有安装npm, 可以将*src/connector/nodejs/*拷贝到您的nodejs 项目目录下 + +To interact with TDengine, we make use of the [node-gyp](https://github.com/nodejs/node-gyp) library. To install, you will need to install the following depending on platform (the following instructions are quoted from node-gyp)我们使用[node-gyp](https://github.com/nodejs/node-gyp)和TDengine服务端进行交互。安装node.js 连接器之前,还需安装以下软件: + +### Unix + +- `python` (建议`v2.7` , `v3.x.x` 目前还不支持) +- `make` +- c语言编译器比如[GCC](https://gcc.gnu.org) + +### macOS + +- `python` (建议`v2.7` , `v3.x.x` 目前还不支持) + +- Xcode + + - 然后通过Xcode安装 + + ``` + Command Line Tools + ``` + + 在 + ``` + Xcode -> Preferences -> Locations + ``` + + 目录下可以找到这个工具。或者在终端里执行 + + ``` + xcode-select --install + ``` + + + - 该步执行后 `gcc` 和 `make`就被安装上了 + +### 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 2.7](https://www.python.org/downloads/) (`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". + +### 使用方法 + +(http://docs.taosdata.com/node) +以下是node.js 连接器的一些基本使用方法,详细的使用方法可参考[该文档](http://docs.taosdata.com/node) + +#### 连接 + +使用node.js连接器时,必须先require```td-connector```,然后使用 ```taos.connect``` 函数。```taos.connect``` 函数必须提供的参数是```host```,其它参数在没有提供的情况下会使用如下的默认值。最后需要初始化```cursor``` 来和TDengine服务端通信 + +```javascript +const taos = require('td-connector'); +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 +``` + +关闭连接可执行 + +```javascript +conn.close(); +``` + +#### 查询 + +可通过 ```cursor.query``` 函数来查询数据库。 + +```javascript +var query = cursor.query('show databases;') +``` + +查询的结果可以通过 ```query.execute()``` 函数获取并打印出来 + +```javascript +var promise = query.execute(); +promise.then(function(result) { + result.pretty(); +}); +``` +格式化查询语句还可以使用```query```的```bind```方法。如下面的示例:```query```会自动将提供的数值填入查询语句的```?```里。 + +```javascript +var query = cursor.query('select * from meterinfo.meters where ts <= ? and areaid = ?;').bind(new Date(), 5); +query.execute().then(function(result) { + result.pretty(); +}) +``` +如果在```query```语句里提供第二个参数并设为```true```也可以立即获取查询结果。如下: + + +```javascript +var promise = cursor.query('select * from meterinfo.meters where v1 = 30;', true) +promise.then(function(result) { + result.pretty(); +}) +``` +#### 异步函数 +异步查询数据库的操作和上面类似,只需要在`cursor.execute`, `TaosQuery.execute`等函数后面加上`_a`。 +```javascript +var promise1 = cursor.query('select count(*), avg(v1), avg(v2) from meter1;').execute_a() +var promise2 = cursor.query('select count(*), avg(v1), avg(v2) from meter2;').execute_a(); +promise1.then(function(result) { + result.pretty(); +}) +promise2.then(function(result) { + result.pretty(); +}) +``` + + +### 示例 +[这里](https://github.com/taosdata/TDengine/tree/master/tests/examples/nodejs/node-example.js)提供了一个使用NodeJS 连接器建表,插入天气数据并查询插入的数据的代码示例 + +[这里](https://github.com/taosdata/TDengine/tree/master/tests/examples/nodejs/node-example-raw.js)同样是一个使用NodeJS 连接器建表,插入天气数据并查询插入的数据的代码示例,但和上面不同的是,该示例只使用`cursor`. \ No newline at end of file From ccb02180f41a7746f21c95ecbda04cf327570515 Mon Sep 17 00:00:00 2001 From: fang Date: Wed, 4 Dec 2019 09:10:49 +0800 Subject: [PATCH 2/2] Chinese version for node.js connector --- documentation/webdocs/markdowndocs/Connector.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/documentation/webdocs/markdowndocs/Connector.md b/documentation/webdocs/markdowndocs/Connector.md index a979c0158e..2ecb14c710 100644 --- a/documentation/webdocs/markdowndocs/Connector.md +++ b/documentation/webdocs/markdowndocs/Connector.md @@ -533,15 +533,15 @@ To interact with TDengine, we make use of the [node-gyp](https://github.com/node in your Terminal) - - 安装`gcc` 和`make` + - This step will install `gcc` and the related toolchain containing `make` -### Windows +### On Windows -#### 安装方法1 +#### Option 1 Install all the required tools and configurations using Microsoft's [windows-build-tools](https://github.com/felixrieseberg/windows-build-tools) using `npm install --global --production windows-build-tools` from an elevated PowerShell or CMD.exe (run as Administrator). -#### 安装方法2 +#### Option 2 Install tools and configuration manually: