提交 09c2189f 编写于 作者: P Ping Xiao

Merge branch 'develop' into xiaoping/cases

...@@ -126,7 +126,7 @@ sudo yum install zlib-devel xz-devel snappy-devel jansson jansson-devel pkgconfi ...@@ -126,7 +126,7 @@ sudo yum install zlib-devel xz-devel snappy-devel jansson jansson-devel pkgconfi
注意:由于 snappy 缺乏 pkg-config 支持 注意:由于 snappy 缺乏 pkg-config 支持
(参考 [链接](https://github.com/google/snappy/pull/86)),会导致 (参考 [链接](https://github.com/google/snappy/pull/86)),会导致
cmake 提示无法发现 libsnappy,实际上工作正常。 cmake 提示无法发现 libsnappy,实际上工作正常。
## 获取源码 ## 获取源码
...@@ -229,13 +229,17 @@ cmake .. && cmake --build . ...@@ -229,13 +229,17 @@ cmake .. && cmake --build .
# 安装 # 安装
生成完成后,安装 TDengine(下文给出的指令以 Linux 为例,如果是在 Windows 下,那么对应的指令会是 `nmake install`): ## Linux 系统
生成完成后,安装 TDengine:
```bash ```bash
sudo make install sudo make install
``` ```
用户可以在[文件目录结构](https://www.taosdata.com/cn/documentation/administrator#directories)中了解更多在操作系统中生成的目录或文件。 用户可以在[文件目录结构](https://www.taosdata.com/cn/documentation/administrator#directories)中了解更多在操作系统中生成的目录或文件。
从 2.0 版本开始, 从源代码安装也会为 TDengine 配置服务管理。
用户也可以选择[从安装包中安装](https://www.taosdata.com/en/getting-started/#Install-from-Package)
安装成功后,在终端中启动 TDengine 服务: 安装成功后,在终端中启动 TDengine 服务:
...@@ -251,6 +255,40 @@ taos ...@@ -251,6 +255,40 @@ taos
如果 TDengine Shell 连接服务成功,将会打印出欢迎消息和版本信息。如果失败,则会打印出错误消息。 如果 TDengine Shell 连接服务成功,将会打印出欢迎消息和版本信息。如果失败,则会打印出错误消息。
## Windows 系统
生成完成后,安装 TDengine:
```cmd
nmake install
```
## macOS 系统
生成完成后,安装 TDengine:
```bash
sudo make install
```
安装成功后,如果想以服务形式启动,先配置 `.plist` 文件,在终端中执行:
```bash
sudo cp ../packaging/macOS/com.taosdata.tdengine.plist /Library/LaunchDaemons
```
在终端中启动 TDengine 服务:
```bash
sudo launchctl load /Library/LaunchDaemons/com.taosdata.tdengine.plist
```
在终端中停止 TDengine 服务:
```bash
sudo launchctl unload /Library/LaunchDaemons/com.taosdata.tdengine.plist
```
## 快速运行 ## 快速运行
如果不希望以服务方式运行 TDengine,也可以在终端中直接运行它。也即在生成完成后,执行以下命令(在 Windows 下,生成的可执行文件会带有 .exe 后缀,例如会名为 taosd.exe ): 如果不希望以服务方式运行 TDengine,也可以在终端中直接运行它。也即在生成完成后,执行以下命令(在 Windows 下,生成的可执行文件会带有 .exe 后缀,例如会名为 taosd.exe ):
...@@ -271,7 +309,7 @@ taos ...@@ -271,7 +309,7 @@ taos
在 TDengine 终端中,用户可以通过 SQL 命令来创建/删除数据库、表等,并进行插入查询操作。 在 TDengine 终端中,用户可以通过 SQL 命令来创建/删除数据库、表等,并进行插入查询操作。
```bash ```sql
CREATE DATABASE demo; CREATE DATABASE demo;
USE demo; USE demo;
CREATE TABLE t (ts TIMESTAMP, speed INT); CREATE TABLE t (ts TIMESTAMP, speed INT);
...@@ -325,8 +363,8 @@ TDengine 的测试框架和所有测试例全部开源。 ...@@ -325,8 +363,8 @@ TDengine 的测试框架和所有测试例全部开源。
# 加入技术交流群 # 加入技术交流群
TDengine 官方社群「物联网大数据群」对外开放,欢迎您加入讨论。搜索微信号 "tdengine",加小T为好友,即可入群。 TDengine 官方社群「物联网大数据群」对外开放,欢迎您加入讨论。搜索微信号 "tdengine",加小 T 为好友,即可入群。
# [谁在使用TDengine](https://github.com/taosdata/TDengine/issues/2432) # [谁在使用 TDengine](https://github.com/taosdata/TDengine/issues/2432)
欢迎所有 TDengine 用户及贡献者在 [这里](https://github.com/taosdata/TDengine/issues/2432) 分享您在当前工作中开发/使用 TDengine 的故事。 欢迎所有 TDengine 用户及贡献者在 [这里](https://github.com/taosdata/TDengine/issues/2432) 分享您在当前工作中开发/使用 TDengine 的故事。
...@@ -34,7 +34,7 @@ TDengine can be widely applied to Internet of Things (IoT), Connected Vehicles, ...@@ -34,7 +34,7 @@ TDengine can be widely applied to Internet of Things (IoT), Connected Vehicles,
# Documentation # Documentation
For user manual, system design and architecture, engineering blogs, refer to [TDengine Documentation](https://www.taosdata.com/en/documentation/)(中文版请点击[这里](https://www.taosdata.com/cn/documentation20/)) For user manual, system design and architecture, engineering blogs, refer to [TDengine Documentation](https://www.taosdata.com/en/documentation/)(中文版请点击[这里](https://www.taosdata.com/cn/documentation20/))
for details. The documentation from our website can also be downloaded locally from *documentation/tdenginedocs-en* or *documentation/tdenginedocs-cn*. for details. The documentation from our website can also be downloaded locally from _documentation/tdenginedocs-en_ or _documentation/tdenginedocs-cn_.
# Building # Building
...@@ -258,7 +258,9 @@ cmake .. && cmake --build . ...@@ -258,7 +258,9 @@ cmake .. && cmake --build .
# Installing # Installing
After building successfully, TDengine can be installed by: (On Windows platform, the following command should be `nmake install`) ## On Linux platform
After building successfully, TDengine can be installed by
```bash ```bash
sudo make install sudo make install
...@@ -281,7 +283,7 @@ taos ...@@ -281,7 +283,7 @@ taos
If TDengine shell connects the server successfully, welcome messages and version info are printed. Otherwise, an error message is shown. If TDengine shell connects the server successfully, welcome messages and version info are printed. Otherwise, an error message is shown.
## Install TDengine by apt-get ### Install TDengine by apt-get
If you use Debian or Ubuntu system, you can use 'apt-get' command to install TDengine from official repository. Please use following commands to setup: If you use Debian or Ubuntu system, you can use 'apt-get' command to install TDengine from official repository. Please use following commands to setup:
...@@ -294,6 +296,40 @@ apt-cache policy tdengine ...@@ -294,6 +296,40 @@ apt-cache policy tdengine
sudo apt-get install tdengine sudo apt-get install tdengine
``` ```
## On Windows platform
After building successfully, TDengine can be installed by:
```cmd
nmake install
```
## On macOS platform
After building successfully, TDengine can be installed by:
```bash
sudo make install
```
To start the service after installation, config `.plist` file first, in a terminal, use:
```bash
sudo cp ../packaging/macOS/com.taosdata.tdengine.plist /Library/LaunchDaemons
```
To start the service, in a terminal, use:
```bash
sudo launchctl load /Library/LaunchDaemons/com.taosdata.tdengine.plist
```
To stop the service, in a terminal, use:
```bash
sudo launchctl unload /Library/LaunchDaemons/com.taosdata.tdengine.plist
```
## Quick Run ## Quick Run
If you don't want to run TDengine as a service, you can run it in current shell. For example, to quickly start a TDengine server after building, run the command below in terminal: (We take Linux as an example, command on Windows will be `taosd.exe`) If you don't want to run TDengine as a service, you can run it in current shell. For example, to quickly start a TDengine server after building, run the command below in terminal: (We take Linux as an example, command on Windows will be `taosd.exe`)
...@@ -315,13 +351,17 @@ option "-c test/cfg" specifies the system configuration file directory. ...@@ -315,13 +351,17 @@ option "-c test/cfg" specifies the system configuration file directory.
It is easy to run SQL commands from TDengine shell which is the same as other SQL databases. It is easy to run SQL commands from TDengine shell which is the same as other SQL databases.
```sql ```sql
create database db; CREATE DATABASE demo;
use db; USE demo;
create table t (ts timestamp, a int); CREATE TABLE t (ts TIMESTAMP, speed INT);
insert into t values ('2019-07-15 00:00:00', 1); INSERT INTO t VALUES('2019-07-15 00:00:00', 10);
insert into t values ('2019-07-15 01:00:00', 2); INSERT INTO t VALUES('2019-07-15 01:00:00', 20);
select * from t; SELECT * FROM t;
drop database db; ts | speed |
===================================
19-07-15 00:00:00.000| 10|
19-07-15 01:00:00.000| 20|
Query OK, 2 row(s) in set (0.001700s)
``` ```
# Developing with TDengine # Developing with TDengine
...@@ -348,8 +388,8 @@ The TDengine community has also kindly built some of their own connectors! Follo ...@@ -348,8 +388,8 @@ The TDengine community has also kindly built some of their own connectors! Follo
# How to run the test cases and how to add a new test case # How to run the test cases and how to add a new test case
TDengine's test framework and all test cases are fully open source. TDengine's test framework and all test cases are fully open source.
Please refer to [this document](https://github.com/taosdata/TDengine/blob/develop/tests/How-To-Run-Test-And-How-To-Add-New-Test-Case.md) for how to run test and develop new test case. Please refer to [this document](https://github.com/taosdata/TDengine/blob/develop/tests/How-To-Run-Test-And-How-To-Add-New-Test-Case.md) for how to run test and develop new test case.
# TDengine Roadmap # TDengine Roadmap
......
# Alert # Alert
The Alert application reads data from [TDEngine](https://www.taosdata.com/), calculating according to predefined rules to generate alerts, and pushes alerts to downstream applications like [AlertManager](https://github.com/prometheus/alertmanager). The Alert application reads data from [TDengine](https://www.taosdata.com/), calculating according to predefined rules to generate alerts, and pushes alerts to downstream applications like [AlertManager](https://github.com/prometheus/alertmanager).
## Install ## Install
...@@ -22,7 +22,7 @@ $ ./install_driver.sh ...@@ -22,7 +22,7 @@ $ ./install_driver.sh
Two prerequisites are required to install from source. Two prerequisites are required to install from source.
1. TDEngine server or client must be installed. 1. TDengine server or client must be installed.
2. Latest [Go](https://golang.org) language must be installed. 2. Latest [Go](https://golang.org) language must be installed.
When these two prerequisites are ready, please follow steps below to build the application: When these two prerequisites are ready, please follow steps below to build the application:
...@@ -61,13 +61,13 @@ The use of each configuration item is: ...@@ -61,13 +61,13 @@ The use of each configuration item is:
* **port**: This is the `http` service port which enables other application to manage rules by `restful API`. * **port**: This is the `http` service port which enables other application to manage rules by `restful API`.
* **database**: rules are stored in a `sqlite` database, this is the path of the database file (if the file does not exist, the alert application creates it automatically). * **database**: rules are stored in a `sqlite` database, this is the path of the database file (if the file does not exist, the alert application creates it automatically).
* **tdengine**: connection string of `TDEngine` server (please refer the documentation of GO connector for the detailed format of this string), note the database name should be put in the `sql` field of a rule in most cases, thus it should NOT be included in the string. * **tdengine**: connection string of `TDengine` server (please refer the documentation of GO connector for the detailed format of this string), note the database name should be put in the `sql` field of a rule in most cases, thus it should NOT be included in the string.
* **log > level**: log level, could be `production` or `debug`. * **log > level**: log level, could be `production` or `debug`.
* **log > path**: log output file path. * **log > path**: log output file path.
* **receivers > alertManager**: the alert application pushes alerts to `AlertManager` at this URL. * **receivers > alertManager**: the alert application pushes alerts to `AlertManager` at this URL.
* **receivers > console**: print out alerts to console (stdout) or not. * **receivers > console**: print out alerts to console (stdout) or not.
When the configruation file is ready, the alert application can be started with below command (`alert.cfg` is the path of the configuration file): When the configuration file is ready, the alert application can be started with below command (`alert.cfg` is the path of the configuration file):
``` ```
$ ./alert -cfg alert.cfg $ ./alert -cfg alert.cfg
...@@ -75,7 +75,7 @@ $ ./alert -cfg alert.cfg ...@@ -75,7 +75,7 @@ $ ./alert -cfg alert.cfg
## Prepare an alert rule ## Prepare an alert rule
From technical aspect, an alert could be defined as: query and filter recent data from `TDEngine`, and calculating out a boolean value from these data according to a formula, and trigger an alert if the boolean value last for a certain duration. From technical aspect, an alert could be defined as: query and filter recent data from `TDengine`, and calculating out a boolean value from these data according to a formula, and trigger an alert if the boolean value last for a certain duration.
This is a rule example in `json` format: This is a rule example in `json` format:
...@@ -98,8 +98,8 @@ This is a rule example in `json` format: ...@@ -98,8 +98,8 @@ This is a rule example in `json` format:
The fields of the rule is explained below: The fields of the rule is explained below:
* **name**: the name of the rule, must be unique. * **name**: the name of the rule, must be unique.
* **sql**: this is the `sql` statement used to query data from `TDEngine`, columns of the query result are used in later processing, so please give the column an alias if aggregation functions are used. * **sql**: this is the `sql` statement used to query data from `TDengine`, columns of the query result are used in later processing, so please give the column an alias if aggregation functions are used.
* **expr**: an expression whose result is a boolean value, arithmatic and logical calculations can be included in the expression, and builtin functions (see below) are also supported. Alerts are only triggered when the expression evaluates to `true`. * **expr**: an expression whose result is a boolean value, arithmetic and logical calculations can be included in the expression, and builtin functions (see below) are also supported. Alerts are only triggered when the expression evaluates to `true`.
* **for**: this item is a duration which default value is zero second. when `expr` evaluates to `true` and last at least this duration, an alert is triggered. * **for**: this item is a duration which default value is zero second. when `expr` evaluates to `true` and last at least this duration, an alert is triggered.
* **period**: the interval for the alert application to check the rule, default is 1 minute. * **period**: the interval for the alert application to check the rule, default is 1 minute.
* **labels**: a label list, labels are used to generate alert information. note if the `sql` statement includes a `group by` clause, the `group by` columns are inserted into this list automatically. * **labels**: a label list, labels are used to generate alert information. note if the `sql` statement includes a `group by` clause, the `group by` columns are inserted into this list automatically.
......
# Alert # Alert
报警监测程序,从 [TDEngine](https://www.taosdata.com/) 读取数据后,根据预定义的规则计算和生成报警,并将它们推送到 [AlertManager](https://github.com/prometheus/alertmanager) 或其它下游应用。 报警监测程序,从 [TDengine](https://www.taosdata.com/) 读取数据后,根据预定义的规则计算和生成报警,并将它们推送到 [AlertManager](https://github.com/prometheus/alertmanager) 或其它下游应用。
## 安装 ## 安装
...@@ -20,7 +20,7 @@ $ ./install_driver.sh ...@@ -20,7 +20,7 @@ $ ./install_driver.sh
### 从源码安装 ### 从源码安装
从源码安装需要在您用于编译的计算机上提前安装好 TDEngine 的服务端或客户端,如果您还没有安装,可以参考 TDEngine 的文档。 从源码安装需要在您用于编译的计算机上提前安装好 TDengine 的服务端或客户端,如果您还没有安装,可以参考 TDengine 的文档。
报警监测程序使用 [Go语言](https://golang.org) 开发,请安装最新版的 Go 语言编译环境。 报警监测程序使用 [Go语言](https://golang.org) 开发,请安装最新版的 Go 语言编译环境。
...@@ -58,7 +58,7 @@ $ go build ...@@ -58,7 +58,7 @@ $ go build
* **port**:报警监测程序支持使用 `restful API` 对规则进行管理,这个参数用于配置 `http` 服务的侦听端口。 * **port**:报警监测程序支持使用 `restful API` 对规则进行管理,这个参数用于配置 `http` 服务的侦听端口。
* **database**:报警监测程序将规则保存到了一个 `sqlite` 数据库中,这个参数用于指定数据库文件的路径(不需要提前创建这个文件,如果它不存在,程序会自动创建它)。 * **database**:报警监测程序将规则保存到了一个 `sqlite` 数据库中,这个参数用于指定数据库文件的路径(不需要提前创建这个文件,如果它不存在,程序会自动创建它)。
* **tdengine**`TDEngine` 的连接字符串(这个字符串的详细格式说明请见 GO 连接器的文档),一般来说,数据库名应该在报警规则的 `sql` 语句中指定,所以这个字符串中 **不** 应包含数据库名。 * **tdengine**`TDengine` 的连接字符串(这个字符串的详细格式说明请见 GO 连接器的文档),一般来说,数据库名应该在报警规则的 `sql` 语句中指定,所以这个字符串中 **不** 应包含数据库名。
* **log > level**:日志的记录级别,可选 `production``debug` * **log > level**:日志的记录级别,可选 `production``debug`
* **log > path**:日志文件的路径。 * **log > path**:日志文件的路径。
* **receivers > alertManager**:报警监测程序会将报警推送到 `AlertManager`,在这里指定 `AlertManager` 的接收地址。 * **receivers > alertManager**:报警监测程序会将报警推送到 `AlertManager`,在这里指定 `AlertManager` 的接收地址。
...@@ -72,7 +72,7 @@ $ ./alert -cfg alert.cfg ...@@ -72,7 +72,7 @@ $ ./alert -cfg alert.cfg
## 编写报警规则 ## 编写报警规则
从技术角度,可以将报警描述为:从 `TDEngine` 中查询最近一段时间、符合一定过滤条件的数据,并基于这些数据根据定义好的计算方法得出一个结果,当结果符合某个条件且持续一定时间后,触发报警。 从技术角度,可以将报警描述为:从 `TDengine` 中查询最近一段时间、符合一定过滤条件的数据,并基于这些数据根据定义好的计算方法得出一个结果,当结果符合某个条件且持续一定时间后,触发报警。
根据上面的描述,可以很容易的知道报警规则中需要包含的大部分信息。 以下是一个完整的报警规则,采用标准 `json` 格式: 根据上面的描述,可以很容易的知道报警规则中需要包含的大部分信息。 以下是一个完整的报警规则,采用标准 `json` 格式:
...@@ -95,7 +95,7 @@ $ ./alert -cfg alert.cfg ...@@ -95,7 +95,7 @@ $ ./alert -cfg alert.cfg
其中: 其中:
* **name**:用于为规则指定一个唯一的名字。 * **name**:用于为规则指定一个唯一的名字。
* **sql**:从 `TDEngine` 中查询数据时使用的 `sql` 语句,查询结果中的列将被后续计算使用,所以,如果使用了聚合函数,请为这一列指定一个别名。 * **sql**:从 `TDengine` 中查询数据时使用的 `sql` 语句,查询结果中的列将被后续计算使用,所以,如果使用了聚合函数,请为这一列指定一个别名。
* **expr**:一个计算结果为布尔型的表达式,支持算数运算、逻辑运算,并且内置了部分函数,也可以引用查询结果中的列。 当表达式计算结果为 `true` 时,进入报警状态。 * **expr**:一个计算结果为布尔型的表达式,支持算数运算、逻辑运算,并且内置了部分函数,也可以引用查询结果中的列。 当表达式计算结果为 `true` 时,进入报警状态。
* **for**:当表达式计算结果为 `true` 的连续时长超过这个选项时,触发报警,否则报警处于“待定”状态。默认为0,表示一旦计算结果为 `true`,立即触发报警。 * **for**:当表达式计算结果为 `true` 的连续时长超过这个选项时,触发报警,否则报警处于“待定”状态。默认为0,表示一旦计算结果为 `true`,立即触发报警。
* **period**:规则的检查周期,默认1分钟。 * **period**:规则的检查周期,默认1分钟。
......
...@@ -4,36 +4,71 @@ title: 产品简介 ...@@ -4,36 +4,71 @@ title: 产品简介
toc_max_heading_level: 2 toc_max_heading_level: 2
--- ---
## TDengine 简介 ## TDengine 主要功能
TDengine 是一款高性能、分布式、支持 SQL 的时序数据库。而且除时序数据库功能外,它还提供[缓存](/develop/cache/)、数据订阅、流式计算等功能,最大程度减少研发和运维的复杂度,且核心代码,包括集群功能全部开源(开源协议,AGPL v3.0)。与其他时序数据数据库相比,TDengine 有以下特点 TDengine 是一款高性能、分布式、支持 SQL 的时序数据库,其核心代码,包括集群功能全部开源(开源协议,AGPL v3.0)。TDengine 能被广泛运用于物联网、工业互联网、车联网、IT 运维、金融等领域。除核心的时序数据库功能外,TDengine 还提供[缓存](/develop/cache/)[数据订阅](/develop/subscribe)[流式计算](/develop/continuous-query)等大数据平台所需要的系列功能,最大程度减少研发和运维的复杂度。主要功能如下
- **高性能**:通过创新的存储引擎设计,无论是数据写入还是查询,TDengine 的性能比通用数据库快 10 倍以上,也远超其他时序数据库,而且存储空间也大为节省。 1. 高速数据写入,除 [SQL 写入](/develop/insert-data/sql-writing)外,还支持 [Schemaless 写入](/reference/schemaless/),支持 [InfluxDB LINE 协议](/develop/insert-data/influxdb-line)[OpenTSDB Telnet](/develop/insert-data/opentsdb-telnet), [OpenTSDB JSON ](/develop/insert-data/opentsdb-json)等协议写入;
2. 第三方数据采集工具 [Telegraf](/third-party/telegraf)[Prometheus](/third-party/prometheus)[StatsD](/third-party/statsd)[collectd](/third-party/collectd)[icinga2](/third-party/icinga2), [Tcollector](/third-party/tcollector), [EMQ](/third-party/emq-broker), [HiveMQ](/third-party/hive-mq-broker) 等都可以进行配置后,不用任何代码,即可将数据写入;
3. 支持[各种查询](/query-data),包括聚合查询、嵌套查询、降采样查询、插值等
4. 支持[用户自定义函数](/develop/udf)
5. 支持[缓存](/develop/cache),将每张表的最后一条记录缓存起来,这样无需 Redis
6. 支持[连续查询](/develop/continuous-query)(Continuous Query)
7. 支持[数据订阅](/develop/subscribe),而且可以指定过滤条件
8. 支持[集群](/cluster/),可以通过多节点进行水平扩展,并通过多副本实现高可靠
9. 提供[命令行程序](/reference/taos-shell),便于管理集群,检查系统状态,做即席查询
10. 提供多种数据的[导入](/operation/import)[导出](/operation/export)
11. 支持对[TDengine 集群本身的监控](/operation/monitor)
12. 提供 [C/C++](/reference/connector/cpp), [Java](/reference/connector/java), [Python](/reference/connector/python), [Go](/reference/connector/go), [Rust](/reference/connector/rust), [Node.js](/reference/connector/node) 等多种编程语言的[连接器](/reference/connector/)
13. 支持 [REST 接口](/reference/rest-api/)
14. 支持与[ Grafana 无缝集成](/third-party/grafana)
15. 支持与 Google Data Studio 无缝集成
- **分布式**:通过原生分布式的设计,TDengine 提供了水平扩展的能力,只需要增加节点就能获得更强的数据处理能力,同时通过多副本机制保证了系统的高可用 更多细小的功能,请阅读整个文档
- **支持 SQL**:TDengine 采用 SQL 作为数据查询语言,减少学习和迁移成本,同时提供 SQL 扩展来处理时序数据特有的分析,而且支持方便灵活的 schemaless 数据写入。 ## TDengine 主要亮点
由于 TDengine 充分利用了[时序数据特点](https://www.taosdata.com/blog/2019/07/09/105.html),比如结构化、无需事务、很少删除或更新、写多读少等等,设计了全新的针对时序数据的存储引擎和计算引擎,因此与其他时序数据库相比,TDengine 有以下特点:
- **[高性能](https://www.taosdata.com/fast)**:通过创新的存储引擎设计,无论是数据写入还是查询,TDengine 的性能比通用数据库快 10 倍以上,也远超其他时序数据库,而且存储空间也大为节省。
- **[分布式](https://www.taosdata.com/scalable)**:通过原生分布式的设计,TDengine 提供了水平扩展的能力,只需要增加节点就能获得更强的数据处理能力,同时通过多副本机制保证了系统的高可用。
- **[支持 SQL](https://www.taosdata.com/sql-support)**:TDengine 采用 SQL 作为数据查询语言,减少学习和迁移成本,同时提供 SQL 扩展来处理时序数据特有的分析,而且支持方便灵活的 schemaless 数据写入。
- **All in One**:将数据库、消息队列、缓存、流式计算等功能融合一起,应用无需再集成 Kafka/Redis/HBase/Spark 等软件,大幅降低应用开发和维护成本。 - **All in One**:将数据库、消息队列、缓存、流式计算等功能融合一起,应用无需再集成 Kafka/Redis/HBase/Spark 等软件,大幅降低应用开发和维护成本。
- **零管理**:安装、集群几秒搞定,无任何依赖,不用分库分表,系统运行状态监测能与 Grafana 或其他运维工具无缝集成。 - **零管理**:安装、集群几秒搞定,无任何依赖,不用分库分表,系统运行状态监测能与 Grafana 或其他运维工具无缝集成。
- **零学习成本**:采用 SQL 查询语言,支持 Python, Java, C/C++, Go, Rust, Node.js 等多种编程语言,与 MySQL 相似,零学习成本。 - **零学习成本**:采用 SQL 查询语言,支持 C/C++、Python、Java、Go、Rust、Node.js、C#、Lua(社区贡献)、PHP(社区贡献) 等多种编程语言,与 MySQL 相似,零学习成本。
- **无缝集成**:不用一行代码,即可与 Telegraf, Grafana, EMQX, Prometheus, StatsD, collectd, Matlab, R 等第三方工具无缝集成。 - **无缝集成**:不用一行代码,即可与 Telegraf、Grafana、Prometheus、EMQX、HiveMQ、StatsD、collectd、icinga、TCollector、Matlab、R 等第三方工具无缝集成。
- **互动 Console**: 通过命令行 console,不用编程,执行 SQL 语句就能做即席查询、各种数据库的操作、管理以及集群的维护. - **互动 Console**: 通过命令行 console,不用编程,执行 SQL 语句就能做即席查询、各种数据库的操作、管理以及集群的维护.
采用 TDengine,可将典型的物联网、车联网、工业互联网大数据平台的总拥有成本大幅降低。表现在几个方面,1:由于其超强性能,它能将系统需要的计算资源和存储资源大幅降低;2:因为采用SQL接口,能与众多第三放软件无缝集成,学习迁移成本大幅下降;3:因为其All In One的特性,系统复杂度降低,能降研发成本;4:因为运维维护简单,运营维护成本能大幅降低。 采用 TDengine,可将典型的物联网、车联网、工业互联网大数据平台的总拥有成本大幅降低。表现在几个方面:
1. 由于其超强性能,它能将系统需要的计算资源和存储资源大幅降低
2. 因为采用 SQL 接口,能与众多第三放软件无缝集成,学习迁移成本大幅下降
3. 因为其 All In One 的特性,系统复杂度降低,能降研发成本
4. 因为运维维护简单,运营维护成本能大幅降低
## TDengine 技术生态
在整个时序大数据平台中,TDengine 在其中扮演的角色如下:
<figure>
在整个时序大数据平台中,TDengine在其中扮演的角色如下:
![TDengine技术生态图](eco_system.png) ![TDengine技术生态图](eco_system.png)
</figure>
<center>图 1. TDengine技术生态图</center> <center>图 1. TDengine技术生态图</center>
上图中,左侧是各种数据采集或消息队列,包括 OPC-UA、MQTT、Telegraf、也包括 Kafaka, 他们的数据将被源源不断的写入到 TDengine。右侧则是可视化、BI 工具、组态软件、应用程序。下侧则是 TDengine 自身提供的命令行程序 (CLI) 以及可视化管理管理。
## TDengine 总体适用场景 ## TDengine 总体适用场景
作为一个高性能、分布式、支持 SQL 的时序数据库,TDengine 的典型适用场景包括但不限于 IoT、工业互联网、车联网、IT运维、能源、金融证券等领域。需要指出的是,TDengine是针对时序数据场景设计的专用数据库和专用大数据处理工具,因充分利用了时序大数据的特点,它无法用来处理网络爬虫、微博、微信、电商、ERP、CRM 等通用型数据。本文对适用场景做更多详细的分析。 作为一个高性能、分布式、支持 SQL 的时序数据库,TDengine 的典型适用场景包括但不限于 IoT、工业互联网、车联网、IT 运维、能源、金融证券等领域。需要指出的是,TDengine 是针对时序数据场景设计的专用数据库和专用大数据处理工具,因充分利用了时序大数据的特点,它无法用来处理网络爬虫、微博、微信、电商、ERP、CRM 等通用型数据。本文对适用场景做更多详细的分析。
### 数据源特点和需求 ### 数据源特点和需求
......
...@@ -2,175 +2,172 @@ ...@@ -2,175 +2,172 @@
title: 数据模型和基本概念 title: 数据模型和基本概念
--- ---
## 物联网典型场景 为了便于解释基本概念,便于撰写示例程序,整个 TDengine 文档以智能电表作为典型时序数据场景。假设每个智能电表采集电流、电压、相位三个量,有多个智能电表,每个电表有位置 location 和分组 group ID 的静态属性. 其采集的数据类似如下的表格:
在典型的物联网、车联网、运维监测场景中,往往有多种不同类型的数据采集设备或采集点,采集一个到多个不同的物理量。而同一种采集设备类型,往往又有多个具体的采集设备分布在不同的地点。采集的物理量都带有时间标签,而且数据量随时间不断增长,但每个数据采集设备或采集点还有自身的静态属性。对于同一类设备,其采集的数据以及静态属性都是很规则的。以智能电表为例,假设每个智能电表采集电流、电压、相位三个量,其采集的数据类似如下的表格:
<div className="center-table">
<table> <table>
<thead><tr> <thead><tr>
<th >设备ID</th> <th>Device ID</th>
<th >时间戳</th> <th>Time Stamp</th>
<th colspan="3">采集量</th> <th colSpan="3">Collected Metrics</th>
<th colspan="2">标签</th> <th colSpan="2">Tags</th>
</tr> </tr>
<tr> <tr>
<th >Device ID</th> <th>Device ID</th>
<th >Time Stamp</th> <th>Time Stamp</th>
<th >current</th> <th>current</th>
<th >voltage</th> <th>voltage</th>
<th >phase</th> <th>phase</th>
<th >location</th> <th>location</th>
<th >groupId</th> <th>groupId</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<tr> <tr>
<td >d1001</td> <td>d1001</td>
<td >1538548685000</td> <td>1538548685000</td>
<td >10.3</td> <td>10.3</td>
<td >219</td> <td>219</td>
<td >0.31</td> <td>0.31</td>
<td >Beijing.Chaoyang</td> <td>Beijing.Chaoyang</td>
<td >2</td> <td>2</td>
</tr> </tr>
<tr> <tr>
<td >d1002</td> <td>d1002</td>
<td >1538548684000</td> <td>1538548684000</td>
<td >10.2</td> <td>10.2</td>
<td >220</td> <td>220</td>
<td >0.23</td> <td>0.23</td>
<td >Beijing.Chaoyang</td> <td>Beijing.Chaoyang</td>
<td >3</td> <td>3</td>
</tr> </tr>
<tr> <tr>
<td >d1003</td> <td>d1003</td>
<td >1538548686500</td> <td>1538548686500</td>
<td >11.5</td> <td>11.5</td>
<td >221</td> <td>221</td>
<td >0.35</td> <td>0.35</td>
<td >Beijing.Haidian</td> <td>Beijing.Haidian</td>
<td >3</td> <td>3</td>
</tr> </tr>
<tr> <tr>
<td >d1004</td> <td>d1004</td>
<td >1538548685500</td> <td>1538548685500</td>
<td >13.4</td> <td>13.4</td>
<td >223</td> <td>223</td>
<td >0.29</td> <td>0.29</td>
<td >Beijing.Haidian</td> <td>Beijing.Haidian</td>
<td >2</td> <td>2</td>
</tr> </tr>
<tr> <tr>
<td >d1001</td> <td>d1001</td>
<td >1538548695000</td> <td>1538548695000</td>
<td >12.6</td> <td>12.6</td>
<td >218</td> <td>218</td>
<td >0.33</td> <td>0.33</td>
<td >Beijing.Chaoyang</td> <td>Beijing.Chaoyang</td>
<td >2</td> <td>2</td>
</tr> </tr>
<tr> <tr>
<td >d1004</td> <td>d1004</td>
<td >1538548696600</td> <td>1538548696600</td>
<td >11.8</td> <td>11.8</td>
<td >221</td> <td>221</td>
<td >0.28</td> <td>0.28</td>
<td >Beijing.Haidian</td> <td>Beijing.Haidian</td>
<td >2</td> <td>2</td>
</tr> </tr>
<tr> <tr>
<td >d1002</td> <td>d1002</td>
<td >1538548696650</td> <td>1538548696650</td>
<td >10.3</td> <td>10.3</td>
<td >218</td> <td>218</td>
<td >0.25</td> <td>0.25</td>
<td >Beijing.Chaoyang</td> <td>Beijing.Chaoyang</td>
<td >3</td> <td>3</td>
</tr> </tr>
<tr> <tr>
<td >d1001</td> <td>d1001</td>
<td >1538548696800</td> <td>1538548696800</td>
<td >12.3</td> <td>12.3</td>
<td >221</td> <td>221</td>
<td >0.31</td> <td>0.31</td>
<td >Beijing.Chaoyang</td> <td>Beijing.Chaoyang</td>
<td >2</td> <td>2</td>
</tr> </tr>
</tbody> </tbody>
</table> </table>
<a href="#model_table1">表 1:智能电表数据示例</a>
</div>
<center> <a href="#model_table1">表 1:智能电表数据示例</a></center> 每一条记录都有设备 ID,时间戳,采集的物理量以及每个设备相关的静态标签。每个设备是受外界的触发,或按照设定的周期采集数据。采集的数据点是时序的,是一个数据流。
每一条记录都有设备 ID,时间戳,采集的物理量(如上图中的电流、电压、相位),还有与每个设备相关的静态标签(如上述表 1 中的位置 location 和分组 groupId)。每个设备是受外界的触发,或按照设定的周期采集数据。采集的数据点是时序的,是一个数据流。
## 数据特征
除时序特征外,仔细研究发现,物联网、车联网、运维监测类数据及其应用还具有很多其他明显的特征。
1. 数据是结构化的;
2. 数据极少有更新或删除操作;
3. 无需传统数据库的事务处理;
4. 相对互联网应用,写多读少;
5. 流量平稳,根据设备数量和采集频次,可以预测出来;
6. 用户关注的是一段时间的趋势,而不是某一特点时间点的值;
7. 数据是有保留期限的;
8. 数据的查询分析一定是基于时间段和地理区域的;
9. 系统需要各种实时计算和统计操作,包括降采样、插值等特种操作;
10. 数据量巨大,一天采集的数据就可以超过 100 亿条。
充分利用上述特征,TDengine 采取了一特殊的优化的存储和计算设计来处理时序数据,能将系统处理能力显著提高。
## 采集量(Metric)
采集量是指传感器、设备或其他类型采集点采集的物理量,比如电流、电压、温度、压力、GPS位置等,是随时间变化的,数据类型可以是整型、浮点型、布尔型,也可是字符串。随着时间的推移,存储的采集量的数据量越来越大。
## 标签(Label/Tag) ## 采集量 (Metric)
标签是指传感器、设备或其他类型采集点的静态属性,不是随时间变化的,比如设备型号、颜色、设备的所在地等,数据类型可以是任何类型。虽然是静态的,但TDengine容许用户修改、删除或增加标签值。与采集量不一样的是,随时间的推移,存储的标签的数据量不会有什么变化 采集量是指传感器、设备或其他类型采集点采集的物理量,比如电流、电压、温度、压力、GPS 位置等,是随时间变化的,数据类型可以是整型、浮点型、布尔型,也可是字符串。随着时间的推移,存储的采集量的数据量越来越大
## 数据采集点(Data Colletion Point) ## 标签 (Label/Tag)
数据采集点是指按照预设时间周期或受事件触发采集物理量的硬件或软件。一个数据采集点可以采集一个或多个物理量,**但这些物理量都是同一时刻采集的,具有相同的时间戳**。对于复杂的设备,往往有多个数据采集点,每个采集点采集的周期都可能不一样,而且完全独立,不同步。比如对于一台汽车,有数据采集点专门采集GPS位置,有数据采集点专门采集发动机状态,有数据采集点专门采集车内的环境,这样一台汽车就有三个数据采集点 标签是指传感器、设备或其他类型采集点的静态属性,不是随时间变化的,比如设备型号、颜色、设备的所在地等,数据类型可以是任何类型。虽然是静态的,但 TDengine 容许用户修改、删除或增加标签值。与采集量不一样的是,随时间的推移,存储的标签的数据量不会有什么变化
## 关系型数据库模型 ## 数据采集点 (Data Collection Point)
因为采集的数据一般是结构化数据,同时为降低学习门槛,TDengine 采用传统的关系型数据库模型管理数据。用户需要先创建库,然后创建表,之后才能插入或查询数据。TDengine 采用的是结构化存储,而不是 NoSQL 的 key-value 存储 数据采集点是指按照预设时间周期或受事件触发采集物理量的硬件或软件。一个数据采集点可以采集一个或多个采集量,**但这些采集量都是同一时刻采集的,具有相同的时间戳**。对于复杂的设备,往往有多个数据采集点,每个数据采集点采集的周期都可能不一样,而且完全独立,不同步。比如对于一台汽车,有数据采集点专门采集 GPS 位置,有数据采集点专门采集发动机状态,有数据采集点专门采集车内的环境,这样一台汽车就有三个数据采集点
与通用数据库相比,TDengine通过一个数据采集点一张表,来大幅提高单个数据采集点的插入和查询速度。同时TDengine引入了超级表的概念,让每个表都带有标签,解决多个数据采集点之间的聚合分析问题。 ## 表 (Table)
## 一个数据采集点一张表 因为采集量一般是结构化数据,同时为降低学习门槛,TDengine 采用传统的关系型数据库模型管理数据。用户需要先创建库,然后创建表,之后才能插入或查询数据。
为充分利用其数据的时序性和其他数据特点,TDengine 要求**对每个数据采集点单独建表**(比如有一千万个智能电表,就需创建一千万张表,上述表格中的 d1001,d1002,d1003,d1004 都需单独建表),用来存储这个采集点所采集的时序数据。这种设计有几大优点: 为充分利用其数据的时序性和其他数据特点,TDengine 采取**一个数据采集点一张表**的策略,要求对每个数据采集点单独建表(比如有一千万个智能电表,就需创建一千万张表,上述表格中的 d1001,d1002,d1003,d1004 都需单独建表),用来存储这个数据采集点所采集的时序数据。这种设计有几大优点:
1. 由于不同采集设备产生数据的过程完全独立,每个设备的数据源是唯一的,一张表也就只有一个写入者,这样就可采用无锁方式来写,写入速度就能大幅提升。 1. 由于不同数据采集点产生数据的过程完全独立,每个数据采集点的数据源是唯一的,一张表也就只有一个写入者,这样就可采用无锁方式来写,写入速度就能大幅提升。
2. 对于一个数据采集点而言,其产生的数据是按照时间排序的,因此写的操作可用追加的方式实现,进一步大幅提高数据写入速度。 2. 对于一个数据采集点而言,其产生的数据是按照时间排序的,因此写的操作可用追加的方式实现,进一步大幅提高数据写入速度。
3. 一个数据采集点的数据是以块为单位连续存储的。如果读取一个时间段的数据,它能大幅减少随机读取操作,成数量级的提升读取和查询速度。 3. 一个数据采集点的数据是以块为单位连续存储的。如果读取一个时间段的数据,它能大幅减少随机读取操作,成数量级的提升读取和查询速度。
4. 一个数据块内部,采用列式存储,对于不同数据类型,采用不同压缩算法,而且由于一个数据采集点的采集量的变化是缓慢的,压缩率更高。 4. 一个数据块内部,采用列式存储,对于不同数据类型,采用不同压缩算法,而且由于一个数据采集点的采集量的变化是缓慢的,压缩率更高。
如果采用传统的方式,将多个设备的数据写入一张表,由于网络延时不可控,不同设备的数据到达服务器的时序是无法保证的,写入操作是要有锁保护的,而且一个设备的数据是难以保证连续存储在一起的。**采用一个数据采集点一张表的方式,能最大程度的保证单个数据采集点的插入和查询的性能是最优的。** 如果采用传统的方式,将多个数据采集点的数据写入一张表,由于网络延时不可控,不同数据采集点的数据到达服务器的时序是无法保证的,写入操作是要有锁保护的,而且一个数据采集点的数据是难以保证连续存储在一起的。**采用一个数据采集点一张表的方式,能最大程度的保证单个数据采集点的插入和查询的性能是最优的。**
TDengine 建议用数据采集点的名字(如上表中的 D1001)来做表名。每个数据采集点可能同时采集多个物理量(如上表中的 curent,voltage,phase),每个物理量对应一张表中的一列,数据类型可以是整型、浮点型、字符串等。除此之外,表的第一列必须是时间戳,即数据类型为 timestamp。对采集的数据,TDengine 将自动按照时间戳建立索引,但对采集的物理量不建任何索引。数据用列式存储方式保存。 TDengine 建议用数据采集点的名字(如上表中的 D1001)来做表名。每个数据采集点可能同时采集多个采集量(如上表中的 curent,voltage,phase),每个采集量对应一张表中的一列,数据类型可以是整型、浮点型、字符串等。除此之外,表的第一列必须是时间戳,即数据类型为 timestamp。对采集量,TDengine 将自动按照时间戳建立索引,但对采集量本身不建任何索引。数据用列式存储方式保存。
对于复杂的设备,比如汽车,它有多个数据采集点,那么就需要为一台汽车建立多张表。 对于复杂的设备,比如汽车,它有多个数据采集点,那么就需要为一台汽车建立多张表。
## 超级表:同一类型数据采集点的集合 ## 超级表 (STable)
由于一个数据采集点一张表,导致表的数量巨增,难以管理,而且应用经常需要做采集点之间的聚合操作,聚合的操作也变得复杂起来。为解决这个问题,TDengine 引入超级表(Super Table,简称为 STable)的概念。 由于一个数据采集点一张表,导致表的数量巨增,难以管理,而且应用经常需要做采集点之间的聚合操作,聚合的操作也变得复杂起来。为解决这个问题,TDengine 引入超级表(Super Table,简称为 STable)的概念。
超级表是指某一特定类型的数据采集点的集合。同一类型的数据采集点,其表的结构是完全一样的,但每个表(数据采集点)的静态属性(标签)是不一样的。描述一个超级表(某一特定类型的数据采集点的集合),除需要定义采集量的表结构之外,还需要定义其标签的 schema,标签的数据类型可以是整数、浮点数、字符串,标签可以有多个,可以事后增加、删除或修改。如果整个系统有 N 个不同类型的数据采集点,就需要建立 N 个超级表。 超级表是指某一特定类型的数据采集点的集合。同一类型的数据采集点,其表的结构是完全一样的,但每个表(数据采集点)的静态属性(标签)是不一样的。描述一个超级表(某一特定类型的数据采集点的集合),除需要定义采集量的表结构之外,还需要定义其标签的 schema,标签的数据类型可以是整数、浮点数、字符串,标签可以有多个,可以事后增加、删除或修改。如果整个系统有 N 个不同类型的数据采集点,就需要建立 N 个超级表。
在 TDengine 的设计里,**表用来代表一个具体的数据采集点,超级表用来代表一组相同类型的数据采集点集合**。当为某个具体数据采集点创建表时,用户使用超级表的定义做模板,同时指定该具体采集点(表)的标签值。与传统的关系型数据库相比,表(一个数据采集点)是带有静态标签的,而且这些标签可以事后增加、删除、修改。超级表与与基于超级表建立的子表之间的关系表现在: 在 TDengine 的设计里,**表用来代表一个具体的数据采集点,超级表用来代表一组相同类型的数据采集点集合**
## 子表 (Subtable)
当为某个具体数据采集点创建表时,用户可以使用超级表的定义做模板,同时指定该具体采集点(表)的具体标签值来创建该表。**通过超级表创建的表称之为子表**。正常的表与子表的差异在于:
1. 子表就是表,因此所有正常表的SQL操作都可以在子表上执行。
2. 子表在正常表的基础上有扩展,它是带有静态标签的,而且这些标签可以事后增加、删除、修改,而正常的表没有。
3. 子表一定属于一张超级表,但普通表不属于任何超级表
4. 普通表无法转为子表,子表也无法转为普通表。
超级表与与基于超级表建立的子表之间的关系表现在:
1. 一张超级表包含有多张子表,这些子表具有相同的采集量 schema,但带有不同的标签值。 1. 一张超级表包含有多张子表,这些子表具有相同的采集量 schema,但带有不同的标签值。
2. 不能通过子表调整数据或标签的模式,对于超级表的数据模式修改立即对所有的子表生效。 2. 不能通过子表调整数据或标签的模式,对于超级表的数据模式修改立即对所有的子表生效。
3. 超级表只定义一个模板,自身不存储任何数据或标签信息。因此,不能向一个超级表写入数据,只能将数据写入子表中。 3. 超级表只定义一个模板,自身不存储任何数据或标签信息。因此,不能向一个超级表写入数据,只能将数据写入子表中。
针对超级表的查询,TDengine将把所有子表中的数据视为一个整体数据集进行处理,会先把满足标签过滤条件的表从超级表中找出来,然后再扫描这些表的时序数据,进行聚合操作,这样需要扫描的数据集会大幅减少,从而显著提高查询的性能。本质上,TDengine通过对超级表查询的支持,实现了多个同类数据采集点的高效聚合。 查询既可以在表上进行,也可以在超级表上进行。针对超级表的查询,TDengine 将把所有子表中的数据视为一个整体数据集进行处理,会先把满足标签过滤条件的表从超级表中找出来,然后再扫描这些表的时序数据,进行聚合操作,这样需要扫描的数据集会大幅减少,从而显著提高查询的性能。本质上,TDengine 通过对超级表查询的支持,实现了多个同类数据采集点的高效聚合。
TDengine系统建议给一个数据采集点建表,需要通过超级表建表,而不是建普通表。
## 库 (database)
库是指一组表的集合。TDengine 容许一个运行实例有多个库,而且每个库可以配置不同的存储策略。不同类型的数据采集点往往具有不同的数据特征,包括数据采集频率的高低,数据保留时间的长短,副本的数目,数据块的大小,是否允许更新数据等等。为了在各种场景下 TDengine 都能最大效率的工作,TDengine 建议将不同数据特征的超级表创建在不同的库里。
一个库里,可以有一到多个超级表,但一个超级表只属于一个库。一个超级表所拥有的子表全部存在一个库里。
## FQDN & End Point ## FQDN & End Point
FQDN (fully qualified domain name, 完全限定域名)是 Internet 上特定计算机或主机的完整域名。FQDN由两部分组成:主机名和域名。例如,假设邮件服务器的FQDN可能是mail.tdengine.com。主机名是mail,主机位于域名tdengine.com中。DNS(Domain Name System),负责将FQDN翻译成IP,是互联网应用的寻址方式。对于没有DNS的系统,可以通过配置hosts文件来解决。 FQDN (fully qualified domain name, 完全限定域名)是 Internet 上特定计算机或主机的完整域名。FQDN 由两部分组成:主机名和域名。例如,假设邮件服务器的 FQDN 可能是 mail.tdengine.com。主机名是 mail,主机位于域名 tdengine.com 中。DNS(Domain Name System),负责将 FQDN 翻译成 IP,是互联网应用的寻址方式。对于没有 DNS 的系统,可以通过配置 hosts 文件来解决。
TDengine集群的每个节点是由End Point来唯一标识的,End Point是由 FQDN 外加 Port 组成,比如 h1.tdengine.com:6030。这样当IP发生变化的时候,我们依然可以使用 FQDN 来动态找到节点,不需要更改集群的任何配置。而且采用 FQDN,便于内网和外网对同一个集群的统一访问。 TDengine 集群的每个节点是由 End Point 来唯一标识的,End Point 是由 FQDN 外加 Port 组成,比如 h1.tdengine.com:6030。这样当 IP 发生变化的时候,我们依然可以使用 FQDN 来动态找到节点,不需要更改集群的任何配置。而且采用 FQDN,便于内网和外网对同一个集群的统一访问。
TDengine 不建议采用直接的IP地址访问集群,不利于管理。不了解 FQDN 概念,请看博文[《一篇文章说清楚 TDengine 的 FQDN》](https://www.taosdata.com/blog/2020/09/11/1824.html) TDengine 不建议采用直接的 IP 地址访问集群,不利于管理。不了解 FQDN 概念,请看博文[《一篇文章说清楚 TDengine 的 FQDN》](https://www.taosdata.com/blog/2020/09/11/1824.html)
```csharp title="原生连接" ```csharp title="原生连接"
{{#include docs-examples/csharp/ConnectExample.cs}} {{#include docs-examples/csharp/ConnectExample.cs}}
``` ```
:::info :::info
C# 连接器目前只支持原生连接。 C# 连接器目前只支持原生连接。
::: :::
\ No newline at end of file
#### 使用数据库访问统一接口 #### 使用数据库访问统一接口
```go title="原生连接" ```go title="原生连接"
{{#include docs-examples/go/connect/cgoexample/main.go}} {{#include docs-examples/go/connect/cgoexample/main.go}}
``` ```
```go title="REST 连接" ```go title="REST 连接"
{{#include docs-examples/go/connect/restexample/main.go}} {{#include docs-examples/go/connect/restexample/main.go}}
``` ```
#### 使用高级封装 #### 使用高级封装
也可以使用 driver-go 的 af 包建立连接。这个模块封装了 TDengine 的高级功能, 如:参数绑定、订阅等。 也可以使用 driver-go 的 af 包建立连接。这个模块封装了 TDengine 的高级功能, 如:参数绑定、订阅等。
```go title="使用 af 包建立原生连接" ```go title="使用 af 包建立原生连接"
{{#include docs-examples/go/connect/afconn/main.go}} {{#include docs-examples/go/connect/afconn/main.go}}
``` ```
```java title="原生连接" ```java title="原生连接"
{{#include docs-examples/java/src/main/java/com/taos/example/JNIConnectExample.java}} {{#include docs-examples/java/src/main/java/com/taos/example/JNIConnectExample.java}}
``` ```
```java title="REST 连接" ```java title="REST 连接"
{{#include docs-examples/java/src/main/java/com/taos/example/RESTConnectExample.java:main}} {{#include docs-examples/java/src/main/java/com/taos/example/RESTConnectExample.java:main}}
``` ```
使用REST 连接时,如果查询数据量比较大,还可开启批量拉取功能。
使用 REST 连接时,如果查询数据量比较大,还可开启批量拉取功能。
```java title="开启批量拉取功能" {4} ```java title="开启批量拉取功能" {4}
{{#include docs-examples/java/src/main/java/com/taos/example/WSConnectExample.java:main}} {{#include docs-examples/java/src/main/java/com/taos/example/WSConnectExample.java:main}}
``` ```
更多连接参数配置,参考[Java 连接器](/reference/connector/java)
\ No newline at end of file 更多连接参数配置,参考[Java 连接器](/reference/connector/java)
```js title="原生连接" ```js title="原生连接"
{{#include docs-examples/node/nativeexample/connect.js}} {{#include docs-examples/node/nativeexample/connect.js}}
``` ```
```js title="REST 连接" ```js title="REST 连接"
{{#include docs-examples/node/restexample/connect.js}} {{#include docs-examples/node/restexample/connect.js}}
``` ```
\ No newline at end of file
```php title="原生连接"
{{#include docs-examples/php/connect.php}}
```
```python title="原生连接" ```python title="原生连接"
{{#include docs-examples/python/connect_exmaple.py}} {{#include docs-examples/python/connect_exmaple.py}}
``` ```
\ No newline at end of file
```r title="原生连接" ```r title="原生连接"
{{#include docs-examples/R/connect_native.r:demo}} {{#include docs-examples/R/connect_native.r:demo}}
``` ```
\ No newline at end of file
```rust title="原生连接/REST 连接" ```rust title="原生连接/REST 连接"
{{#include docs-examples/rust/nativeexample/examples/connect.rs}} {{#include docs-examples/rust/nativeexample/examples/connect.rs}}
``` ```
:::note :::note
对于 Rust 连接器, 连接方式的不同只体现在使用的特性不同。如果启用了 "rest" 特性,那么只有 RESTful 的实现会被编译进来。 对于 Rust 连接器, 连接方式的不同只体现在使用的特性不同。如果启用了 "rest" 特性,那么只有 RESTful 的实现会被编译进来。
......
...@@ -13,18 +13,19 @@ import ConnPythonNative from "./_connect_python.mdx"; ...@@ -13,18 +13,19 @@ import ConnPythonNative from "./_connect_python.mdx";
import ConnCSNative from "./_connect_cs.mdx"; import ConnCSNative from "./_connect_cs.mdx";
import ConnC from "./_connect_c.mdx"; import ConnC from "./_connect_c.mdx";
import ConnR from "./_connect_r.mdx"; import ConnR from "./_connect_r.mdx";
import ConnPHP from "./_connect_php.mdx";
import InstallOnWindows from "../../14-reference/03-connector/_linux_install.mdx"; import InstallOnWindows from "../../14-reference/03-connector/_linux_install.mdx";
import InstallOnLinux from "../../14-reference/03-connector/_windows_install.mdx"; import InstallOnLinux from "../../14-reference/03-connector/_windows_install.mdx";
import VerifyLinux from "../../14-reference/03-connector/_verify_linux.mdx"; import VerifyLinux from "../../14-reference/03-connector/_verify_linux.mdx";
import VerifyWindows from "../../14-reference/03-connector/_verify_windows.mdx"; import VerifyWindows from "../../14-reference/03-connector/_verify_windows.mdx";
TDengine 提供 REST API,容许在任何平台的任何应用程序通过它访问 TDengine 运行实例,详细介绍请看 [REST API](/reference/rest-api/)。除 REST API 之外,TDengine 还提供多种编程语言的连接器方便用户开发应用程序,其中包括 C/C++、Java、Python、Go、Node.js、C# 等。 本节介绍如何使用连接器建立与 TDengine 的连接,给出连接器安装、连接的简单说明。关于各连接器的详细功能说明,请查看[连接器](https://docs.taosdata.com/reference/connector/) TDengine 提供了丰富的应用程序开发接口,为了便于用户快速开发自己的应用,TDengine 支持了多种编程语言的连接器,其中官方连接器包括支持 C/C++、Java、Python、Go、Node.js、C#、Rust、Lua(社区贡献)和 PHP (社区贡献)的连接器。这些连接器支持使用原生接口(taosc)和 REST 接口(部分语言暂不支持)连接 TDengine 集群。社区开发者也贡献了多个非官方连接器,例如 ADO.NET 连接器、Lua 连接器和 PHP 连接器。
## 连接器建立连接的方式 ## 连接器建立连接的方式
连接器建立连接的方式,TDengine 提供两种: 连接器建立连接的方式,TDengine 提供两种:
1. 通过 taosAdapter 组件提供的 REST API 建立与 taosd 的连接,这种连接方式下文中简称"REST 连接“ 1. 通过 taosAdapter 组件提供的 REST API 建立与 taosd 的连接,这种连接方式下文中简称“REST 连接”
2. 通过客户端驱动程序 taosc 直接与服务端程序 taosd 建立连接,这种连接方式下文中简称“原生连接”。 2. 通过客户端驱动程序 taosc 直接与服务端程序 taosd 建立连接,这种连接方式下文中简称“原生连接”。
无论使用何种方式建立连接,连接器都提供了相同或相似的 API 操作数据库,都可以执行 SQL 语句,只是初始化连接的方式稍有不同,用户在使用上不会感到什么差别。 无论使用何种方式建立连接,连接器都提供了相同或相似的 API 操作数据库,都可以执行 SQL 语句,只是初始化连接的方式稍有不同,用户在使用上不会感到什么差别。
...@@ -200,6 +201,46 @@ install.packages("RJDBC") ...@@ -200,6 +201,46 @@ install.packages("RJDBC")
如果已经安装了 TDengine 服务端软件或 TDengine 客户端驱动 taosc, 那么已经安装了 C 连接器,无需额外操作。 如果已经安装了 TDengine 服务端软件或 TDengine 客户端驱动 taosc, 那么已经安装了 C 连接器,无需额外操作。
<br/> <br/>
</TabItem>
<TabItem label="PHP" value="php">
**下载代码并解压:**
```shell
curl -L -o php-tdengine.tar.gz https://github.com/Yurunsoft/php-tdengine/archive/refs/tags/v1.0.2.tar.gz \
&& mkdir php-tdengine \
&& tar -xzf php-tdengine.tar.gz -C php-tdengine --strip-components=1
```
> 版本 `v1.0.0` 可替换为任意更新的版本,可在 Release 中查看最新版本。
**非 Swoole 环境:**
```shell
phpize && ./configure && make -j && make install
```
**手动指定 tdengine 目录:**
```shell
phpize && ./configure --with-tdengine-dir=/usr/local/Cellar/tdengine/2.4.0.0 && make -j && make install
```
> `--with-tdengine-dir=` 后跟上 tdengine 目录。
> 适用于默认找不到的情况,或者 MacOS 系统用户。
**Swoole 环境:**
```shell
phpize && ./configure --enable-swoole && make -j && make install
```
**启用扩展:**
方法一:在 `php.ini` 中加入 `extension=tdengine`
方法二:运行带参数 `php -dextension=tdengine test.php
</TabItem> </TabItem>
</Tabs> </Tabs>
...@@ -232,6 +273,9 @@ install.packages("RJDBC") ...@@ -232,6 +273,9 @@ install.packages("RJDBC")
<TabItem label="C" value="c"> <TabItem label="C" value="c">
<ConnC /> <ConnC />
</TabItem> </TabItem>
<TabItem label="PHP" value="php">
<ConnPHP />
</TabItem>
</Tabs> </Tabs>
:::tip :::tip
......
...@@ -18,6 +18,8 @@ import CsSQL from "./_cs_sql.mdx"; ...@@ -18,6 +18,8 @@ import CsSQL from "./_cs_sql.mdx";
import CsStmt from "./_cs_stmt.mdx"; import CsStmt from "./_cs_stmt.mdx";
import CSQL from "./_c_sql.mdx"; import CSQL from "./_c_sql.mdx";
import CStmt from "./_c_stmt.mdx"; import CStmt from "./_c_stmt.mdx";
import PhpSQL from "./_php_sql.mdx";
import PhpStmt from "./_php_stmt.mdx";
## SQL 写入简介 ## SQL 写入简介
...@@ -88,6 +90,9 @@ INSERT INTO d1001 VALUES (1538548685000, 10.3, 219, 0.31) (1538548695000, 12.6, ...@@ -88,6 +90,9 @@ INSERT INTO d1001 VALUES (1538548685000, 10.3, 219, 0.31) (1538548695000, 12.6,
<TabItem label="C" value="c"> <TabItem label="C" value="c">
<CSQL /> <CSQL />
</TabItem> </TabItem>
<TabItem label="PHP" value="php">
<PhpSQL />
</TabItem>
</Tabs> </Tabs>
:::note :::note
...@@ -125,5 +130,8 @@ TDengine 也提供了支持参数绑定的 Prepare API,与 MySQL 类似,这 ...@@ -125,5 +130,8 @@ TDengine 也提供了支持参数绑定的 Prepare API,与 MySQL 类似,这
<TabItem label="C" value="c"> <TabItem label="C" value="c">
<CStmt /> <CStmt />
</TabItem> </TabItem>
<TabItem label="PHP" value="php">
<PhpStmt />
</TabItem>
</Tabs> </Tabs>
label: 写入数据 label: 写入数据
link: \ No newline at end of file
type: generated-index
slug: /insert-data/
description: "TDengine 支持多种写入协议,包括 SQL,InfluxDB Line 协议, OpenTSDB Telnet 协议,OpenTSDB JSON 格式协议。数据可以单条插入,也可以批量插入,可以插入一个数据采集点的数据,也可以同时插入多个数据采集点的数据。同时,TDengine 支持多线程插入,支持时间乱序数据插入,也支持历史数据插入。InfluxDB Line 协议、OpenTSDB Telnet 协议和 OpenTSDB JSON 格式协议是 TDengine 支持的三种无模式写入协议。使用无模式方式写入无需提前创建超级表和子表,并且引擎能自适用数据对表结构做调整。"
```php
{{#include docs-examples/php/insert.php}}
```
```php
{{#include docs-examples/php/insert_stmt.php}}
```
---
title: 写入数据
---
TDengine 支持多种写入协议,包括 SQL,InfluxDB Line 协议, OpenTSDB Telnet 协议,OpenTSDB JSON 格式协议。数据可以单条插入,也可以批量插入,可以插入一个数据采集点的数据,也可以同时插入多个数据采集点的数据。同时,TDengine 支持多线程插入,支持时间乱序数据插入,也支持历史数据插入。InfluxDB Line 协议、OpenTSDB Telnet 协议和 OpenTSDB JSON 格式协议是 TDengine 支持的三种无模式写入协议。使用无模式方式写入无需提前创建超级表和子表,并且引擎能自适用数据对表结构做调整。
```mdx-code-block
import DocCardList from '@theme/DocCardList';
import {useCurrentSidebarCategory} from '@docusaurus/theme-common';
<DocCardList items={useCurrentSidebarCategory().items}/>
```
\ No newline at end of file
```go
{{#include docs-examples/php/query.php}}
```
...@@ -13,6 +13,7 @@ import RustQuery from "./_rust.mdx"; ...@@ -13,6 +13,7 @@ import RustQuery from "./_rust.mdx";
import NodeQuery from "./_js.mdx"; import NodeQuery from "./_js.mdx";
import CsQuery from "./_cs.mdx"; import CsQuery from "./_cs.mdx";
import CQuery from "./_c.mdx"; import CQuery from "./_c.mdx";
import PhpQuery from "./_php.mdx";
import PyAsync from "./_py_async.mdx"; import PyAsync from "./_py_async.mdx";
import NodeAsync from "./_js_async.mdx"; import NodeAsync from "./_js_async.mdx";
import CsAsync from "./_cs_async.mdx"; import CsAsync from "./_cs_async.mdx";
...@@ -150,12 +151,15 @@ Query OK, 5 row(s) in set (0.001521s) ...@@ -150,12 +151,15 @@ Query OK, 5 row(s) in set (0.001521s)
<TabItem label="C" value="c"> <TabItem label="C" value="c">
<CQuery /> <CQuery />
</TabItem> </TabItem>
<TabItem label="PHP" value="php">
<PhpQuery />
</TabItem>
</Tabs> </Tabs>
:::note :::note
1. 无论是使用 REST 连接还是原生连接的连接器,以上示例代码都能正常工作。 1. 无论是使用 REST 连接还是原生连接的连接器,以上示例代码都能正常工作。
2. 唯一需要注意的是:由于 RESTful 接口无状态, 不能使用 `use db` 语句来切换数据库。 2. 唯一需要注意的是:由于 REST 接口无状态, 不能使用 `use db` 语句来切换数据库。
::: :::
......
label: 开发指南 label: 开发指南
link: \ No newline at end of file
type: generated-index
slug: /develop
description: "开始指南是对开发者友好的使用教程,既包括数据建模、写入、查询等基础功能的使用,也包括数据订阅、连续查询等高级功能的使用。对于每个主题,都配有各编程语言的连接器的示例代码,方便开发者快速上手。如果想更深入地了解各连接器的使用,请阅读连接器参考指南。"
---
title: 开发指南
---
开始指南是对开发者友好的使用教程,既包括数据建模、写入、查询等基础功能的使用,也包括数据订阅、连续查询等高级功能的使用。对于每个主题,都配有各编程语言的连接器的示例代码,方便开发者快速上手。如果想更深入地了解各连接器的使用,请阅读连接器参考指南。
```mdx-code-block
import DocCardList from '@theme/DocCardList';
import {useCurrentSidebarCategory} from '@docusaurus/theme-common';
<DocCardList items={useCurrentSidebarCategory().items}/>
```
\ No newline at end of file
label: 集群管理 label: 集群管理
link:
type: generated-index
slug: /cluster/
description: "TDengine支持以集群方式部署,以提升系统的处理能力和高可用性。TDengine集群支持任意数据的多副本从而提升高可用性,并自动实现负载均衡。同时TDengine集群具有很好的横向扩展能力以处理更多的数据采集点和更大的数据量。"
keywords:
[
集群,
高可用,
负载均衡,
横向扩展
]
---
title: 集群管理
---
TDengine支持以集群方式部署,以提升系统的处理能力和高可用性。TDengine集群支持任意数据的多副本从而提升高可用性,并自动实现负载均衡。同时TDengine集群具有很好的横向扩展能力以处理更多的数据采集点和更大的数据量。
```mdx-code-block
import DocCardList from '@theme/DocCardList';
import {useCurrentSidebarCategory} from '@docusaurus/theme-common';
<DocCardList items={useCurrentSidebarCategory().items}/>
```
\ No newline at end of file
...@@ -19,7 +19,23 @@ CREATE DATABASE [IF NOT EXISTS] db_name [KEEP keep] [DAYS days] [UPDATE 1]; ...@@ -19,7 +19,23 @@ CREATE DATABASE [IF NOT EXISTS] db_name [KEEP keep] [DAYS days] [UPDATE 1];
4. 更多关于 UPDATE 参数的用法,请参考[FAQ](/train-faq/faq) 4. 更多关于 UPDATE 参数的用法,请参考[FAQ](/train-faq/faq)
3. 数据库名最大长度为 33; 3. 数据库名最大长度为 33;
4. 一条 SQL 语句的最大长度为 65480 个字符; 4. 一条 SQL 语句的最大长度为 65480 个字符;
5. 数据库还有更多与数据库相关的配置参数,如 cache, blocks, days, keep, minRows, maxRows, wal, fsync, update, cacheLast, replica, quorum, maxVgroupsPerDb, ctime, comp, prec, 具体细节请参见 [配置参数](/reference/config/) 章节。 5. 创建数据库时可用的参数有:
- cache: [Description](/reference/config/#cache)
- blocks: [Description](/reference/config/#blocks)
- days: [Description](/reference/config/#days)
- keep: [Description](/reference/config/#keep)
- minRows: [Description](/reference/config/#minrows)
- maxRows: [Description](/reference/config/#maxrows)
- wal: [Description](/reference/config/#wallevel)
- fsync: [Description](/reference/config/#fsync)
- update: [Description](/reference/config/#update)
- cacheLast: [Description](/reference/config/#cachelast)
- replica: [Description](/reference/config/#replica)
- quorum: [Description](/reference/config/#quorum)
- maxVgroupsPerDb: [Description](/reference/config/#maxvgroupsperdb)
- comp: [Description](/reference/config/#comp)
- precision: [Description](reference/config/#precision)
6. 请注意上面列出的所有参数都可以配置在配置文件 `taosd.cfg` 中作为创建数据库时使用的默认配置, `create database` 的参数中明确指定的会覆盖配置文件中的设置。
::: :::
......
...@@ -915,9 +915,9 @@ SELECT ROUND(field_name) FROM { tb_name | stb_name } [WHERE clause]; ...@@ -915,9 +915,9 @@ SELECT ROUND(field_name) FROM { tb_name | stb_name } [WHERE clause];
### CSUM ### CSUM
```sql ```sql
SELECT CSUM(field_name) FROM { tb_name | stb_name } [WHERE clause] SELECT CSUM(field_name) FROM { tb_name | stb_name } [WHERE clause]
``` ```
**功能说明**:累加和(Cumulative sum),输出行与输入行数相同。 **功能说明**:累加和(Cumulative sum),输出行与输入行数相同。
...@@ -937,9 +937,9 @@ SELECT ROUND(field_name) FROM { tb_name | stb_name } [WHERE clause]; ...@@ -937,9 +937,9 @@ SELECT ROUND(field_name) FROM { tb_name | stb_name } [WHERE clause];
### MAVG ### MAVG
```sql ```sql
SELECT MAVG(field_name, K) FROM { tb_name | stb_name } [WHERE clause] SELECT MAVG(field_name, K) FROM { tb_name | stb_name } [WHERE clause]
``` ```
**功能说明**: 计算连续 k 个值的移动平均数(moving average)。如果输入行数小于 k,则无结果输出。参数 k 的合法输入范围是 1≤ k ≤ 1000。 **功能说明**: 计算连续 k 个值的移动平均数(moving average)。如果输入行数小于 k,则无结果输出。参数 k 的合法输入范围是 1≤ k ≤ 1000。
...@@ -959,9 +959,9 @@ SELECT ROUND(field_name) FROM { tb_name | stb_name } [WHERE clause]; ...@@ -959,9 +959,9 @@ SELECT ROUND(field_name) FROM { tb_name | stb_name } [WHERE clause];
### SAMPLE ### SAMPLE
```sql ```sql
SELECT SAMPLE(field_name, K) FROM { tb_name | stb_name } [WHERE clause] SELECT SAMPLE(field_name, K) FROM { tb_name | stb_name } [WHERE clause]
``` ```
**功能说明**: 获取数据的 k 个采样值。参数 k 的合法输入范围是 1≤ k ≤ 1000。 **功能说明**: 获取数据的 k 个采样值。参数 k 的合法输入范围是 1≤ k ≤ 1000。
...@@ -980,9 +980,9 @@ SELECT ROUND(field_name) FROM { tb_name | stb_name } [WHERE clause]; ...@@ -980,9 +980,9 @@ SELECT ROUND(field_name) FROM { tb_name | stb_name } [WHERE clause];
### ASIN ### ASIN
```sql ```sql
SELECT ASIN(field_name) FROM { tb_name | stb_name } [WHERE clause] SELECT ASIN(field_name) FROM { tb_name | stb_name } [WHERE clause]
``` ```
**功能说明**:获得指定列的反正弦结果 **功能说明**:获得指定列的反正弦结果
...@@ -1000,9 +1000,9 @@ SELECT ROUND(field_name) FROM { tb_name | stb_name } [WHERE clause]; ...@@ -1000,9 +1000,9 @@ SELECT ROUND(field_name) FROM { tb_name | stb_name } [WHERE clause];
### ACOS ### ACOS
```sql ```sql
SELECT ACOS(field_name) FROM { tb_name | stb_name } [WHERE clause] SELECT ACOS(field_name) FROM { tb_name | stb_name } [WHERE clause]
``` ```
**功能说明**:获得指定列的反余弦结果 **功能说明**:获得指定列的反余弦结果
...@@ -1020,9 +1020,9 @@ SELECT ROUND(field_name) FROM { tb_name | stb_name } [WHERE clause]; ...@@ -1020,9 +1020,9 @@ SELECT ROUND(field_name) FROM { tb_name | stb_name } [WHERE clause];
### ATAN ### ATAN
```sql ```sql
SELECT ATAN(field_name) FROM { tb_name | stb_name } [WHERE clause] SELECT ATAN(field_name) FROM { tb_name | stb_name } [WHERE clause]
``` ```
**功能说明**:获得指定列的反正切结果 **功能说明**:获得指定列的反正切结果
...@@ -1040,9 +1040,9 @@ SELECT ROUND(field_name) FROM { tb_name | stb_name } [WHERE clause]; ...@@ -1040,9 +1040,9 @@ SELECT ROUND(field_name) FROM { tb_name | stb_name } [WHERE clause];
### SIN ### SIN
```sql ```sql
SELECT SIN(field_name) FROM { tb_name | stb_name } [WHERE clause] SELECT SIN(field_name) FROM { tb_name | stb_name } [WHERE clause]
``` ```
**功能说明**:获得指定列的正弦结果 **功能说明**:获得指定列的正弦结果
...@@ -1060,9 +1060,9 @@ SELECT ROUND(field_name) FROM { tb_name | stb_name } [WHERE clause]; ...@@ -1060,9 +1060,9 @@ SELECT ROUND(field_name) FROM { tb_name | stb_name } [WHERE clause];
### COS ### COS
```sql ```sql
SELECT COS(field_name) FROM { tb_name | stb_name } [WHERE clause] SELECT COS(field_name) FROM { tb_name | stb_name } [WHERE clause]
``` ```
**功能说明**:获得指定列的余弦结果 **功能说明**:获得指定列的余弦结果
...@@ -1080,9 +1080,9 @@ SELECT ROUND(field_name) FROM { tb_name | stb_name } [WHERE clause]; ...@@ -1080,9 +1080,9 @@ SELECT ROUND(field_name) FROM { tb_name | stb_name } [WHERE clause];
### TAN ### TAN
```sql ```sql
SELECT TAN(field_name) FROM { tb_name | stb_name } [WHERE clause] SELECT TAN(field_name) FROM { tb_name | stb_name } [WHERE clause]
``` ```
**功能说明**:获得指定列的正切结果 **功能说明**:获得指定列的正切结果
...@@ -1100,9 +1100,9 @@ SELECT ROUND(field_name) FROM { tb_name | stb_name } [WHERE clause]; ...@@ -1100,9 +1100,9 @@ SELECT ROUND(field_name) FROM { tb_name | stb_name } [WHERE clause];
### POW ### POW
```sql ```sql
SELECT POW(field_name, power) FROM { tb_name | stb_name } [WHERE clause] SELECT POW(field_name, power) FROM { tb_name | stb_name } [WHERE clause]
``` ```
**功能说明**:获得指定列的指数为 power 的幂 **功能说明**:获得指定列的指数为 power 的幂
...@@ -1120,9 +1120,9 @@ SELECT ROUND(field_name) FROM { tb_name | stb_name } [WHERE clause]; ...@@ -1120,9 +1120,9 @@ SELECT ROUND(field_name) FROM { tb_name | stb_name } [WHERE clause];
### LOG ### LOG
```sql ```sql
SELECT LOG(field_name, base) FROM { tb_name | stb_name } [WHERE clause] SELECT LOG(field_name, base) FROM { tb_name | stb_name } [WHERE clause]
``` ```
**功能说明**:获得指定列对于底数 base 的对数 **功能说明**:获得指定列对于底数 base 的对数
...@@ -1140,9 +1140,9 @@ SELECT ROUND(field_name) FROM { tb_name | stb_name } [WHERE clause]; ...@@ -1140,9 +1140,9 @@ SELECT ROUND(field_name) FROM { tb_name | stb_name } [WHERE clause];
### ABS ### ABS
```sql ```sql
SELECT ABS(field_name) FROM { tb_name | stb_name } [WHERE clause] SELECT ABS(field_name) FROM { tb_name | stb_name } [WHERE clause]
``` ```
**功能说明**:获得指定列的绝对值 **功能说明**:获得指定列的绝对值
...@@ -1160,9 +1160,9 @@ SELECT ROUND(field_name) FROM { tb_name | stb_name } [WHERE clause]; ...@@ -1160,9 +1160,9 @@ SELECT ROUND(field_name) FROM { tb_name | stb_name } [WHERE clause];
### SQRT ### SQRT
```sql ```sql
SELECT SQRT(field_name) FROM { tb_name | stb_name } [WHERE clause] SELECT SQRT(field_name) FROM { tb_name | stb_name } [WHERE clause]
``` ```
**功能说明**:获得指定列的平方根 **功能说明**:获得指定列的平方根
...@@ -1180,9 +1180,9 @@ SELECT ROUND(field_name) FROM { tb_name | stb_name } [WHERE clause]; ...@@ -1180,9 +1180,9 @@ SELECT ROUND(field_name) FROM { tb_name | stb_name } [WHERE clause];
### CAST ### CAST
```sql ```sql
SELECT CAST(expression AS type_name) FROM { tb_name | stb_name } [WHERE clause] SELECT CAST(expression AS type_name) FROM { tb_name | stb_name } [WHERE clause]
``` ```
**功能说明**:数据类型转换函数,输入参数 expression 支持普通列、常量、标量函数及它们之间的四则运算,不支持 tag 列,只适用于 select 子句中。 **功能说明**:数据类型转换函数,输入参数 expression 支持普通列、常量、标量函数及它们之间的四则运算,不支持 tag 列,只适用于 select 子句中。
...@@ -1206,9 +1206,9 @@ SELECT ROUND(field_name) FROM { tb_name | stb_name } [WHERE clause]; ...@@ -1206,9 +1206,9 @@ SELECT ROUND(field_name) FROM { tb_name | stb_name } [WHERE clause];
### CONCAT ### CONCAT
```sql ```sql
SELECT CONCAT(str1|column1, str2|column2, ...) FROM { tb_name | stb_name } [WHERE clause] SELECT CONCAT(str1|column1, str2|column2, ...) FROM { tb_name | stb_name } [WHERE clause]
``` ```
**功能说明**:字符串连接函数。 **功能说明**:字符串连接函数。
...@@ -1226,9 +1226,9 @@ SELECT ROUND(field_name) FROM { tb_name | stb_name } [WHERE clause]; ...@@ -1226,9 +1226,9 @@ SELECT ROUND(field_name) FROM { tb_name | stb_name } [WHERE clause];
### CONCAT_WS ### CONCAT_WS
``` ```
SELECT CONCAT_WS(separator, str1|column1, str2|column2, ...) FROM { tb_name | stb_name } [WHERE clause] SELECT CONCAT_WS(separator, str1|column1, str2|column2, ...) FROM { tb_name | stb_name } [WHERE clause]
``` ```
**功能说明**:带分隔符的字符串连接函数。 **功能说明**:带分隔符的字符串连接函数。
...@@ -1246,9 +1246,9 @@ SELECT ROUND(field_name) FROM { tb_name | stb_name } [WHERE clause]; ...@@ -1246,9 +1246,9 @@ SELECT ROUND(field_name) FROM { tb_name | stb_name } [WHERE clause];
### LENGTH ### LENGTH
``` ```
SELECT LENGTH(str|column) FROM { tb_name | stb_name } [WHERE clause] SELECT LENGTH(str|column) FROM { tb_name | stb_name } [WHERE clause]
``` ```
**功能说明**:以字节计数的字符串长度。 **功能说明**:以字节计数的字符串长度。
...@@ -1265,9 +1265,9 @@ SELECT ROUND(field_name) FROM { tb_name | stb_name } [WHERE clause]; ...@@ -1265,9 +1265,9 @@ SELECT ROUND(field_name) FROM { tb_name | stb_name } [WHERE clause];
### CHAR_LENGTH ### CHAR_LENGTH
``` ```
SELECT CHAR_LENGTH(str|column) FROM { tb_name | stb_name } [WHERE clause] SELECT CHAR_LENGTH(str|column) FROM { tb_name | stb_name } [WHERE clause]
``` ```
**功能说明**:以字符计数的字符串长度。 **功能说明**:以字符计数的字符串长度。
...@@ -1284,9 +1284,9 @@ SELECT ROUND(field_name) FROM { tb_name | stb_name } [WHERE clause]; ...@@ -1284,9 +1284,9 @@ SELECT ROUND(field_name) FROM { tb_name | stb_name } [WHERE clause];
### LOWER ### LOWER
``` ```
SELECT LOWER(str|column) FROM { tb_name | stb_name } [WHERE clause] SELECT LOWER(str|column) FROM { tb_name | stb_name } [WHERE clause]
``` ```
**功能说明**:将字符串参数值转换为全小写字母。 **功能说明**:将字符串参数值转换为全小写字母。
...@@ -1303,9 +1303,9 @@ SELECT ROUND(field_name) FROM { tb_name | stb_name } [WHERE clause]; ...@@ -1303,9 +1303,9 @@ SELECT ROUND(field_name) FROM { tb_name | stb_name } [WHERE clause];
### UPPER ### UPPER
``` ```
SELECT UPPER(str|column) FROM { tb_name | stb_name } [WHERE clause] SELECT UPPER(str|column) FROM { tb_name | stb_name } [WHERE clause]
``` ```
**功能说明**:将字符串参数值转换为全大写字母。 **功能说明**:将字符串参数值转换为全大写字母。
...@@ -1322,9 +1322,9 @@ SELECT ROUND(field_name) FROM { tb_name | stb_name } [WHERE clause]; ...@@ -1322,9 +1322,9 @@ SELECT ROUND(field_name) FROM { tb_name | stb_name } [WHERE clause];
### LTRIM ### LTRIM
``` ```
SELECT LTRIM(str|column) FROM { tb_name | stb_name } [WHERE clause] SELECT LTRIM(str|column) FROM { tb_name | stb_name } [WHERE clause]
``` ```
**功能说明**:返回清除左边空格后的字符串。 **功能说明**:返回清除左边空格后的字符串。
...@@ -1341,9 +1341,9 @@ SELECT ROUND(field_name) FROM { tb_name | stb_name } [WHERE clause]; ...@@ -1341,9 +1341,9 @@ SELECT ROUND(field_name) FROM { tb_name | stb_name } [WHERE clause];
### RTRIM ### RTRIM
``` ```
SELECT RTRIM(str|column) FROM { tb_name | stb_name } [WHERE clause] SELECT RTRIM(str|column) FROM { tb_name | stb_name } [WHERE clause]
``` ```
**功能说明**:返回清除右边空格后的字符串。 **功能说明**:返回清除右边空格后的字符串。
...@@ -1360,9 +1360,9 @@ SELECT ROUND(field_name) FROM { tb_name | stb_name } [WHERE clause]; ...@@ -1360,9 +1360,9 @@ SELECT ROUND(field_name) FROM { tb_name | stb_name } [WHERE clause];
### SUBSTR ### SUBSTR
``` ```
SELECT SUBSTR(str,pos[,len]) FROM { tb_name | stb_name } [WHERE clause] SELECT SUBSTR(str,pos[,len]) FROM { tb_name | stb_name } [WHERE clause]
``` ```
**功能说明**:从源字符串 str 中的指定位置 pos 开始取一个长度为 len 的子串并返回。 **功能说明**:从源字符串 str 中的指定位置 pos 开始取一个长度为 len 的子串并返回。
...@@ -1461,7 +1461,7 @@ Query OK, 6 row(s) in set (0.002791s) ...@@ -1461,7 +1461,7 @@ Query OK, 6 row(s) in set (0.002791s)
### STATEDURATION ### STATEDURATION
``` ```sql
SELECT stateDuration(field_name, oper, val, unit) FROM { tb_name | stb_name } [WHERE clause]; SELECT stateDuration(field_name, oper, val, unit) FROM { tb_name | stb_name } [WHERE clause];
``` ```
......
...@@ -30,4 +30,11 @@ taos> DESCRIBE meters; ...@@ -30,4 +30,11 @@ taos> DESCRIBE meters;
groupid | INT | 4 | TAG | groupid | INT | 4 | TAG |
``` ```
数据集包含 4 个智能电表的数据,按照 TDengine 的建模规则,对应 4 个子表,其名称分别是 d1001, d1002, d1003, d1004。 数据集包含 4 个智能电表的数据,按照 TDengine 的建模规则,对应 4 个子表,其名称分别是 d1001, d1002, d1003, d1004。
\ No newline at end of file
```mdx-code-block
import DocCardList from '@theme/DocCardList';
import {useCurrentSidebarCategory} from '@docusaurus/theme-common';
<DocCardList items={useCurrentSidebarCategory().items}/>
```
\ No newline at end of file
label: 运维指南 label: 运维指南
link:
slug: /operation/
type: generated-index
---
title: 运维指南
---
```mdx-code-block
import DocCardList from '@theme/DocCardList';
import {useCurrentSidebarCategory} from '@docusaurus/theme-common';
<DocCardList items={useCurrentSidebarCategory().items}/>
```
\ No newline at end of file
...@@ -20,11 +20,11 @@ import PkgList from "/components/PkgList"; ...@@ -20,11 +20,11 @@ import PkgList from "/components/PkgList";
运行 install_client.sh 进行安装。 运行 install_client.sh 进行安装。
4. 配置 taos.cfg 4. 配置 taos.cfg
编辑 taos.cfg 文件(默认路径/etc/taos/taos.cfg),将 firstEP 修改为 TDengine 服务器的 End Point,例如:`h1.tdengine.com:6030` 编辑 `taos.cfg` 文件(默认路径/etc/taos/taos.cfg),将 `firstEP` 修改为 TDengine 服务器的 End Point,例如:`h1.tdengine.com:6030`
:::tip :::tip
1. 如本机没有部署 TDengine 服务,仅安装了应用驱动,则 taos.cfg 中仅需配置 firstEP,无需在本机配置 FQDN 1. 如本机没有部署 TDengine 服务,仅安装了应用驱动,则 `taos.cfg` 中仅需配置 `firstEP`,无需在本机配置 `FQDN`
2. 为防止与服务器端连接时出现“Unable to resolve FQDN”错误,建议确认本机的 hosts 文件已经配置了服务器正确的 FQDN 值,或配置好了 DNS 服务。 2. 为防止与服务器端连接时出现“Unable to resolve FQDN”错误,建议确认本机的 `/etc/hosts` 文件已经配置了服务器正确的 FQDN 值,或配置好了 DNS 服务。
::: :::
...@@ -136,7 +136,7 @@ import ( ...@@ -136,7 +136,7 @@ import (
) )
func main() { func main() {
var taosUri = "root:taosdata/tcp(localhost:6030)/" var taosUri = "root:taosdata@tcp(localhost:6030)/"
taos, err := sql.Open("taosSql", taosUri) taos, err := sql.Open("taosSql", taosUri)
if err != nil { if err != nil {
fmt.Println("failed to connect TDengine, err:", err) fmt.Println("failed to connect TDengine, err:", err)
...@@ -168,7 +168,7 @@ import ( ...@@ -168,7 +168,7 @@ import (
) )
func main() { func main() {
var taosUri = "root:taosdata/http(localhost:6041)/" var taosUri = "root:taosdata@http(localhost:6041)/"
taos, err := sql.Open("taosRestful", taosUri) taos, err := sql.Open("taosRestful", taosUri)
if err != nil { if err != nil {
fmt.Println("failed to connect TDengine, err:", err) fmt.Println("failed to connect TDengine, err:", err)
......
...@@ -211,7 +211,7 @@ url 中的配置参数如下: ...@@ -211,7 +211,7 @@ url 中的配置参数如下:
INSERT INTO test.t1 USING test.weather (ts, temperature) TAGS('beijing') VALUES(now, 24.6); INSERT INTO test.t1 USING test.weather (ts, temperature) TAGS('beijing') VALUES(now, 24.6);
``` ```
- 从 taos-jdbcdriver-2.0.36 和 TDengine 2.2.0.0 版本开始,如果在 url 中指定了 dbname,那么,JDBC REST 连接会默认使用/rest/sql/dbname 作为 resful 请求的 url,在 SQL 中不需要指定 dbname。例如:url 为 jdbc:TAOS-RS://127.0.0.1:6041/test,那么,可以执行 sql:insert into t1 using weather(ts, temperatrue) tags('beijing') values(now, 24.6); - 从 taos-jdbcdriver-2.0.36 和 TDengine 2.2.0.0 版本开始,如果在 url 中指定了 dbname,那么,JDBC REST 连接会默认使用/rest/sql/dbname 作为 restful 请求的 url,在 SQL 中不需要指定 dbname。例如:url 为 jdbc:TAOS-RS://127.0.0.1:6041/test,那么,可以执行 sql:insert into t1 using weather(ts, temperature) tags('beijing') values(now, 24.6);
::: :::
...@@ -348,8 +348,8 @@ JDBC 连接器可能报错的错误码包括 3 种:JDBC driver 本身的报错 ...@@ -348,8 +348,8 @@ JDBC 连接器可能报错的错误码包括 3 种:JDBC driver 本身的报错
具体的错误码请参考: 具体的错误码请参考:
- https://github.com/taosdata/TDengine/blob/develop/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBErrorNumbers.java - [TDengine Java Connector](https://github.com/taosdata/TDengine/blob/develop/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBErrorNumbers.java)
- https://github.com/taosdata/TDengine/blob/develop/src/inc/taoserror.h - [TDengine_ERROR_CODE](https://github.com/taosdata/TDengine/blob/develop/src/inc/taoserror.h)
### 通过参数绑定写入数据 ### 通过参数绑定写入数据
...@@ -816,21 +816,21 @@ Query OK, 1 row(s) in set (0.000141s) ...@@ -816,21 +816,21 @@ Query OK, 1 row(s) in set (0.000141s)
1. 使用 Statement 的 `addBatch` 和 `executeBatch` 来执行“批量写入/更行”,为什么没有带来性能上的提升? 1. 使用 Statement 的 `addBatch` 和 `executeBatch` 来执行“批量写入/更行”,为什么没有带来性能上的提升?
**原因**:TDengine 的 JDBC 实现中,通过 `addBatch` 方法提交的 SQL 语句,会按照添加的顺序,依次执行,这种方式没有减少与服务端的交互次数,不会带来性能上的提升。 **原因**:TDengine 的 JDBC 实现中,通过 `addBatch` 方法提交的 SQL 语句,会按照添加的顺序,依次执行,这种方式没有减少与服务端的交互次数,不会带来性能上的提升。
**解决方法**:1. 在一条 insert 语句中拼接多个 values 值;2. 使用多线程的方式并发插入;3. 使用参数绑定的写入方式 **解决方法**:1. 在一条 insert 语句中拼接多个 values 值;2. 使用多线程的方式并发插入;3. 使用参数绑定的写入方式
2. java.lang.UnsatisfiedLinkError: no taos in java.library.path 2. java.lang.UnsatisfiedLinkError: no taos in java.library.path
**原因**:程序没有找到依赖的本地函数库 taos。 **原因**:程序没有找到依赖的本地函数库 taos。
**解决方法**:Windows 下可以将 C:\TDengine\driver\taos.dll 拷贝到 C:\Windows\System32\ 目录下,Linux 下将建立如下软链 `ln -s /usr/local/taos/driver/libtaos.so.x.x.x.x /usr/lib/libtaos.so` 即可。 **解决方法**:Windows 下可以将 C:\TDengine\driver\taos.dll 拷贝到 C:\Windows\System32\ 目录下,Linux 下将建立如下软链 `ln -s /usr/local/taos/driver/libtaos.so.x.x.x.x /usr/lib/libtaos.so` 即可。
3. java.lang.UnsatisfiedLinkError: taos.dll Can't load AMD 64 bit on a IA 32-bit platform 3. java.lang.UnsatisfiedLinkError: taos.dll Can't load AMD 64 bit on a IA 32-bit platform
**原因**:目前 TDengine 只支持 64 位 JDK。 **原因**:目前 TDengine 只支持 64 位 JDK。
**解决方法**:重新安装 64 位 JDK。 **解决方法**:重新安装 64 位 JDK。
4. 其它问题请参考 [FAQ](/train-faq/faq) 4. 其它问题请参考 [FAQ](/train-faq/faq)
......
...@@ -361,7 +361,7 @@ taosBenchmark -A INT,DOUBLE,NCHAR,BINARY\(16\) ...@@ -361,7 +361,7 @@ taosBenchmark -A INT,DOUBLE,NCHAR,BINARY\(16\)
### 查询场景配置参数 ### 查询场景配置参数
查询场景下 `filetype` 必须设置为 `qeury`,该参数及其它通用参数详见[通用配置参数](#通用配置参数) 查询场景下 `filetype` 必须设置为 `query`,该参数及其它通用参数详见[通用配置参数](#通用配置参数)
#### 执行指定查询语句的配置参数 #### 执行指定查询语句的配置参数
...@@ -400,7 +400,7 @@ taosBenchmark -A INT,DOUBLE,NCHAR,BINARY\(16\) ...@@ -400,7 +400,7 @@ taosBenchmark -A INT,DOUBLE,NCHAR,BINARY\(16\)
- **threads** : 执行 SQL 的线程数,默认为 1。 - **threads** : 执行 SQL 的线程数,默认为 1。
- **interva** : 执行订阅的时间间隔,单位为秒,默认为 0。 - **interval** : 执行订阅的时间间隔,单位为秒,默认为 0。
- **restart** : "yes" 表示开始新的订阅,"no" 表示继续之前的订阅,默认值为 "no"。 - **restart** : "yes" 表示开始新的订阅,"no" 表示继续之前的订阅,默认值为 "no"。
...@@ -420,7 +420,7 @@ taosBenchmark -A INT,DOUBLE,NCHAR,BINARY\(16\) ...@@ -420,7 +420,7 @@ taosBenchmark -A INT,DOUBLE,NCHAR,BINARY\(16\)
- **threads** : 执行 SQL 的线程数,默认为 1。 - **threads** : 执行 SQL 的线程数,默认为 1。
- **interva** : 执行订阅的时间间隔,单位为秒,默认为 0。 - **interval** : 执行订阅的时间间隔,单位为秒,默认为 0。
- **restart** : "yes" 表示开始新的订阅,"no" 表示继续之前的订阅,默认值为 "no"。 - **restart** : "yes" 表示开始新的订阅,"no" 表示继续之前的订阅,默认值为 "no"。
......
...@@ -12,7 +12,7 @@ TDengine 命令行程序(以下简称 TDengine CLI)是用户操作 TDengine ...@@ -12,7 +12,7 @@ TDengine 命令行程序(以下简称 TDengine CLI)是用户操作 TDengine
## 执行 ## 执行
要进入 TDengine CLI,您只要在 Linux 终端或Windos 终端执行 `taos` 即可。 要进入 TDengine CLI,您只要在 Linux 终端或Windows 终端执行 `taos` 即可。
```bash ```bash
taos taos
......
label: 参考指南 label: 参考指南
link: \ No newline at end of file
slug: /reference/
type: generated-index
description: "参考指南是对 TDengine 本身、 TDengine 各语言连接器及自带的工具最详细的介绍。"
...@@ -38,7 +38,7 @@ LoadPlugin network ...@@ -38,7 +38,7 @@ LoadPlugin network
其中 <taosAdapter's host\> 填写运行 taosAdapter 的服务器域名或 IP 地址。<port for collectd direct\> 填写 taosAdapter 用于接收 collectd 数据的端口(默认为 6045)。 其中 <taosAdapter's host\> 填写运行 taosAdapter 的服务器域名或 IP 地址。<port for collectd direct\> 填写 taosAdapter 用于接收 collectd 数据的端口(默认为 6045)。
例如下: 例如下:
```text ```text
LoadPlugin network LoadPlugin network
......
...@@ -33,7 +33,7 @@ object OpenTsdbWriter "opentsdb" { ...@@ -33,7 +33,7 @@ object OpenTsdbWriter "opentsdb" {
} }
``` ```
例文件: 例文件:
``` ```
object OpenTsdbWriter "opentsdb" { object OpenTsdbWriter "opentsdb" {
......
---
title: 参考指南
---
参考指南是对 TDengine 本身、 TDengine 各语言连接器及自带的工具最详细的介绍。
```mdx-code-block
import DocCardList from '@theme/DocCardList';
import {useCurrentSidebarCategory} from '@docusaurus/theme-common';
<DocCardList items={useCurrentSidebarCategory().items}/>
```
\ No newline at end of file
...@@ -5,6 +5,12 @@ title: Grafana ...@@ -5,6 +5,12 @@ title: Grafana
TDengine 能够与开源数据可视化系统 [Grafana](https://www.grafana.com/) 快速集成搭建数据监测报警系统,整个过程无需任何代码开发,TDengine 中数据表的内容可以在仪表盘(DashBoard)上进行可视化展现。关于 TDengine 插件的使用您可以在[GitHub](https://github.com/taosdata/grafanaplugin/blob/master/README.md)中了解更多。 TDengine 能够与开源数据可视化系统 [Grafana](https://www.grafana.com/) 快速集成搭建数据监测报警系统,整个过程无需任何代码开发,TDengine 中数据表的内容可以在仪表盘(DashBoard)上进行可视化展现。关于 TDengine 插件的使用您可以在[GitHub](https://github.com/taosdata/grafanaplugin/blob/master/README.md)中了解更多。
## 前置条件
要让 Grafana 能正常添加 TDengine 数据源,需要以下几方面的准备工作。
- TDengine 集群已经部署并正常运行
- taosAdapter 已经安装并正常运行。具体细节请参考 [taosAdapter 的使用手册](/reference/taosadapter)
## 安装 Grafana ## 安装 Grafana
目前 TDengine 支持 Grafana 7.0 以上的版本。用户可以根据当前的操作系统,到 Grafana 官网下载安装包,并执行安装。下载地址如下:<https://grafana.com/grafana/download>。 目前 TDengine 支持 Grafana 7.0 以上的版本。用户可以根据当前的操作系统,到 Grafana 官网下载安装包,并执行安装。下载地址如下:<https://grafana.com/grafana/download>。
...@@ -78,7 +84,7 @@ GF_PLUGINS_ALLOW_LOADING_UNSIGNED_PLUGINS=tdengine-datasource ...@@ -78,7 +84,7 @@ GF_PLUGINS_ALLOW_LOADING_UNSIGNED_PLUGINS=tdengine-datasource
![img](/img/connections/create_dashboard1.jpg) ![img](/img/connections/create_dashboard1.jpg)
如上图所示,在 Query 中选中 `TDengine` 数据源,在下方查询框可输入相应 sql 进行查询,具体说明如下: 如上图所示,在 Query 中选中 `TDengine` 数据源,在下方查询框可输入相应 SQL 进行查询,具体说明如下:
- INPUT SQL:输入要查询的语句(该 SQL 语句的结果集应为两列多行),例如:`select avg(mem_system) from log.dn where ts >= $from and ts < $to interval($interval)` ,其中,from、to 和 interval 为 TDengine 插件的内置变量,表示从 Grafana 插件面板获取的查询范围和时间间隔。除了内置变量外,`也支持可以使用自定义模板变量`。 - INPUT SQL:输入要查询的语句(该 SQL 语句的结果集应为两列多行),例如:`select avg(mem_system) from log.dn where ts >= $from and ts < $to interval($interval)` ,其中,from、to 和 interval 为 TDengine 插件的内置变量,表示从 Grafana 插件面板获取的查询范围和时间间隔。除了内置变量外,`也支持可以使用自定义模板变量`。
- ALIAS BY:可设置当前查询别名。 - ALIAS BY:可设置当前查询别名。
......
---
sidebar_label: EMQ Broker
title: EMQ Broker 写入
---
MQTT 是流行的物联网数据传输协议,[EMQ](https://github.com/emqx/emqx)是一开源的 MQTT Broker 软件,无需任何代码,只需要在 EMQ Dashboard 里使用“规则”做简单配置,即可将 MQTT 的数据直接写入 TDengine。EMQ X 支持通过 发送到 Web 服务的方式保存数据到 TDEngine,也在企业版上提供原生的 TDEngine 驱动实现直接保存。详细使用方法请参考 [EMQ 官方文档](https://docs.emqx.io/broker/latest/cn/rule/rule-example.html#%E4%BF%9D%E5%AD%98%E6%95%B0%E6%8D%AE%E5%88%B0-tdengine)
---
sidebar_label: EMQX Broker
title: EMQX Broker 写入
---
MQTT 是流行的物联网数据传输协议,[EMQX](https://github.com/emqx/emqx)是一开源的 MQTT Broker 软件,无需任何代码,只需要在 EMQX Dashboard 里使用“规则”做简单配置,即可将 MQTT 的数据直接写入 TDengine。EMQX 支持通过 发送到 Web 服务的方式保存数据到 TDengine,也在企业版上提供原生的 TDengine 驱动实现直接保存。
## 前置条件
要让 EMQX 能正常添加 TDengine 数据源,需要以下几方面的准备工作。
- TDengine 集群已经部署并正常运行
- taosAdapter 已经安装并正常运行。具体细节请参考 [taosAdapter 的使用手册](/reference/taosadapter)
- 如果使用后文介绍的模拟写入程序,需要安装合适版本的 Node.js,推荐安装 v12。
## 安装并启动 EMQX
用户可以根据当前的操作系统,到 EMQX 官网下载安装包,并执行安装。下载地址如下:<https://www.emqx.io/zh/downloads>。安装后使用 `sudo emqx start``sudo systemctl start emqx` 启动 EMQX 服务。
## 在 TDengine 中为接收 MQTT 数据创建相应数据库和表结构
### 以 Docker 安装 TDengine 为例
```bash
docker exec -it tdengine bash
taos
```
### 创建数据库和表
```sql
create database test;
use test;
create table:
CREATE TABLE sensor_data (ts timestamp, temperature float, humidity float, volume float, PM10 float, pm25 float, SO2 float, NO2 float, CO float, sensor_id NCHAR(255), area TINYINT, coll_time timestamp);
```
注:表结构以博客[数据传输、存储、展现,EMQ X + TDengine 搭建 MQTT 物联网数据可视化平台](https://www.taosdata.com/blog/2020/08/04/1722.html)为例。后续操作均以此博客场景为例进行,请你根据实际应用场景进行修改。
## 配置 EMQX 规则
由于 EMQX 不同版本配置界面所有不同,这里仅以 v4.4.3 为例,其他版本请参考相应官网文档。
### 登录 EMQX Dashboard
使用浏览器打开网址 http://IP:18083 并登录 EMQX Dashboard。初次安装用户名为 `admin` 密码为:`public`
![img](./emqx/login-dashboard.png)
### 创建规则(Rule)
选择左侧“规则引擎(Rule Engine)”中的“规则(Rule)”并点击“创建(Create)”按钮:
![img](./emqx/rule-engine.png)
### 编辑 SQL 字段
![img](./emqx/create-rule.png)
### 新增“动作(action handler)”
![img](./emqx/add-action-handler.png)
### 新增“资源(Resource)”
![img](./emqx/create-resource.png)
选择“发送数据到 Web 服务“并点击“新建资源”按钮:
### 编辑“资源(Resource)”
选择“发送数据到 Web 服务“并填写 请求 URL 为 运行 taosAdapter 的服务器地址和端口(默认为 6041)。其他属性请保持默认值。
![img](./emqx/edit-resource.png)
### 编辑“动作(action)”
编辑资源配置,增加 Authorization 认证的键/值配对项,相关文档请参考[ TDengine REST API 文档](https://docs.taosdata.com/reference/rest-api/)。在消息体中输入规则引擎替换模板。
![img](./emqx/edit-action.png)
## 编写模拟测试程序
```javascript
// mock.js
const mqtt = require('mqtt')
const Mock = require('mockjs')
const EMQX_SERVER = 'mqtt://localhost:1883'
const CLIENT_NUM = 10
const STEP = 5000 // 模拟采集时间间隔 ms
const AWAIT = 5000 // 每次发送完后休眠时间,防止消息速率过快 ms
const CLIENT_POOL = []
startMock()
function sleep(timer = 100) {
return new Promise(resolve => {
setTimeout(resolve, timer)
})
}
async function startMock() {
const now = Date.now()
for (let i = 0; i < CLIENT_NUM; i++) {
const client = await createClient(`mock_client_${i}`)
CLIENT_POOL.push(client)
}
// last 24h every 5s
const last = 24 * 3600 * 1000
for (let ts = now - last; ts <= now; ts += STEP) {
for (const client of CLIENT_POOL) {
const mockData = generateMockData()
const data = {
...mockData,
id: client.clientId,
area: 0,
ts,
}
client.publish('sensor/data', JSON.stringify(data))
}
const dateStr = new Date(ts).toLocaleTimeString()
console.log(`${dateStr} send success.`)
await sleep(AWAIT)
}
console.log(`Done, use ${(Date.now() - now) / 1000}s`)
}
/**
* Init a virtual mqtt client
* @param {string} clientId ClientID
*/
function createClient(clientId) {
return new Promise((resolve, reject) => {
const client = mqtt.connect(EMQX_SERVER, {
clientId,
})
client.on('connect', () => {
console.log(`client ${clientId} connected`)
resolve(client)
})
client.on('reconnect', () => {
console.log('reconnect')
})
client.on('error', (e) => {
console.error(e)
reject(e)
})
})
}
/**
* Generate mock data
*/
function generateMockData() {
return {
"temperature": parseFloat(Mock.Random.float(22, 100).toFixed(2)),
"humidity": parseFloat(Mock.Random.float(12, 86).toFixed(2)),
"volume": parseFloat(Mock.Random.float(20, 200).toFixed(2)),
"PM10": parseFloat(Mock.Random.float(0, 300).toFixed(2)),
"pm25": parseFloat(Mock.Random.float(0, 300).toFixed(2)),
"SO2": parseFloat(Mock.Random.float(0, 50).toFixed(2)),
"NO2": parseFloat(Mock.Random.float(0, 50).toFixed(2)),
"CO": parseFloat(Mock.Random.float(0, 50).toFixed(2)),
"area": Mock.Random.integer(0, 20),
"ts": 1596157444170,
}
}
```
注意:代码中 CLIENT_NUM 在开始测试中可以先设置一个较小的值,避免硬件性能不能完全处理较大并发客户端数量。
![img](./emqx/client-num.png)
## 执行测试模拟发送 MQTT 数据
```
npm install mqtt mockjs --save --registry=https://registry.npm.taobao.org
node mock.js
```
![img](./emqx/run-mock.png)
## 验证 EMQX 接收到数据
在 EMQX Dashboard 规则引擎界面进行刷新,可以看到有多少条记录被正确接收到:
![img](./emqx/check-rule-matched.png)
## 验证数据写入到 TDengine
使用 TDengine CLI 程序登录并查询相应数据库和表,验证数据是否被正确写入到 TDengine 中:
![img](./emqx/check-result-in-taos.png)
TDengine 详细使用方法请参考 [TDengine 官方文档](https://docs.taosdata.com/)
EMQX 详细使用方法请参考 [EMQ 官方文档](https://www.emqx.io/docs/zh/v4.4/rule/rule-engine.html)
label: 第三方工具 label: 第三方工具
link: \ No newline at end of file
type: generated-index
slug: /third-party/
description: TDengine 通过对标准 SQL 命令、常用数据库连接器标准(例如 JDBC)、ORM 以及其他流行时序数据库写入协议(例如 InfluxDB Line Protocol、OpenTSDB JSON、OpenTSDB Telnet 等)的支持可以使 TDengine 非常容易和第三方工具共同使用。
---
title: 第三方工具
---
TDengine 通过对标准 SQL 命令、常用数据库连接器标准(例如 JDBC)、ORM 以及其他流行时序数据库写入协议(例如 InfluxDB Line Protocol、OpenTSDB JSON、OpenTSDB Telnet 等)的支持可以使 TDengine 非常容易和第三方工具共同使用。
```mdx-code-block
import DocCardList from '@theme/DocCardList';
import {useCurrentSidebarCategory} from '@docusaurus/theme-common';
<DocCardList items={useCurrentSidebarCategory().items}/>
```
\ No newline at end of file
label: 技术内幕 label: 技术内幕
link: \ No newline at end of file
slug: /tdinternal/
type: generated-index
\ No newline at end of file
---
title: 技术内幕
---
```mdx-code-block
import DocCardList from '@theme/DocCardList';
import {useCurrentSidebarCategory} from '@docusaurus/theme-common';
<DocCardList items={useCurrentSidebarCategory().items}/>
```
\ No newline at end of file
...@@ -34,11 +34,11 @@ IT 运维监测数据通常都是对时间特性比较敏感的数据,例如 ...@@ -34,11 +34,11 @@ IT 运维监测数据通常都是对时间特性比较敏感的数据,例如
### TDengine ### TDengine
从涛思数据官网[下载](http://taosdata.com/cn/all-downloads/)页面下载最新 TDengine-server 2.3.0.0 或以上版本安装。 从涛思数据官网[下载](http://taosdata.com/cn/all-downloads/)页面下载最新 TDengine-server 2.4.0.x 或以上版本安装。
## 数据链路设置 ## 数据链路设置
### 下载 TDengine 插件到 grafana 插件目录 ### 下载 TDengine 插件到 Grafana 插件目录
```bash ```bash
1. wget -c https://github.com/taosdata/grafanaplugin/releases/download/v3.1.3/tdengine-datasource-3.1.3.zip 1. wget -c https://github.com/taosdata/grafanaplugin/releases/download/v3.1.3/tdengine-datasource-3.1.3.zip
...@@ -50,7 +50,7 @@ IT 运维监测数据通常都是对时间特性比较敏感的数据,例如 ...@@ -50,7 +50,7 @@ IT 运维监测数据通常都是对时间特性比较敏感的数据,例如
### 修改 /etc/telegraf/telegraf.conf ### 修改 /etc/telegraf/telegraf.conf
配置方法,在 /etc/telegraf/telegraf.conf 增加如下文字,其中 database name 请填写希望在 TDengine 保存 Telegraf 数据的数据库名,TDengine server/cluster host、username 和 password 填写 TDengine 实际值: 配置方法,在 `/etc/telegraf/telegraf.conf` 增加如下文字,其中 `database name` 请填写希望在 TDengine 保存 Telegraf 数据的数据库名,`TDengine server/cluster host``username``password` 填写 TDengine 实际值:
``` ```
[[outputs.http]] [[outputs.http]]
...@@ -63,7 +63,7 @@ IT 运维监测数据通常都是对时间特性比较敏感的数据,例如 ...@@ -63,7 +63,7 @@ IT 运维监测数据通常都是对时间特性比较敏感的数据,例如
influx_max_line_bytes = 250 influx_max_line_bytes = 250
``` ```
然后重启 telegraf: 然后重启 Telegraf:
```bash ```bash
sudo systemctl start telegraf sudo systemctl start telegraf
...@@ -71,12 +71,12 @@ sudo systemctl start telegraf ...@@ -71,12 +71,12 @@ sudo systemctl start telegraf
### 导入 Dashboard ### 导入 Dashboard
使用 Web 浏览器访问 IP:3000 登录 Grafana 界面,系统初始用户名密码为 admin/admin。 使用 Web 浏览器访问 `IP:3000` 登录 Grafana 界面,系统初始用户名密码为 admin/admin。
点击左侧齿轮图标并选择 Plugins,应该可以找到 TDengine data source 插件图标。 点击左侧齿轮图标并选择 `Plugins`,应该可以找到 TDengine data source 插件图标。
点击左侧加号图标并选择 Import,从 `https://github.com/taosdata/grafanaplugin/blob/master/examples/telegraf/grafana/dashboards/telegraf-dashboard-v0.1.0.json` 下载 dashboard JSON 文件后导入。之后可以看到如下界面的仪表盘: 点击左侧加号图标并选择 `Import`,从 `https://github.com/taosdata/grafanaplugin/blob/master/examples/telegraf/grafana/dashboards/telegraf-dashboard-v0.1.0.json` 下载 dashboard JSON 文件后导入。之后可以看到如下界面的仪表盘:
![IT-DevOps-Solutions-telegraf-dashboard.png](/img/IT-DevOps-Solutions-telegraf-dashboard.png) ![IT-DevOps-Solutions-telegraf-dashboard.png](/img/IT-DevOps-Solutions-telegraf-dashboard.png)
## 总结 ## 总结
以上演示如何快速搭建一个完整的 IT 运维展示系统。得力于 TDengine 2.3.0.0 版本中新增的 schemaless 协议解析功能,以及强大的生态软件适配能力,用户可以短短数分钟就可以搭建一个高效易用的 IT 运维系统。TDengine 强大的数据写入查询性能和其他丰富功能请参考官方文档和产品落地案例。 以上演示如何快速搭建一个完整的 IT 运维展示系统。得力于 TDengine 2.4.0.0 版本中新增的 schemaless 协议解析功能,以及强大的生态软件适配能力,用户可以短短数分钟就可以搭建一个高效易用的 IT 运维系统。TDengine 强大的数据写入查询性能和其他丰富功能请参考官方文档和产品落地案例。
...@@ -36,7 +36,7 @@ IT 运维监测数据通常都是对时间特性比较敏感的数据,例如 ...@@ -36,7 +36,7 @@ IT 运维监测数据通常都是对时间特性比较敏感的数据,例如
### 安装 TDengine ### 安装 TDengine
从涛思数据官网[下载](http://taosdata.com/cn/all-downloads/)页面下载最新 TDengine-server 2.3.0.0 或以上版本安装。 从涛思数据官网[下载](http://taosdata.com/cn/all-downloads/)页面下载最新 TDengine-server 2.4.0.x 或以上版本安装。
## 数据链路设置 ## 数据链路设置
...@@ -52,7 +52,7 @@ IT 运维监测数据通常都是对时间特性比较敏感的数据,例如 ...@@ -52,7 +52,7 @@ IT 运维监测数据通常都是对时间特性比较敏感的数据,例如
### 配置 collectd ### 配置 collectd
`/etc/collectd/collectd.conf` 文件中增加如下内容,其中 host 和 port 请填写 TDengine 和 taosAdapter 配置的实际值: `/etc/collectd/collectd.conf` 文件中增加如下内容,其中 `host``port` 请填写 TDengine 和 taosAdapter 配置的实际值:
``` ```
LoadPlugin network LoadPlugin network
...@@ -65,7 +65,7 @@ sudo systemctl start collectd ...@@ -65,7 +65,7 @@ sudo systemctl start collectd
### 配置 StatsD ### 配置 StatsD
`config.js` 文件中增加如下内容后启动 StatsD,其中 host 和 port 请填写 TDengine 和 taosAdapter 配置的实际值: `config.js` 文件中增加如下内容后启动 StatsD,其中 `host``port` 请填写 TDengine 和 taosAdapter 配置的实际值:
``` ```
backends 部分添加 "./backends/repeater" backends 部分添加 "./backends/repeater"
...@@ -74,22 +74,22 @@ repeater 部分添加 { host:'<TDengine server/cluster host>', port: <port for S ...@@ -74,22 +74,22 @@ repeater 部分添加 { host:'<TDengine server/cluster host>', port: <port for S
### 导入 Dashboard ### 导入 Dashboard
使用 Web 浏览器访问运行 Grafana 的服务器的 3000 端口 host:3000 登录 Grafana 界面,系统初始用户名密码为 admin/admin 使用 Web 浏览器访问运行 Grafana 的服务器的 3000 端口 `host:3000` 登录 Grafana 界面,系统初始用户名密码为 `admin/admin`
点击左侧齿轮图标并选择 Plugins,应该可以找到 TDengine data source 插件图标。 点击左侧齿轮图标并选择 `Plugins`,应该可以找到 TDengine data source 插件图标。
#### 导入 collectd 仪表盘 #### 导入 collectd 仪表盘
从 https://github.com/taosdata/grafanaplugin/blob/master/examples/collectd/grafana/dashboards/collect-metrics-with-tdengine-v0.1.0.json 下载 dashboard json 文件,点击左侧加号图标并选择 Import,按照界面提示选择 JSON 文件导入。之后可以看到如下界面的仪表盘: 从 https://github.com/taosdata/grafanaplugin/blob/master/examples/collectd/grafana/dashboards/collect-metrics-with-tdengine-v0.1.0.json 下载 dashboard json 文件,点击左侧加号图标并选择 `Import`,按照界面提示选择 JSON 文件导入。之后可以看到如下界面的仪表盘:
![IT-DevOps-Solutions-collectd-dashboard.png](/img/IT-DevOps-Solutions-collectd-dashboard.png) ![IT-DevOps-Solutions-collectd-dashboard.png](/img/IT-DevOps-Solutions-collectd-dashboard.png)
#### 导入 StatsD 仪表盘 #### 导入 StatsD 仪表盘
https://github.com/taosdata/grafanaplugin/blob/master/examples/statsd/dashboards/statsd-with-tdengine-v0.1.0.json 下载 dashboard json 文件,点击左侧加号图标并选择 Import,按照界面提示导入 JSON 文件。之后可以看到如下界面的仪表盘: `https://github.com/taosdata/grafanaplugin/blob/master/examples/statsd/dashboards/statsd-with-tdengine-v0.1.0.json` 下载 dashboard json 文件,点击左侧加号图标并选择 `Import`,按照界面提示导入 JSON 文件。之后可以看到如下界面的仪表盘:
![IT-DevOps-Solutions-statsd-dashboard.png](/img/IT-DevOps-Solutions-statsd-dashboard.png) ![IT-DevOps-Solutions-statsd-dashboard.png](/img/IT-DevOps-Solutions-statsd-dashboard.png)
## 总结 ## 总结
TDengine 作为新兴的时序大数据平台,具备极强的高性能、高可靠、易管理、易维护的优势。得力于 TDengine 2.3.0.0 版本中新增的 schemaless 协议解析功能,以及强大的生态软件适配能力,用户可以短短数分钟就可以搭建一个高效易用的 IT 运维系统或者适配一个已存在的系统。 TDengine 作为新兴的时序大数据平台,具备极强的高性能、高可靠、易管理、易维护的优势。得力于 TDengine 2.4.0.0 版本中新增的 schemaless 协议解析功能,以及强大的生态软件适配能力,用户可以短短数分钟就可以搭建一个高效易用的 IT 运维系统或者适配一个已存在的系统。
TDengine 强大的数据写入查询性能和其他丰富功能请参考官方文档和产品成功落地案例。 TDengine 强大的数据写入查询性能和其他丰富功能请参考官方文档和产品成功落地案例。
label: 应用实践 label: 应用实践
link:
slug: /application/
type: generated-index
---
title: 应用实践
---
```mdx-code-block
import DocCardList from '@theme/DocCardList';
import {useCurrentSidebarCategory} from '@docusaurus/theme-common';
<DocCardList items={useCurrentSidebarCategory().items}/>
```
\ No newline at end of file
...@@ -177,40 +177,9 @@ title: 常见问题及反馈 ...@@ -177,40 +177,9 @@ title: 常见问题及反馈
<a class="anchor" id="timezone"></a> <a class="anchor" id="timezone"></a>
**18. 时间戳的时区信息是怎样处理的?** **18. go 语言编写组件编译失败怎样解决?**
TDengine 中时间戳的时区总是由客户端进行处理,而与服务端无关。具体来说,客户端会对 SQL 语句中的时间戳进行时区转换,转为 UTC 时区(即 Unix 时间戳——Unix Timestamp)再交由服务端进行写入和查询;在读取数据时,服务端也是采用 UTC 时区提供原始数据,客户端收到后再根据本地设置,把时间戳转换为本地系统所要求的时区进行显示。 TDengine 2.3.0.0 及之后的版本包含一个使用 go 语言开发的 taosAdapter 独立组件,需要单独运行,取代之前 taosd 内置的 httpd ,提供包含原 httpd 功能以及支持多种其他软件(Prometheus、Telegraf、collectd、StatsD 等)的数据接入功能。
客户端在处理时间戳字符串时,会采取如下逻辑:
1. 在未做特殊设置的情况下,客户端默认使用所在操作系统的时区设置。
2. 如果在 taos.cfg 中设置了 timezone 参数,则客户端会以这个配置文件中的设置为准。
3. 如果在 C/C++/Java/Python 等各种编程语言的 Connector Driver 中,在建立数据库连接时显式指定了 timezone,那么会以这个指定的时区设置为准。例如 Java Connector 的 JDBC URL 中就有 timezone 参数。
4. 在书写 SQL 语句时,也可以直接使用 Unix 时间戳(例如 `1554984068000`)或带有时区的时间戳字符串,也即以 RFC 3339 格式(例如 `2013-04-12T15:52:01.123+08:00`)或 ISO-8601 格式(例如 `2013-04-12T15:52:01.123+0800`)来书写时间戳,此时这些时间戳的取值将不再受其他时区设置的影响。
<a class="anchor" id="port"></a>
**19. TDengine 都会用到哪些网络端口?**
在 TDengine 2.0 版本中,会用到以下这些网络端口(以默认端口 6030 为前提进行说明,如果修改了配置文件中的设置,那么这里列举的端口都会出现变化),管理员可以参考这里的信息调整防火墙设置:
| 协议 | 默认端口 | 用途说明 | 修改方法 |
| :--- | :-------- | :---------------------------------- | :--------------------------------------------------------------------------------------------------------------------------------- |
| TCP | 6030 | 客户端与服务端之间通讯。 | 由配置文件设置 serverPort 决定。 |
| TCP | 6035 | 多节点集群的节点间通讯。 | 随 serverPort 端口变化。 |
| TCP | 6040 | 多节点集群的节点间数据同步。 | 随 serverPort 端口变化。 |
| TCP | 6041 | 客户端与服务端之间的 RESTful 通讯。 | 随 serverPort 端口变化。注意 taosAdapter 配置或有不同,请参考相应[文档](/reference/taosadapter/)。 |
| TCP | 6042 | Arbitrator 的服务端口。 | 随 Arbitrator 启动参数设置变化。 |
| TCP | 6043 | TaosKeeper 监控服务端口。 | 随 TaosKeeper 启动参数设置变化。 |
| TCP | 6044 | 支持 StatsD 的数据接入端口。 | 随 taosAdapter 启动参数设置变化(2.3.0.1+以上版本)。 |
| TCP | 6045 | 支持 collectd 数据接入端口。 | 随 taosAdapter 启动参数设置变化(2.3.0.1+以上版本)。 |
| TCP | 6060 | 企业版内 Monitor 服务的网络端口。 | |
| UDP | 6030-6034 | 客户端与服务端之间通讯。 | 随 serverPort 端口变化。 |
| UDP | 6035-6039 | 多节点集群的节点间通讯。 | 随 serverPort 端口变化。 |
**20. go 语言编写组件编译失败怎样解决?**
新版本 TDengine 2.3.0.0 包含一个使用 go 语言开发的 taosAdapter 独立组件,需要单独运行,取代之前 taosd 内置的 httpd ,提供包含原 httpd 功能以及支持多种其他软件(Prometheus、Telegraf、collectd、StatsD 等)的数据接入功能。
使用最新 develop 分支代码编译需要先 `git submodule update --init --recursive` 下载 taosAdapter 仓库代码后再编译。 使用最新 develop 分支代码编译需要先 `git submodule update --init --recursive` 下载 taosAdapter 仓库代码后再编译。
目前编译方式默认自动编译 taosAdapter。go 语言版本要求 1.14 以上,如果发生 go 编译错误,往往是国内访问 go mod 问题,可以通过设置 go 环境变量来解决: 目前编译方式默认自动编译 taosAdapter。go 语言版本要求 1.14 以上,如果发生 go 编译错误,往往是国内访问 go mod 问题,可以通过设置 go 环境变量来解决:
...@@ -222,3 +191,14 @@ title: 常见问题及反馈 ...@@ -222,3 +191,14 @@ title: 常见问题及反馈
如果希望继续使用之前的内置 httpd,可以关闭 taosAdapter 编译,使用 如果希望继续使用之前的内置 httpd,可以关闭 taosAdapter 编译,使用
`cmake .. -DBUILD_HTTP=true` 使用原来内置的 httpd。 `cmake .. -DBUILD_HTTP=true` 使用原来内置的 httpd。
**19. 如何查询数据占用的存储空间大小?**
默认情况下,TDengine 的数据文件存储在 /var/lib/taos ,日志文件存储在 /var/log/taos 。
若想查看所有数据文件占用的具体大小,可以执行 Shell 指令:`du -sh /var/lib/taos/vnode --exclude='wal'` 来查看。此处排除了 wal 目录,因为在持续写入的情况下,这里大小几乎是固定的,并且每当正常关闭 TDengine 让数据落盘后, wal 目录都会清空。
若想查看单个数据库占用的大小,可在命令行程序 taos 内指定要查看的数据库后执行 `show vgroups;` ,通过得到的 VGroup id 去 /var/lib/taos/vnode 下查看包含的文件夹大小。
若仅仅想查看指定(超级)表的数据块分布及大小,可查看[_block_dist 函数](https://docs.taosdata.com/taos-sql/select/#_block_dist-%E5%87%BD%E6%95%B0)
label: FAQ、教程及其它 label: FAQ 及其他
link:
slug: /train-faq/
type: generated-index
---
title: FAQ 及其他
---
```mdx-code-block
import DocCardList from '@theme/DocCardList';
import {useCurrentSidebarCategory} from '@docusaurus/theme-common';
<DocCardList items={useCurrentSidebarCategory().items}/>
```
\ No newline at end of file
此差异已折叠。
label: TDengine 介绍 label: Introduction
此差异已折叠。
label: 基本概念 label: Concepts
\ No newline at end of file \ No newline at end of file
--- ---
title: 立即开始 title: Get Started
description: " Docker,安装包或使用 apt-get 快速安装 TDengine, 通过命令行程序TAOS CLI和工具 taosdemo 快速体验 TDengine 功能" description: "Install TDengine from Docker image, apt-get or package, and run TAOS CLI and taosBenchmark to experience the features"
--- ---
import Tabs from "@theme/Tabs"; import Tabs from "@theme/Tabs";
...@@ -8,38 +8,38 @@ import TabItem from "@theme/TabItem"; ...@@ -8,38 +8,38 @@ import TabItem from "@theme/TabItem";
import PkgInstall from "./\_pkg_install.mdx"; import PkgInstall from "./\_pkg_install.mdx";
import AptGetInstall from "./\_apt_get_install.mdx"; import AptGetInstall from "./\_apt_get_install.mdx";
## 安装 ## Quick Install
TDengine 完整的软件包包括服务端(taosd)、用于与第三方系统对接并提供RESTful接口的taosAdapter、应用驱动(taosc)、命令行程序 (CLI,taos) 和一些工具软件,目前 2.X 版服务端taosd、taosAdapter 仅在 Linux 系统上安装和运行,后续将支持 Windows、macOS 等系统。应用驱动 taosc 与 TDengine CLI 可以在 Windows 或 Linux 上安装和运行。TDengine 除 RESTful接口外,还提供一些列编程语言的连接器。2.4 之前的版本中,无 taosAdapter,RESTfule 接口均由 taosd 内置的 http 服务提供。 The full package of TDengine includes server(taosd), taosAdapter for connecting with third-party systems and providing RESTful interface, application driver(taosc), command line program(CLI, taos) and some tools. For current version, the server taosd and taosAdapter can only be installed and run on Linux systems, and will support Windows, macOS and other systems in the future. The application driver taosc and TDengine CLI can be installed and run on Windows or Linux. In addition to the RESTful interface, TDengine also provides connectors for a number of programming languages. In versions before 2.4, there is no taosAdapter, and the RESTfule interface is provided by the built-in http service of taosd.
TDengine 支持 X64/ARM64/MIPS64/Alpha64 硬件平台,后续将支持 ARM32、RISC-V 等 CPU 架构。 TDengine supports X64/ARM64/MIPS64/Alpha64 hardware platforms, and will support ARM32, RISC-V and other CPU architectures in the future.
<Tabs defaultValue="apt-get"> <Tabs defaultValue="apt-get">
<TabItem value="docker" label="Docker"> <TabItem value="docker" label="Docker">
如果已经安装了 docker, 只需执行下面的命令。 If docker is already installed on your computer, execute the following command:
```shell ```shell
docker run -d -p 6030-6049:6030-6049 -p 6030-6049:6030-6049/udp tdengine/tdengine docker run -d -p 6030-6049:6030-6049 -p 6030-6049:6030-6049/udp tdengine/tdengine
``` ```
确定该容器已经启动并且在正常运行 Make sure the container is running
```shell ```shell
docker ps docker ps
``` ```
进入该容器并执行 bash Enter into container and execute bash
```shell ```shell
docker exec -it <containrid> bash docker exec -it <containrid> bash
``` ```
然后就可以执行相关的 Linux 命令操作和访问 TDengine Then you can execute the Linux commands and access TDengine.
详细操作方法请参照 [通过 Docker 快速体验 TDengine](/train-faq/docker) For detailed steps, please visit [Experience TDengine via Docker](/train-faq/docker)
:::info :::info
从 2.4.0.10 开始,除 taosd 以外,Docker 镜像还包含:taos、taosAdapter、taosdump、taosBenchmark、TDinsight 安装脚本和示例代码。启动 Docker 容器时,将同时启动 taosAdapter 和 taosd,实现对 RESTful 的支持。 Starting from 2.4.0.10,besides taosd,TDengine docker image includes: taos,taosAdapter,taosdump,taosBenchmark,TDinsight, scripts and sample code. Once the TDengine container is started,it will start both taosAdapter and taosd automatically to support RESTful interface.
::: :::
...@@ -47,66 +47,64 @@ docker exec -it <containrid> bash ...@@ -47,66 +47,64 @@ docker exec -it <containrid> bash
<TabItem value="apt-get" label="apt-get"> <TabItem value="apt-get" label="apt-get">
<AptGetInstall /> <AptGetInstall />
</TabItem> </TabItem>
<TabItem value="pkg" label="安装包"> <TabItem value="pkg" label="Package">
<PkgInstall /> <PkgInstall />
</TabItem> </TabItem>
<TabItem value="src" label="源码"> <TabItem value="src" label="Source Code">
如果您希望对 TDengine 贡献代码或对内部实现感兴趣,请参考我们的 [TDengine GitHub 主页](https://github.com/taosdata/TDengine) 下载源码构建和安装. If you like to check the source code, build the package by youself or contribute to the project, please check [TDengine GitHub Repository](https://github.com/taosdata/TDengine)
下载其他组件、最新 Beta 版及之前版本的安装包,请点击[这里](https://www.taosdata.com/cn/all-downloads/)
</TabItem> </TabItem>
</Tabs> </Tabs>
## 启动 ## Quick Launch
安装后,请使用 `systemctl` 命令来启动 TDengine 的服务进程。 After installation, you can launch the TDengine service by the 'systemctl' command to start 'taosd'.
```bash ```bash
systemctl start taosd systemctl start taosd
``` ```
检查服务是否正常工作 Check if taosd is running
```bash ```bash
systemctl status taosd systemctl status taosd
``` ```
如果 TDengine 服务正常工作,那么您可以通过 TDengine 的命令行程序 `taos` 来访问并体验 TDengine。 If everything is fine,you can run TDengine command line interface `taos` to access TDengine and play around it.
:::info :::info
- systemctl 命令需要 _root_ 权限来运行,如果您非 _root_ 用户,请在命令前添加 sudo 。 - systemctl requires _root_ privileges,if you are not _root_ ,please add sudo before the command.
- 为更好的获得产品反馈,改善产品,TDengine 会采集基本的使用信息,但您可以修改系统配置文件 taos.cfg 里的配置参数 telemetryReporting,将其设为 0,就可将其关闭。 - To get feedback and keep polishing the prodcut, TDengine is colleting some basic usage information, but you can turn it off by setting telemetryReporting to 0 in configuration file taos.cfg.
- TDengine 采用 FQDN(一般就是 hostname)作为节点的 ID,为保证正常运行,需要给运行 taosd 的服务器配置好 FQDN,在 TDengine CLI 或应用运行的机器配置好 DNS 服务或 hosts 文件,保证 FQDN 能够解析。 - TDengine uses FQDN (usually hostname)as the ID for a node. To make system work, you need to configure the FQDN for the server running taosd, and configure the DNS service or hosts file on the the machine where the application or TDengine CLI runs to ensure that the FQDN can be resolved.
- `systemctl stop taosd` 指令在执行后并不会马上停止 TDengine 服务,而是会等待系统中必要的落盘工作正常完成。在数据量很大的情况下,这可能会消耗较长时间。 - `systemctl stop taosd` won't stop the server right away, it will wait until all the data in memory are flushed to disk. It may takes time depending on the cache size.
TDengine 支持在使用 [`systemd`](https://en.wikipedia.org/wiki/Systemd) 做进程服务管理的 Linux 系统上安装,用 `which systemctl` 命令来检测系统中是否存在 `systemd`: TDengine supports the installation on system which runs [`systemd`](https://en.wikipedia.org/wiki/Systemd) for process management,use `which systemctl` to check if the system has `systemd` installed:
```bash ```bash
which systemctl which systemctl
``` ```
如果系统中不支持 `systemd`,也可以用手动运行 `/usr/local/taos/bin/taosd` 方式启动 TDengine 服务。 If the system does not have `systemd`,you can start TDengine manually by executing `/usr/local/taos/bin/taosd`
:::note :::note
## TDengine 命令行 (CLI) ## Command Line Interface
为便于检查 TDengine 的状态,执行各种即席(Ad Hoc)查询,TDengine 提供一命令行应用程序(以下简称为 TDengine CLI) taos。要进入 TDengine 命令行,您只要在安装有 TDengine 的 Linux 终端执行 `taos` 即可。 To manage the TDengine running instance,or execute ad-hoc queries, TDengine provides a Command Line Interface(hereinafter referred to as TDengine CLI) taos. To enter into the interactive CLI,execute `taos` on a Linux terminal where TDengine is installed.
```bash ```bash
taos taos
``` ```
如果连接服务成功,将会打印出欢迎消息和版本信息。如果失败,则会打印错误消息出来(请参考 [FAQ](/train-faq/faq) 来解决终端连接服务端失败的问题)。 TDengine CLI 的提示符号如下 If it connects to the TDengine server successfully, it will print out the version and welcome message. If it fails, it will print out the error message, please check [FAQ](/train-faq/faq) for trouble shooting connection issue. TDengine CLI's prompt is
```cmd ```cmd
taos> taos>
``` ```
在 TDengine CLI中,用户可以通过 SQL 命令来创建/删除数据库、表等,并进行插入查询操作。在终端中运行的 SQL 语句需要以分号结束来运行。示例: Inside TDengine CLI,you can execute SQL commands to create/drop database/table, and run queries. The SQL command must be eneded with a semicolon. For example:
```sql ```sql
create database demo; create database demo;
...@@ -121,52 +119,50 @@ select * from t; ...@@ -121,52 +119,50 @@ select * from t;
2019-07-15 01:00:00.000 | 20 | 2019-07-15 01:00:00.000 | 20 |
Query OK, 2 row(s) in set (0.003128s) Query OK, 2 row(s) in set (0.003128s)
``` ```
Besides executing SQL commands, system administrator can check running status, add/drop user accounts and manage the running instances. TAOS CLI with application driver can be installed and run on either Linux or windows machine. For more details on CLI, please [check here](../reference/taos-shell/).
除执行 SQL 语句外,系统管理员还可以从 TDengine CLI 进行检查系统运行状态、添加删除用户账号等操作。TAOS CLI 连同应用驱动也可以独立安装在 Linux 或 windows 机器上运行,更多细节请参考 [这里](../reference/taos-shell/) ## Experience the blazing fast speed
## 使用 taosBenchmark 体验写入速度
启动 TDengine 的服务,在 Linux 终端执行 `taosBenchmark` (曾命名为 `taosdemo` After TDengine server is running,execute `taosBenchmark`(named as taosdemo before) from a Linux terminal
```bash ```bash
taosBenchmark taosBenchmark
``` ```
This command will create a super table "meters" under database "test". Unde "meters", 10000 tables are created with name from "d0" to "d9999". Each table has 10000 rows and each row has four columns (ts, current, voltage, phase). Time stamp is starting from "2017-07-14 10:40:00 000" to "2017-07-14 10:40:09 999". Each table has tags "location" and "groupId". groupId is set 1 to 10 randomly, and location is set to "beijing" or "shanghai".
该命令将在数据库 test 下面自动创建一张超级表 meters,该超级表下有 1 万张表,表名为 "d0" 到 "d9999",每张表有 1 万条记录,每条记录有 (ts, current, voltage, phase) 四个字段,时间戳从 "2017-07-14 10:40:00 000" 到 "2017-07-14 10:40:09 999",每张表带有标签 location 和 groupId,groupId 被设置为 1 到 10, location 被设置为 "beijing" 或者 "shanghai"。 This command will insert 100 million rows into database quickly. Depends on the hardware configuration, it only takes a dozen seconds for a regular PC server.
这条命令很快完成 1 亿条记录的插入。具体时间取决于硬件性能,即使在一台普通的 PC 服务器往往也仅需十几秒。
taosBenchmark 命令本身带有很多选项,配置表的数目、记录条数等等,您可以设置不同参数进行体验,请执行 `taosBenchmark --help` 详细列出。taosBenchmark 详细使用方法请参照 [如何使用 taosBenchmark 对 TDengine 进行性能测试](https://www.taosdata.com/2021/10/09/3111.html)
## 使用 TDengine CLI 体验查询速度 taosBenchmark provides you command line options and confuguration file to customize the scenarios, like number of tables, number of rows per table, number of columns and more. Please execute `taosBenchmark --help` to list them. For details on running taosBenchmark, please check [reference for taosBenchmark](/reference/taosbenchmark)
使用上述 taosBenchmark 插入数据后,可以在 TDengine CLI 输入查询命令,体验查询速度。 ## Experience query speed
After using taosBenchmark to insert a number of rows data, you can execute queries from TDengine CLI to experience the lightning query speed.
查询超级表下记录总条数 query the total number of rows under super table "meters"
```sql ```sql
taos> select count(*) from test.meters; taos> select count(*) from test.meters;
``` ```
查询 1 亿条记录的平均值、最大值、最小值等: query the average, maximum, minimum of 100 million rows:
```sql ```sql
taos> select avg(current), max(voltage), min(phase) from test.meters; taos> select avg(current), max(voltage), min(phase) from test.meters;
``` ```
查询 location="beijing" 的记录总条数: query the total number of rows with location="beijing":
```sql ```sql
taos> select count(*) from test.meters where location="beijing"; taos> select count(*) from test.meters where location="beijing";
``` ```
查询 groupId=10 的所有记录的平均值、最大值、最小值等: query the average, maximum, minimum of all rows with groupId=10:
```sql ```sql
taos> select avg(current), max(voltage), min(phase) from test.meters where groupId=10; taos> select avg(current), max(voltage), min(phase) from test.meters where groupId=10;
``` ```
对表 d10 按 10s 进行平均值、最大值和最小值聚合统计 query the average, maximum, minimum for table d10 in 10 seconds time interal
```sql ```sql
taos> select avg(current), max(voltage), min(phase) from test.d10 interval(10s); taos> select avg(current), max(voltage), min(phase) from test.d10 interval(10s);
......
可以使用 apt-get 工具从官方仓库安装。 `apt-get` can be used to install TDengine from official package repository.
**安装包仓库** **Package Repository**
``` ```
wget -qO - http://repos.taosdata.com/tdengine.key | sudo apt-key add - wget -qO - http://repos.taosdata.com/tdengine.key | sudo apt-key add -
echo "deb [arch=amd64] http://repos.taosdata.com/tdengine-stable stable main" | sudo tee /etc/apt/sources.list.d/tdengine-stable.list echo "deb [arch=amd64] http://repos.taosdata.com/tdengine-stable stable main" | sudo tee /etc/apt/sources.list.d/tdengine-stable.list
``` ```
如果安装 Beta 版需要安装包仓库 The repository required for installing beta versions can be configured as below:
``` ```
echo "deb [arch=amd64] http://repos.taosdata.com/tdengine-beta beta main" | sudo tee /etc/apt/sources.list.d/tdengine-beta.list echo "deb [arch=amd64] http://repos.taosdata.com/tdengine-beta beta main" | sudo tee /etc/apt/sources.list.d/tdengine-beta.list
``` ```
**使用 apt-get 命令安装** **Install With apt-get**
``` ```
sudo apt-get update sudo apt-get update
...@@ -22,5 +22,5 @@ sudo apt-get install tdengine ...@@ -22,5 +22,5 @@ sudo apt-get install tdengine
``` ```
:::tip :::tip
apt-get 方式只适用于 Debian 或 Ubuntu 系统 `apt-get` can only be used on Debian or Ubuntu Linux.
:::: ::::
```c title="原生连接" ```c title="Native Connection"
{{#include docs-examples/c/connect_example.c}} {{#include docs-examples/c/connect_example.c}}
``` ```
```csharp title="原生连接" ```csharp title="Native Connection"
{{#include docs-examples/csharp/ConnectExample.cs}} {{#include docs-examples/csharp/ConnectExample.cs}}
``` ```
:::info :::info
C# 连接器目前只支持原生连接。 C# connector supports only native connection for now.
::: :::
\ No newline at end of file
#### 使用数据库访问统一接口 #### Unified Database Access Interface
```go title="原生连接"
```go title="Native Connection"
{{#include docs-examples/go/connect/cgoexample/main.go}} {{#include docs-examples/go/connect/cgoexample/main.go}}
``` ```
```go title="REST 连接"
```go title="REST Connection"
{{#include docs-examples/go/connect/restexample/main.go}} {{#include docs-examples/go/connect/restexample/main.go}}
``` ```
#### 使用高级封装
也可以使用 driver-go 的 af 包建立连接。这个模块封装了 TDengine 的高级功能, 如:参数绑定、订阅等。
```go title="使用 af 包建立原生连接" #### Advanced Features
{{#include docs-examples/go/connect/afconn/main.go}}
```
The af package of driver-go can also be used to establish connection, with this way some advanced features of TDengine, like parameter binding and subscription, can be used.
```go title="Establish native connection using af package"
{{#include docs-examples/go/connect/afconn/main.go}}
```
```java title="原生连接" ```java title="Native Connection"
{{#include docs-examples/java/src/main/java/com/taos/example/JNIConnectExample.java}} {{#include docs-examples/java/src/main/java/com/taos/example/JNIConnectExample.java}}
``` ```
```java title="REST 连接"
```java title="REST Connection"
{{#include docs-examples/java/src/main/java/com/taos/example/RESTConnectExample.java:main}} {{#include docs-examples/java/src/main/java/com/taos/example/RESTConnectExample.java:main}}
``` ```
使用REST 连接时,如果查询数据量比较大,还可开启批量拉取功能。
```java title="开启批量拉取功能" {4} When using REST connection, the feature of bulk pulling can be enabled if the size of resulting data set is huge.
```java title="Enable Bulk Pulling" {4}
{{#include docs-examples/java/src/main/java/com/taos/example/WSConnectExample.java:main}} {{#include docs-examples/java/src/main/java/com/taos/example/WSConnectExample.java:main}}
``` ```
更多连接参数配置,参考[Java 连接器](/reference/connector/java)
\ No newline at end of file More configuration about connection,please refer to [Java Connector](/reference/connector/java)
```js title="原生连接" ```js title="Native Connection"
{{#include docs-examples/node/nativeexample/connect.js}} {{#include docs-examples/node/nativeexample/connect.js}}
``` ```
```js title="REST 连接"
```js title="REST Connection"
{{#include docs-examples/node/restexample/connect.js}} {{#include docs-examples/node/restexample/connect.js}}
``` ```
\ No newline at end of file
```python title="原生连接" ```python title="Native Connection"
{{#include docs-examples/python/connect_exmaple.py}} {{#include docs-examples/python/connect_exmaple.py}}
``` ```
\ No newline at end of file
```r title="原生连接" ```r title="Native Connection"
{{#include docs-examples/R/connect_native.r:demo}} {{#include docs-examples/R/connect_native.r:demo}}
``` ```
\ No newline at end of file
```rust title="原生连接/REST 连接" ```rust title="Native Connection/REST Connection"
{{#include docs-examples/rust/nativeexample/examples/connect.rs}} {{#include docs-examples/rust/nativeexample/examples/connect.rs}}
``` ```
:::note :::note
对于 Rust 连接器, 连接方式的不同只体现在使用的特性不同。如果启用了 "rest" 特性,那么只有 RESTful 的实现会被编译进来。 For Rust connector, the connection depends on the feature being used. If "rest" feature is enabled, then only the implementation for "rest" is compiled and packaged.
::: :::
--- ---
title: 建立连接 sidebar_label: Connection
description: "本节介绍如何使用连接器建立与 TDengine 的连接,给出连接器安装、连接的简单说明。" title: Connect to TDengine
description: "This document explains how to establish connection to TDengine, and briefly introduce how to install and use TDengine connectors."
--- ---
import Tabs from "@theme/Tabs"; import Tabs from "@theme/Tabs";
import TabItem from "@theme/TabItem"; import TabItem from "@theme/TabItem";
import ConnJava from "./_connect_java.mdx"; import ConnJava from "./\_connect_java.mdx";
import ConnGo from "./_connect_go.mdx"; import ConnGo from "./\_connect_go.mdx";
import ConnRust from "./_connect_rust.mdx"; import ConnRust from "./\_connect_rust.mdx";
import ConnNode from "./_connect_node.mdx"; import ConnNode from "./\_connect_node.mdx";
import ConnPythonNative from "./_connect_python.mdx"; import ConnPythonNative from "./\_connect_python.mdx";
import ConnCSNative from "./_connect_cs.mdx"; import ConnCSNative from "./\_connect_cs.mdx";
import ConnC from "./_connect_c.mdx"; import ConnC from "./\_connect_c.mdx";
import ConnR from "./_connect_r.mdx"; import ConnR from "./\_connect_r.mdx";
import InstallOnWindows from "../../14-reference/03-connector/_linux_install.mdx"; import InstallOnWindows from "../../14-reference/03-connector/\_linux_install.mdx";
import InstallOnLinux from "../../14-reference/03-connector/_windows_install.mdx"; import InstallOnLinux from "../../14-reference/03-connector/\_windows_install.mdx";
import VerifyLinux from "../../14-reference/03-connector/_verify_linux.mdx"; import VerifyLinux from "../../14-reference/03-connector/\_verify_linux.mdx";
import VerifyWindows from "../../14-reference/03-connector/_verify_windows.mdx"; import VerifyWindows from "../../14-reference/03-connector/\_verify_windows.mdx";
TDengine 提供 REST API,容许在任何平台的任何应用程序通过它访问 TDengine 运行实例,详细介绍请看 [REST API](/reference/rest-api/)。除 REST API 之外,TDengine 还提供多种编程语言的连接器方便用户开发应用程序,其中包括 C/C++、Java、Python、Go、Node.js、C# 等。 本节介绍如何使用连接器建立与 TDengine 的连接,给出连接器安装、连接的简单说明。关于各连接器的详细功能说明,请查看[连接器](https://docs.taosdata.com/reference/connector/) Any application programs running on any kind of platforms can access TDengine through the REST API provided by TDengine. For the details, please refer to [REST API](/reference/rest-api/). Besides, application programs can use the connectors of multiple programming languages to access TDengine, including C/C++, Java, Python, Go, Node.js, C#, and Rust. This chapter describes how to establish connection to TDengine and briefly introduce how to install and use connectors. For details about the connectors, please refer to [Connectors](/reference/connector/)
## 连接器建立连接的方式 ## Establish Connection
连接器建立连接的方式,TDengine 提供两种: There are two ways for a connector to establish connections to TDengine:
1. 通过 taosAdapter 组件提供的 REST API 建立与 taosd 的连接,这种连接方式下文中简称"REST 连接“ 1. Connection through the REST API provided by taosAdapter component, this way is called "REST connection" hereinafter.
2. 通过客户端驱动程序 taosc 直接与服务端程序 taosd 建立连接,这种连接方式下文中简称“原生连接”。 2. Connection through the TDengine application driver taosc, this way is called "Native connection" hereinafter.
无论使用何种方式建立连接,连接器都提供了相同或相似的 API 操作数据库,都可以执行 SQL 语句,只是初始化连接的方式稍有不同,用户在使用上不会感到什么差别。 Either way, same or similar APIs are provided by connectors to access database or execute SQL statements, no obvious difference can be observed.
关键不同点在于 Key differences
1. 使用 REST 连接,用户无需安装客户端驱动程序 taosc,具有跨平台易用的优势,但性能要下降 30%左右。 1. With REST connection, it's not necessary to install TDengine application driver taosc, it's more friendly for cross-platform with the cost of 30% performance downgrade. When taosc has an upgrade, application does not need to make changes.
2. 使用原生连接可以体验 TDengine 的全部功能,如[参数绑定接口](/reference/connector/cpp#参数绑定-api)[订阅](reference/connector/cpp#数据订阅接口)等等。 2. With native connection, full compatibility of TDengine can be utilized, like [Parameter Binding](/reference/connector/cpp#Parameter Binding-api), [Subscription](reference/connector/cpp#Subscription), etc. But taosc has to be installed, some platforms may not be supported.
## 安装客户端驱动 taosc ## Install Client Driver taosc
如果选择原生连接,而且应用程序不在 TDengine 同一台服务器上运行,你需要先安装客户端驱动,否则可以跳过此一步。为避免客户端驱动和服务端不兼容,请使用一致的版本。 If choosing to use native connection and the application is not on the same host as TDengine server, TDengine application driver taosc needs to be installed on the host where the application is. If choosing to use REST connection or the application is on the same host as server side, this step can be skipped. It's better to use same version of taosc as the server.
### 安装步骤 ### Install
<Tabs defaultValue="linux" groupId="os"> <Tabs defaultValue="linux" groupId="os">
<TabItem value="linux" label="Linux"> <TabItem value="linux" label="Linux">
...@@ -49,9 +50,9 @@ TDengine 提供 REST API,容许在任何平台的任何应用程序通过它 ...@@ -49,9 +50,9 @@ TDengine 提供 REST API,容许在任何平台的任何应用程序通过它
</TabItem> </TabItem>
</Tabs> </Tabs>
### 安装验证 ### Verify
以上安装和配置完成后,并确认 TDengine 服务已经正常启动运行,此时可以执行安装包里带有的 TDengine 命令行程序 taos 进行登录。 After the above installation and configuration are done and making sure TDengine service is already started and in service, the TDengine command line interface `taos` can be launched to access TDengine.以
<Tabs defaultValue="linux" groupId="os"> <Tabs defaultValue="linux" groupId="os">
<TabItem value="linux" label="Linux"> <TabItem value="linux" label="Linux">
...@@ -62,12 +63,12 @@ TDengine 提供 REST API,容许在任何平台的任何应用程序通过它 ...@@ -62,12 +63,12 @@ TDengine 提供 REST API,容许在任何平台的任何应用程序通过它
</TabItem> </TabItem>
</Tabs> </Tabs>
## 安装连接器 ## Install Connectors
<Tabs groupId="lang"> <Tabs groupId="lang">
<TabItem label="Java" value="java"> <TabItem label="Java" value="java">
如果使用 maven 管理项目,只需在 pom.xml 中加入以下依赖。 If `maven` is used to manage the projects, what needs to be done is only adding below dependency in `pom.xml`.
```xml ```xml
<dependency> <dependency>
...@@ -80,13 +81,13 @@ TDengine 提供 REST API,容许在任何平台的任何应用程序通过它 ...@@ -80,13 +81,13 @@ TDengine 提供 REST API,容许在任何平台的任何应用程序通过它
</TabItem> </TabItem>
<TabItem label="Python" value="python"> <TabItem label="Python" value="python">
使用 `pip` 从 PyPI 安装: Install from PyPI using `pip`:
``` ```
pip install taospy pip install taospy
``` ```
从 Git URL 安装: Install from Git URL:
``` ```
pip install git+https://github.com/taosdata/taos-connector-python.git pip install git+https://github.com/taosdata/taos-connector-python.git
...@@ -95,7 +96,7 @@ pip install git+https://github.com/taosdata/taos-connector-python.git ...@@ -95,7 +96,7 @@ pip install git+https://github.com/taosdata/taos-connector-python.git
</TabItem> </TabItem>
<TabItem label="Go" value="go"> <TabItem label="Go" value="go">
编辑 `go.mod` 添加 `driver-go` 依赖即可。 Just need to add `driver-go` dependency in `go.mod` .
```go-mod title=go.mod ```go-mod title=go.mod
module goexample module goexample
...@@ -106,14 +107,14 @@ require github.com/taosdata/driver-go/v2 develop ...@@ -106,14 +107,14 @@ require github.com/taosdata/driver-go/v2 develop
``` ```
:::note :::note
driver-go 使用 cgo 封装了 taosc 的 API。cgo 需要使用 gcc 编译 C 的源码。因此需要确保你的系统上有 gcc。 `driver-go` uses `cgo` to wrap the APIs provided by taosc, while `cgo` needs `gcc` to compile source code in C language, so please make sure you have proper `gcc` on your system.
::: :::
</TabItem> </TabItem>
<TabItem label="Rust" value="rust"> <TabItem label="Rust" value="rust">
编辑 `Cargo.toml` 添加 `libtaos` 依赖即可。 Just need to add `libtaos` dependency in `Cargo.toml`.
```toml title=Cargo.toml ```toml title=Cargo.toml
[dependencies] [dependencies]
...@@ -121,7 +122,7 @@ libtaos = { version = "0.4.2"} ...@@ -121,7 +122,7 @@ libtaos = { version = "0.4.2"}
``` ```
:::info :::info
Rust 连接器通过不同的特性区分不同的连接方式。如果要建立 REST 连接,需要开启 `rest` 特性: Rust connector uses different features to distinguish the way to establish connection. To establish REST connection, please enable `rest` feature.
```toml ```toml
libtaos = { version = "*", features = ["rest"] } libtaos = { version = "*", features = ["rest"] }
...@@ -132,28 +133,28 @@ libtaos = { version = "*", features = ["rest"] } ...@@ -132,28 +133,28 @@ libtaos = { version = "*", features = ["rest"] }
</TabItem> </TabItem>
<TabItem label="Node.js" value="node"> <TabItem label="Node.js" value="node">
Node.js 连接器通过不同的包提供不同的连接方式。 Node.js connector provides different ways of establishing connections by providing different packages.
1. 安装 Node.js 原生连接器 1. Install Node.js Native Connector
``` ```
npm i td2.0-connector npm i td2.0-connector
``` ```
:::note :::note
推荐 Node 版本大于等于 `node-v12.8.0` 小于 `node-v13.0.0` It's recommend to use Node whose version is between `node-v12.8.0` and `node-v13.0.0`.
::: :::
2. 安装 Node.js REST 连接器
``` 2. Install Node.js REST Connector
npm i td2.0-rest-connector
``` ```
npm i td2.0-rest-connector
```
</TabItem> </TabItem>
<TabItem label="C#" value="csharp"> <TabItem label="C#" value="csharp">
编辑项目配置文件中添加 [TDengine.Connector](https://www.nuget.org/packages/TDengine.Connector/) 的引用即可: Just need to add the reference to [TDengine.Connector](https://www.nuget.org/packages/TDengine.Connector/) in the project configuration file.
```xml title=csharp.csproj {12} ```xml title=csharp.csproj {12}
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
...@@ -173,22 +174,22 @@ Node.js 连接器通过不同的包提供不同的连接方式。 ...@@ -173,22 +174,22 @@ Node.js 连接器通过不同的包提供不同的连接方式。
</Project> </Project>
``` ```
也可通过 dotnet 命令添加: Or add by `dotnet` command.
``` ```
dotnet add package TDengine.Connector dotnet add package TDengine.Connector
``` ```
:::note :::note
以下示例代码,均基于 dotnet6.0,如果使用其它版本,可能需要做适当调整。 The sample code below are based on dotnet6.0, they may need to be adjusted if your dotnet version is not exactly same.
::: :::
</TabItem> </TabItem>
<TabItem label="R" value="r"> <TabItem label="R" value="r">
1. 下载 [taos-jdbcdriver-version-dist.jar](https://repo1.maven.org/maven2/com/taosdata/jdbc/taos-jdbcdriver/2.0.38/) 1. Download [taos-jdbcdriver-version-dist.jar](https://repo1.maven.org/maven2/com/taosdata/jdbc/taos-jdbcdriver/2.0.38/).
2. 安装 R 的依赖包`RJDBC` 2. Install the dependency package `RJDBC`
```R ```R
install.packages("RJDBC") install.packages("RJDBC")
...@@ -197,15 +198,15 @@ install.packages("RJDBC") ...@@ -197,15 +198,15 @@ install.packages("RJDBC")
</TabItem> </TabItem>
<TabItem label="C" value="c"> <TabItem label="C" value="c">
如果已经安装了 TDengine 服务端软件或 TDengine 客户端驱动 taosc, 那么已经安装了 C 连接器,无需额外操作。 If the client driver taosc is already installed, then the C connector is already available.
<br/> <br/>
</TabItem> </TabItem>
</Tabs> </Tabs>
## 建立连接 ## Establish Connection
在执行这一步之前,请确保有一个正在运行的,且可以访问到的 TDengine,而且服务端的 FQDN 配置正确。以下示例代码,都假设 TDengine 安装在本机,且 FQDN(默认 localhost) 和 serverPort(默认 6030) 都使用默认配置。 Prior to establishing connection, please make sure TDengine is already running and accessible. The following sample code assumes TDengine is running on the same host as the client program, with FQDN configured to "localhost" and serverPort configured to "6030".
<Tabs groupId="lang" defaultValue="java"> <Tabs groupId="lang" defaultValue="java">
<TabItem label="Java" value="java"> <TabItem label="Java" value="java">
...@@ -235,6 +236,6 @@ install.packages("RJDBC") ...@@ -235,6 +236,6 @@ install.packages("RJDBC")
</Tabs> </Tabs>
:::tip :::tip
如果建立连接失败,大部分情况下是 FQDN 或防火墙的配置不正确,详细的排查方法请看[《常见问题及反馈》](https://docs.taosdata.com/train-faq/faq)中的“遇到错误 Unable to establish connection, 我怎么办?” If the connection fails, in most cases it's caused by improper configuration for FQDN or firewall. Please refer to the section "Unable to establish connection" in [FAQ](https://docs.taosdata.com/train-faq/faq).
::: :::
--- ---
slug: /model slug: /model
title: TDengine 数据建模 title: Data Model
--- ---
TDengine 采用类关系型数据模型,需要建库、建表。因此对于一个具体的应用场景,需要考虑库、超级表和普通表的设计。本节不讨论细致的语法规则,只介绍概念。 The data model employed by TDengine is similar to relational database, you need to create databases and tables. For a specific application, the design of databases, STables (abbreviated for super table), and tables need to be considered. This chapter will explain the big picture without syntax details.
关于数据建模请参考[视频教程](https://www.taosdata.com/blog/2020/11/11/1945.html)。 ## Create Database
## 创建库 The characteristics of data from different data collection points may be different, such as collection frequency, days to keep, number of replicas, data block size, whether it's allowed to update data, etc. For TDengine to operate with the best performance, it's strongly suggested to put the data with different characteristics into different databases because different storage policy can be set for each database. When creating a database, there are a lot of parameters that can be configured, such as the days to keep data, the number of replicas, the number of memory blocks, time precision, the minimum and maximum number of rows in each data block, compress or not, the time range of the data in single data file, etc. Below is an example of the SQL statement for creating a database.
不同类型的数据采集点往往具有不同的数据特征,包括数据采集频率的高低,数据保留时间的长短,副本的数目,数据块的大小,是否允许更新数据等等。为了在各种场景下 TDengine 都能最大效率的工作,TDengine 建议将不同数据特征的表创建在不同的库里,因为每个库可以配置不同的存储策略。创建一个库时,除 SQL 标准的选项外,还可以指定保留时长、副本数、内存块个数、时间精度、文件块里最大最小记录条数、是否压缩、一个数据文件覆盖的天数等多种参数。比如:
```sql ```sql
CREATE DATABASE power KEEP 365 DAYS 10 BLOCKS 6 UPDATE 1; CREATE DATABASE power KEEP 365 DAYS 10 BLOCKS 6 UPDATE 1;
``` ```
上述语句将创建一个名为 power 的库,这个库的数据将保留 365 天(超过 365 天将被自动删除),每 10 天一个数据文件,内存块数为 6,允许更新数据。详细的语法及参数请见 [数据库管理](/taos-sql/database) 章节。 In the above SQL statement, a database named "power" will be created, the data in it will be kept for 365 days, which means the data older than 365 days will be deleted automatically, a new data file will be created every 10 days, the number of memory blocks is 6, data is allowed to be updated. For more details please refer to [Database](/taos-sql/database).
创建库之后,需要使用 SQL 命令 `USE` 将当前库切换过来,例如: After creating a database, the current database in use can be switched using SQL command `USE`, for example below SQL statement switches the current database to `power`. Without current database specified, table name must be preceded with the corresponding database name.
```sql ```sql
USE power; USE power;
``` ```
将当前连接里操作的库换为 power,否则对具体表操作前,需要使用“库名.表名”来指定库的名字。
:::note :::note
- 任何一张表或超级表必须属于某个库,在创建表之前,必须先创建库。 - Any table or STable must belong to a database. To create a table or STable, the database it belongs to must be ready.
- 处于两个不同库的表是不能进行 JOIN 操作的。 - JOIN operation can't be performed tables from two different databases.
- 创建并插入记录、查询历史记录的时候,均需要指定时间戳。 - Timestamp needs to be specified when inserting rows or querying historical rows.
::: :::
## 创建超级表 ## Create STable
一个物联网系统,往往存在多种类型的设备,比如对于电网,存在智能电表、变压器、母线、开关等等。为便于多表之间的聚合,使用 TDengine, 需要对每个类型的数据采集点创建一个超级表。以[表 1](/tdinternal/arch#model_table1) 中的智能电表为例,可以使用如下的 SQL 命令创建超级表: In a time-series application, there may be multiple kinds of data collection points. For example, in the electrical power system there are meters, transformers, bus bars, switches, etc. For easy and efficient aggregation of multiple tables, one STable needs to be created for each kind of data collection point. For example, for the meters in [table 1](/tdinternal/arch#model_table1), below SQL statement can be used to create the super table.
```sql ```sql
CREATE STABLE meters (ts timestamp, current float, voltage int, phase float) TAGS (location binary(64), groupId int); CREATE STable meters (ts timestamp, current float, voltage int, phase float) TAGS (location binary(64), groupId int);
``` ```
:::note :::note
这一指令中的 STABLE 关键字,在 2.0.15 之前的版本中需写作 TABLE 。 If you are using versions prior to 2.0.15, the `STable` keyword needs to be replaced with `TABLE`.
::: :::
与创建普通表一样,创建超级表时,需要提供表名(示例中为 meters),表结构 Schema,即数据列的定义。第一列必须为时间戳(示例中为 ts),其他列为采集的物理量(示例中为 current, voltage, phase),数据类型可以为整型、浮点型、字符串等。除此之外,还需要提供标签的 schema (示例中为 location, groupId),标签的数据类型可以为整型、浮点型、字符串等。采集点的静态属性往往可以作为标签,比如采集点的地理位置、设备型号、设备组 ID、管理员 ID 等等。标签的 schema 可以事后增加、删除、修改。具体定义以及细节请见 [TAOS SQL 的超级表管理](/taos-sql/stable) 章节。 Similar to creating a regular table, when creating a STable, name and schema need to be provided too. In the STable schema, the first column must be timestamp (like ts in the example), and other columns (like current, voltage and phase in the example) are the data collected. The type of a column can be integer, float, double, string ,etc. Besides, the schema for tags need to be provided, like location and groupId in the example. The type of a tag can be integer, float, string, etc. The static properties of a data collection point can be defined as tags, like the location, device type, device group ID, manager ID, etc. Tags in the schema can be added, removed or updated. Please refer to [STable](/taos-sql/stable) for more details.
每一种类型的数据采集点需要建立一个超级表,因此一个物联网系统,往往会有多个超级表。对于电网,我们就需要对智能电表、变压器、母线、开关等都建立一个超级表。在物联网中,一个设备就可能有多个数据采集点(比如一台风力发电的风机,有的采集点采集电流、电压等电参数,有的采集点采集温度、湿度、风向等环境参数),这个时候,对这一类型的设备,需要建立多张超级表。 For each kind of data collection points, a corresponding STable must be created. There may be man y STables in an application. For electrical power system, we need to create a STable respectively for meters, transformers, busbars, switches. There may be multiple kinds of data collection points on a single device, for example there may be one data collection point for electrical data like current and voltage and another point for environmental data like temperature, humidity and wind direction, multiple STables are required for such kind of device.
一张超级表最多容许 4096 列 (在 2.1.7.0 版本之前,列数限制为 1024 列),如果一个采集点采集的物理量个数超过 4096,需要建多张超级表来处理。一个系统可以有多个 DB,一个 DB 里可以有一到多个超级表。 At most 4096 (or 1024 prior to version 2.1.7.0) columns are allowed in a STable. If there are more than 4096 of metrics to bo collected for a data collection point, multiple STables are required for such kind of data collection point. There can be multiple databases in system, while one or more STables can exist in a database.
## 创建表 ## Create Table
TDengine 对每个数据采集点需要独立建表。与标准的关系型数据库一样,一张表有表名,Schema,但除此之外,还可以带有一到多个标签。创建时,需要使用超级表做模板,同时指定标签的具体值。以[表 1](/tdinternal/arch#model_table1)中的智能电表为例,可以使用如下的 SQL 命令建表: A specific table needs to be created for each data collection point. Similar to RDBMS, table name and schema are required to create a table. Beside, one or more tags can be created for each table. To create a table, a STable needs to be used as template and the values need to be specified for the tags. For example, for the meters in [Table 1](/tdinternal/arch#model_table1), the table can be created using below SQL statement.
```sql ```sql
CREATE TABLE d1001 USING meters TAGS ("Beijing.Chaoyang", 2); CREATE TABLE d1001 USING meters TAGS ("Beijing.Chaoyang", 2);
``` ```
其中 d1001 是表名,meters 是超级表的表名,后面紧跟标签 Location 的具体标签值 ”Beijing.Chaoyang",标签 groupId 的具体标签值 2。虽然在创建表时,需要指定标签值,但可以事后修改。详细细则请见 [TAOS SQL 的表管理](/taos-sql/table) 章节。 In the above SQL statement, "d1001" is the table name, "meters" is the STable name, followed by the value of tag "Location" and the value of tag "groupId", which are "Beijing.Chaoyang" and "2" respectively in the example. The tag values can be updated after the table is created. Please refer to [Tables](/taos-sql/table) for details.
:::warning In TDengine system, it's recommended to create a table for a data collection point via STable. Table created via STable is called subtable in some parts of TDengine document. All SQL commands applied on regular table can be applied on subtable.
目前 TDengine 没有从技术层面限制使用一个 database (db1) 的超级表作为模板建立另一个 database (db2) 的子表,后续会禁止这种用法,不建议使用这种方法建表。
::: :::warning
It's not recommended to create a table in a database while using a STable from another database as template.
TDengine 建议将数据采集点的全局唯一 ID 作为表名(比如设备序列号)。但对于有的场景,并没有唯一的 ID,可以将多个 ID 组合成一个唯一的 ID。不建议将具有唯一性的 ID 作为标签值。 :::tip
It's suggested to use the global unique ID of a data collection point as the table name, for example the device serial number. If there isn't such a unique ID, multiple IDs that are not global unique can be combined to form a global unique ID. It's not recommended to use a global unique ID as tag value.
### 自动建表 ## Create Table Automatically
在某些特殊场景中,用户在写数据时并不确定某个数据采集点的表是否存在,此时可在写入数据时使用自动建表语法来创建不存在的表,若该表已存在则不会建立新表且后面的 USING 语句被忽略。比如: In some circumstances, it's not sure whether the table already exists when inserting rows. The table can be created automatically using the SQL statement below, and nothing will happen if the table already exist.
```sql ```sql
INSERT INTO d1001 USING meters TAGS ("Beijng.Chaoyang", 2) VALUES (now, 10.2, 219, 0.32); INSERT INTO d1001 USING meters TAGS ("Beijng.Chaoyang", 2) VALUES (now, 10.2, 219, 0.32);
``` ```
上述 SQL 语句将记录`(now, 10.2, 219, 0.32)`插入表 d1001。如果表 d1001 还未创建,则使用超级表 meters 做模板自动创建,同时打上标签值 `"Beijing.Chaoyang", 2`。 In the above SQL statement, a row with value `(now, 10.2, 219, 0.32)` will be inserted into table "d1001". If table "d1001" doesn't exist, it will be created automatically using STable "meters" as template with tag value `"Beijing.Chaoyang", 2`.
关于自动建表的详细语法请参见 [插入记录时自动建表](/taos-sql/insert#插入记录时自动建表) 章节。 For more details please refer to [Create Table Automatically](/taos-sql/insert#automatically-create-table-when-inserting).
## 多列模型 vs 单列模型 ## Single Column vs Multiple Column
TDengine 支持多列模型,只要物理量是一个数据采集点同时采集的(时间戳一致),这些量就可以作为不同列放在一张超级表里。但还有一种极限的设计,单列模型,每个采集的物理量都单独建表,因此每种类型的物理量都单独建立一超级表。比如电流、电压、相位,就建三张超级表。 Multiple columns data model is supported in TDengine. As long as multiple metrics are collected by same data collection point at same time, i.e. the timestamp are identical, these metrics can be put in single stable as columns. However, there is another kind of design, i.e. single column data model, a table is created for each metric, which means a STable is required for each kind of metric. For example, 3 STables are required for current, voltage and phase.
TDengine 建议尽可能采用多列模型,因为插入效率以及存储效率更高。但对于有些场景,一个采集点的采集量的种类经常变化,这个时候,如果采用多列模型,就需要频繁修改超级表的结构定义,让应用变的复杂,这个时候,采用单列模型会显得更简单。 It's recommended to use multiple column data model as much as possible because it's better in the performance of inserting or querying rows. In some cases, however, the metrics to be collected vary frequently and correspondingly the STable schema needs to be changed frequently too. In such case, it's more convenient to use single column data model.
--- ---
title: SQL 写入 sidebar_label: SQL
title: Insert Using SQL
--- ---
import Tabs from "@theme/Tabs"; import Tabs from "@theme/Tabs";
...@@ -19,52 +20,53 @@ import CsStmt from "./_cs_stmt.mdx"; ...@@ -19,52 +20,53 @@ import CsStmt from "./_cs_stmt.mdx";
import CSQL from "./_c_sql.mdx"; import CSQL from "./_c_sql.mdx";
import CStmt from "./_c_stmt.mdx"; import CStmt from "./_c_stmt.mdx";
## SQL 写入简介 ## Introduction
应用通过连接器执行 INSERT 语句来插入数据,用户还可以通过 TAOS Shell,手动输入 INSERT 语句插入数据。 Application program can execute `INSERT` statement through connectors to insert rows. TAOS CLI can be launched manually to insert data too.
### 一次写入一条 ### Insert Single Row
下面这条 INSERT 就将一条记录写入到表 d1001 中:
Below SQL statement is used to insert one row into table "d1001".
```sql ```sql
INSERT INTO d1001 VALUES (1538548685000, 10.3, 219, 0.31); INSERT INTO d1001 VALUES (1538548685000, 10.3, 219, 0.31);
``` ```
### 一次写入多条 ### Insert Multiple Rows
TDengine 支持一次写入多条记录,比如下面这条命令就将两条记录写入到表 d1001 中: Multiple rows can be inserted in single SQL statement. Below example inserts 2 rows into table "d1001".
```sql ```sql
INSERT INTO d1001 VALUES (1538548684000, 10.2, 220, 0.23) (1538548696650, 10.3, 218, 0.25); INSERT INTO d1001 VALUES (1538548684000, 10.2, 220, 0.23) (1538548696650, 10.3, 218, 0.25);
``` ```
### 一次写入多表 ### Insert into Multiple Tables
TDengine 也支持一次向多个表写入数据,比如下面这条命令就向 d1001 写入两条记录,向 d1002 写入一条记录: Data can be inserted into multiple tables in same SQL statement. Below example inserts 2 rows into table "d1001" and 1 row into table "d1002".
```sql ```sql
INSERT INTO d1001 VALUES (1538548685000, 10.3, 219, 0.31) (1538548695000, 12.6, 218, 0.33) d1002 VALUES (1538548696800, 12.3, 221, 0.31); INSERT INTO d1001 VALUES (1538548685000, 10.3, 219, 0.31) (1538548695000, 12.6, 218, 0.33) d1002 VALUES (1538548696800, 12.3, 221, 0.31);
``` ```
详细的 SQL INSERT 语法规则参考 [TAOS SQL 的数据写入](/taos-sql/insert)。 For more details about `INSERT` please refer to [INSERT](/taos-sql/insert).
:::info :::info
- 要提高写入效率,需要批量写入。一批写入的记录条数越多,插入效率就越高。但一条记录不能超过 16K,一条 SQL 语句总长度不能超过 1M 。 - Inserting in batch can gain better performance. Normally, the higher the batch size, the better the performance. Please be noted each single row can't exceed 16K bytes and each single SQL statement can't exceed 1M bytes.
- TDengine 支持多线程同时写入,要进一步提高写入速度,一个客户端需要打开 20 个以上的线程同时写。但线程数达到一定数量后,无法再提高,甚至还会下降,因为线程频繁切换,带来额外开销。 - Inserting with multiple threads can gain better performance too. However, depending on the system resources on the application side and the server side, with the number of inserting threads grows to a specific point, the performance may drop instead of growing. The proper number of threads need to be tested in a specific environment to find the best number.
::: :::
:::warning :::warning
- 对同一张表,如果新插入记录的时间戳已经存在,默认情形下(UPDATE=0)新记录将被直接抛弃,也就是说,在一张表里,时间戳必须是唯一的。如果应用自动生成记录,很有可能生成的时间戳是一样的,这样,成功插入的记录条数会小于应用插入的记录条数。如果在创建数据库时使用了 UPDATE 1 选项,插入相同时间戳的新记录将覆盖原有记录。 - If the timestamp for the row to be inserted already exists in the table, the behavior depends on the value of parameter `UPDATE`. If it's set to 0 (also the default value), the row will be discarded. If it's set to 1, the new values will override the old values for the same row.
- 写入的数据的时间戳必须大于当前时间减去配置参数 keep 的时间。如果 keep 配置为 3650 天,那么无法写入比 3650 天还早的数据。写入数据的时间戳也不能大于当前时间加配置参数 days。如果 days 为 2,那么无法写入比当前时间还晚 2 天的数据。 - The timestamp to be inserted must be newer than the timestamp of subtracting current time by the parameter `KEEP`. If `KEEP` is set to 3650 days, then the data older than 3650 days ago can't be inserted. The timestamp to be inserted can't be newer than the timestamp of current time plus parameter `DAYS`. If `DAYS` is set to 2, the data newer than 2 days later can't be inserted.
::: :::
## 示例程序 ## Examples
### 普通 SQL 写入 ### Insert Using SQL
<Tabs defaultValue="java" groupId="lang"> <Tabs defaultValue="java" groupId="lang">
<TabItem label="Java" value="java"> <TabItem label="Java" value="java">
...@@ -92,16 +94,16 @@ INSERT INTO d1001 VALUES (1538548685000, 10.3, 219, 0.31) (1538548695000, 12.6, ...@@ -92,16 +94,16 @@ INSERT INTO d1001 VALUES (1538548685000, 10.3, 219, 0.31) (1538548695000, 12.6,
:::note :::note
1. 无论 RESTful 方式建立连接还是本地驱动方式建立连接,以上示例代码都能正常工作。 1. With either native connection or REST connection, the above samples can work well.
2. 唯一需要注意的是:由于 RESTful 接口无状态, 不能使用 `use db` 语句来切换数据库, 所以在上面示例中使用了`dbName.tbName`指定表名。 2. Please be noted that `use db` can't be used with REST connection because REST connection is stateless, so in the samples `dbName.tbName` is used to specify the table name.
::: :::
### 参数绑定写入 ### Insert with Parameter Binding
TDengine 也提供了支持参数绑定的 Prepare API,与 MySQL 类似,这些 API 目前也仅支持用问号 `?` 来代表待绑定的参数。从 2.1.1.0 和 2.1.2.0 版本开始,TDengine 大幅改进了参数绑定接口对数据写入(INSERT)场景的支持。这样在通过参数绑定接口写入数据时,就避免了 SQL 语法解析的资源消耗,从而在绝大多数情况下显著提升写入性能。 TDengine also provides Prepare API that support parameter binding. Similar to MySQL, only `?` can be used in these APIs to represent the parameters to bind. From version 2.1.1.0 and 2.1.2.0, parameter binding support for inserting data has been improved significantly to improve the insert performance by avoiding the cost of parsing SQL statements.
需要注意的是,只有使用原生连接的连接器,才能使用参数绑定功能。 Parameter binding is available only with native connection.
<Tabs defaultValue="java" groupId="lang"> <Tabs defaultValue="java" groupId="lang">
<TabItem label="Java" value="java"> <TabItem label="Java" value="java">
...@@ -126,4 +128,3 @@ TDengine 也提供了支持参数绑定的 Prepare API,与 MySQL 类似,这 ...@@ -126,4 +128,3 @@ TDengine 也提供了支持参数绑定的 Prepare API,与 MySQL 类似,这
<CStmt /> <CStmt />
</TabItem> </TabItem>
</Tabs> </Tabs>
--- ---
sidebar_label: InfluxDB 行协议 sidebar_label: InfluxDB Line Protocol
title: InfluxDB 行协议 title: InfluxDB Line Protocol
--- ---
import Tabs from "@theme/Tabs"; import Tabs from "@theme/Tabs";
...@@ -13,20 +13,20 @@ import NodeLine from "./_js_line.mdx"; ...@@ -13,20 +13,20 @@ import NodeLine from "./_js_line.mdx";
import CsLine from "./_cs_line.mdx"; import CsLine from "./_cs_line.mdx";
import CLine from "./_c_line.mdx"; import CLine from "./_c_line.mdx";
## 协议介绍 ## Introduction
InfluxDB Line 协议采用一行字符串来表示一行数据。分为四部分: A single line of text is used in InfluxDB Line protocol format represents one row of data, each line contains 4 parts as shown below.
``` ```
measurement,tag_set field_set timestamp measurement,tag_set field_set timestamp
``` ```
- measurement 将作为超级表名。它与 tag_set 之间使用一个英文逗号来分隔。 - `measurement` will be used as the STable name
- tag_set 将作为标签数据,其格式形如 `<tag_key>=<tag_value>,<tag_key>=<tag_value>`,也即可以使用英文逗号来分隔多个标签数据。它与 field_set 之间使用一个半角空格来分隔。 - `tag_set` will be used as tags, with format like `<tag_key>=<tag_value>,<tag_key>=<tag_value>`
- field_set 将作为普通列数据,其格式形如 `<field_key>=<field_value>,<field_key>=<field_value>`,同样是使用英文逗号来分隔多个普通列的数据。它与 timestamp 之间使用一个半角空格来分隔。 - `field_set`will be used as data columns, with format like `<field_key>=<field_value>,<field_key>=<field_value>`
- timestamp 即本行数据对应的主键时间戳。 - `timestamp` is the primary key timestamp corresponding to this row of data
例如: For example:
``` ```
meters,location=Beijing.Haidian,groupid=2 current=13.4,voltage=223,phase=0.29 1648432611249500 meters,location=Beijing.Haidian,groupid=2 current=13.4,voltage=223,phase=0.29 1648432611249500
...@@ -34,16 +34,16 @@ meters,location=Beijing.Haidian,groupid=2 current=13.4,voltage=223,phase=0.29 16 ...@@ -34,16 +34,16 @@ meters,location=Beijing.Haidian,groupid=2 current=13.4,voltage=223,phase=0.29 16
:::note :::note
- tag_set 中的所有的数据自动转化为 nchar 数据类型; - All the data in `tag_set` will be converted to ncahr type automatically .
- field_set 中的每个数据项都需要对自身的数据类型进行描述, 比如 1.2f32 代表 float 类型的数值 1.2, 如果不带类型后缀会被当作 double 处理; - Each data in `field_set` must be self-description for its data type. For example 1.2f32 means a value 1.2 of float type, it will be treated as double without the "f" type suffix.
- timestamp 支持多种时间精度。写入数据的时候需要用参数指定时间精度,支持从小时到纳秒的 6 种时间精度。 - Multiple kinds of precision can be used for the `timestamp` field. Time precision can be from nanosecond (ns) to hour (h).
::: :::
要了解更多可参考:[InfluxDB Line 协议官方文档](https://docs.influxdata.com/influxdb/v2.0/reference/syntax/line-protocol/) 和 [TDengine 无模式写入参考指南](/reference/schemaless/#无模式写入行协议) For more details please refer to [InfluxDB Line Protocol](https://docs.influxdata.com/influxdb/v2.0/reference/syntax/line-protocol/) and [TDengine Schemaless](/reference/schemaless/#Schemaless-Line-Protocol)
## 示例代码 ## Examples
<Tabs defaultValue="java" groupId="lang"> <Tabs defaultValue="java" groupId="lang">
<TabItem label="Java" value="java"> <TabItem label="Java" value="java">
......
--- ---
sidebar_label: OpenTSDB 行协议 sidebar_label: OpenTSDB Line Protocol
title: OpenTSDB 行协议 title: OpenTSDB Line Protocol
--- ---
import Tabs from "@theme/Tabs"; import Tabs from "@theme/Tabs";
...@@ -13,28 +13,28 @@ import NodeTelnet from "./_js_opts_telnet.mdx"; ...@@ -13,28 +13,28 @@ import NodeTelnet from "./_js_opts_telnet.mdx";
import CsTelnet from "./_cs_opts_telnet.mdx"; import CsTelnet from "./_cs_opts_telnet.mdx";
import CTelnet from "./_c_opts_telnet.mdx"; import CTelnet from "./_c_opts_telnet.mdx";
## 协议介绍 ## Introduction
OpenTSDB 行协议同样采用一行字符串来表示一行数据。OpenTSDB 采用的是单列模型,因此一行只能包含一个普通数据列。标签列依然可以有多个。分为四部分,具体格式约定如下: A single line of text is used in OpenTSDB line protocol to represent one row of data. OpenTSDB employs single column data model, so one line can only contains single data column. There can be multiple tags. Each line contains 4 parts as below:
```txt ```
<metric> <timestamp> <value> <tagk_1>=<tagv_1>[ <tagk_n>=<tagv_n>] <metric> <timestamp> <value> <tagk_1>=<tagv_1>[ <tagk_n>=<tagv_n>]
``` ```
- metric 将作为超级表名。 - `metric` will be used as STable name.
- timestamp 本行数据对应的时间戳。根据时间戳的长度自动识别时间精度。支持秒和毫秒两种时间精度 - `timestamp` is the timestamp of current row of data. The time precision will be determined automatically based on the length of the timestamp. second and millisecond time precision are supported.\
- value 度量值,必须为一个数值。对应的列名也是 “value”。 - `value` is a metric which must be a numeric value, the corresponding column name is "value".
- 最后一部分是标签集, 用空格分隔不同标签, 所有标签自动转化为 nchar 数据类型; - The last part is tag sets separated by space, all tags will be converted to nchar type automatically.
例如: For example:
```txt ```txt
meters.current 1648432611250 11.3 location=Beijing.Haidian groupid=3 meters.current 1648432611250 11.3 location=Beijing.Haidian groupid=3
``` ```
参考[OpenTSDB Telnet API文档](http://opentsdb.net/docs/build/html/api_telnet/put.html)。 Please refer to [OpenTSDB Telnet API](http://opentsdb.net/docs/build/html/api_telnet/put.html) for more details.
## 示例代码 ## Examples
<Tabs defaultValue="java" groupId="lang"> <Tabs defaultValue="java" groupId="lang">
<TabItem label="Java" value="java"> <TabItem label="Java" value="java">
...@@ -60,13 +60,13 @@ meters.current 1648432611250 11.3 location=Beijing.Haidian groupid=3 ...@@ -60,13 +60,13 @@ meters.current 1648432611250 11.3 location=Beijing.Haidian groupid=3
</TabItem> </TabItem>
</Tabs> </Tabs>
以上示例代码会自动创建 2 个超级表, 每个超级表有 4 条数据。 2 STables will be crated automatically while each STable has 4 rows of data in the above sample code.
```cmd ```cmd
taos> use test; taos> use test;
Database changed. Database changed.
taos> show stables; taos> show STables;
name | created_time | columns | tags | tables | name | created_time | columns | tags | tables |
============================================================================================ ============================================================================================
meters.current | 2022-03-30 17:04:10.877 | 2 | 2 | 2 | meters.current | 2022-03-30 17:04:10.877 | 2 | 2 | 2 |
......
--- ---
sidebar_label: OpenTSDB JSON 格式协议 sidebar_label: OpenTSDB JSON Protocol
title: OpenTSDB JSON 格式协议 title: OpenTSDB JSON Protocol
--- ---
import Tabs from "@theme/Tabs"; import Tabs from "@theme/Tabs";
...@@ -13,9 +13,9 @@ import NodeJson from "./_js_opts_json.mdx"; ...@@ -13,9 +13,9 @@ import NodeJson from "./_js_opts_json.mdx";
import CsJson from "./_cs_opts_json.mdx"; import CsJson from "./_cs_opts_json.mdx";
import CJson from "./_c_opts_json.mdx"; import CJson from "./_c_opts_json.mdx";
## 协议介绍 ## Introduction
OpenTSDB JSON 格式协议采用一个 JSON 字符串表示一行或多行数据。例如: A JSON string is used in OpenTSDB JSON to represent one or more rows of data, for exmaple:
```json ```json
[ [
...@@ -40,18 +40,18 @@ OpenTSDB JSON 格式协议采用一个 JSON 字符串表示一行或多行数据 ...@@ -40,18 +40,18 @@ OpenTSDB JSON 格式协议采用一个 JSON 字符串表示一行或多行数据
] ]
``` ```
与 OpenTSDB 行协议类似, metric 将作为超级表名, timestamp 表示时间戳,value 表示度量值, tags 表示标签集。 Similar to OpenTSDB line protocol, `metric` will be used as the STable name, `timestamp` is the timestamp to be used, `value` represents the metric collected, `tags` are the tag sets.
参考[OpenTSDB HTTP API文档](http://opentsdb.net/docs/build/html/api_http/put.html)。 Please refer to [OpenTSDB HTTP API](http://opentsdb.net/docs/build/html/api_http/put.html) for more details.
:::note :::note
- 对于 JSON 格式协议,TDengine 并不会自动把所有标签转成 nchar 类型, 字符串将将转为 nchar 类型, 数值将同样转换为 double 类型。 - In JSON protocol, strings will be converted to nchar type and numeric values will be converted to double type.
- TDengine 只接收 JSON **数组格式**的字符串,即使一行数据也需要转换成数组形式。 - Only data in array format is accepted, array must be used even there is only one row.
::: :::
## 示例代码 ## Examples
<Tabs defaultValue="java" groupId="lang"> <Tabs defaultValue="java" groupId="lang">
<TabItem label="Java" value="java"> <TabItem label="Java" value="java">
...@@ -77,13 +77,13 @@ OpenTSDB JSON 格式协议采用一个 JSON 字符串表示一行或多行数据 ...@@ -77,13 +77,13 @@ OpenTSDB JSON 格式协议采用一个 JSON 字符串表示一行或多行数据
</TabItem> </TabItem>
</Tabs> </Tabs>
以上示例代码会自动创建 2 个超级表, 每个超级表有 2 条数据。 The above sample code will created 2 STables automatically while each STable has 2 rows of data.
```cmd ```cmd
taos> use test; taos> use test;
Database changed. Database changed.
taos> show stables; taos> show STables;
name | created_time | columns | tags | tables | name | created_time | columns | tags | tables |
============================================================================================ ============================================================================================
meters.current | 2022-03-29 16:05:25.193 | 2 | 2 | 1 | meters.current | 2022-03-29 16:05:25.193 | 2 | 2 | 1 |
......
```c title=一次绑定一行 ```c title=Single Row Binding
{{#include docs-examples/c/stmt_example.c}} {{#include docs-examples/c/stmt_example.c}}
``` ```
```c title=一次绑定多行 72:117 ```c title=Multiple Row Binding 72:117
{{#include docs-examples/c/multi_bind_example.c}} {{#include docs-examples/c/multi_bind_example.c}}
``` ```
\ No newline at end of file
label: 写入数据 label: Insert Data
link:
type: generated-index
slug: /insert-data/
description: "TDengine 支持多种写入协议,包括 SQL,InfluxDB Line 协议, OpenTSDB Telnet 协议,OpenTSDB JSON 格式协议。数据可以单条插入,也可以批量插入,可以插入一个数据采集点的数据,也可以同时插入多个数据采集点的数据。同时,TDengine 支持多线程插入,支持时间乱序数据插入,也支持历史数据插入。InfluxDB Line 协议、OpenTSDB Telnet 协议和 OpenTSDB JSON 格式协议是 TDengine 支持的三种无模式写入协议。使用无模式方式写入无需提前创建超级表和子表,并且引擎能自适用数据对表结构做调整。"
...@@ -3,6 +3,6 @@ ...@@ -3,6 +3,6 @@
``` ```
:::tip :::tip
driver-go 的模块 `github.com/taosdata/driver-go/v2/wrapper` 是 C 接口的底层封装。使用这个模块也可以实现参数绑定写入。 `github.com/taosdata/driver-go/v2/wrapper` module in driver-go is the wrapper for C API, it can be used to insert data with parameter binding.
::: :::
```js title=一次绑定一行 ```js title=Single Row Binding
{{#include docs-examples/node/nativeexample/param_bind_example.js}} {{#include docs-examples/node/nativeexample/param_bind_example.js}}
``` ```
```js title=一次绑定多行 ```js title=Multiple Row Binding
{{#include docs-examples/node/nativeexample/multi_bind_example.js:insertData}} {{#include docs-examples/node/nativeexample/multi_bind_example.js:insertData}}
``` ```
:::info :::info
一次绑定一行效率不如一次绑定多行,但支持非 INSERT 语句。一次绑定多行效率更高,但仅支持 INSERT 语句。 Multiple row binding is better in performance than single row binding, but it can only be used with `INSERT` statement while single row binding can be used for other SQL statements besides `INSERT`.
::: :::
```py title=一次绑定一行 ```py title=Single Row Binding
{{#include docs-examples/python/bind_param_example.py}} {{#include docs-examples/python/bind_param_example.py}}
``` ```
```py title=一次绑定多行 ```py title=Multiple Row Binding
{{#include docs-examples/python/multi_bind_example.py:bind_batch}} {{#include docs-examples/python/multi_bind_example.py:bind_batch}}
``` ```
:::info :::info
一次绑定一行效率不如一次绑定多行,但支持非 INSERT 语句。一次绑定多行效率更高,但仅支持 INSERT 语句。 Multiple row binding is better in performance than single row binding, but it can only be used with `INSERT` statement while single row binding can be used for other SQL statements besides `INSERT`.
::: :::
\ No newline at end of file
---
title: Insert
---
TDengine supports multiple protocols of inserting data, including SQL, InfluxDB Line protocol, OpenTSDB Telnet protocol, OpenTSDB JSON protocol. Data can be inserted row by row, or in batch. Data from one or more collecting points can be inserted simultaneously. In the meantime, data can be inserted with multiple threads, out of order data and historical data can be inserted too. InfluxDB Line protocol, OpenTSDB Telnet protocol and OpenTSDB JSON protocol are the 3 kinds of schemaless insert protocols supported by TDengine. It's not necessary to create stable and table in advance if using schemaless protocols, and the schemas can be adjusted automatically according to the data to be inserted.
```mdx-code-block
import DocCardList from '@theme/DocCardList';
import {useCurrentSidebarCategory} from '@docusaurus/theme-common';
<DocCardList items={useCurrentSidebarCategory().items}/>
```
\ No newline at end of file
通过迭代逐行获取查询结果。 Result set is iterated row by row.
```py ```py
{{#include docs-examples/python/query_example.py:iter}} {{#include docs-examples/python/query_example.py:iter}}
``` ```
一次获取所有查询结果,并把每一行转化为一个字典返回。 Result set is retrieved as a whole, each row is converted to a dict and returned.
```py ```py
{{#include docs-examples/python/query_example.py:fetch_all}} {{#include docs-examples/python/query_example.py:fetch_all}}
``` ```
\ No newline at end of file
...@@ -3,6 +3,6 @@ ...@@ -3,6 +3,6 @@
``` ```
:::note :::note
这个示例程序,目前在 Windows 系统上还无法运行 This sample code can't be run on Windows system for now.
::: :::
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册