Explorar el Código

fea(sinks): write sql in batch

Signed-off-by: Jianxiang Ran <rxan_embedded@163.com>
Jianxiang Ran hace 2 años
padre
commit
81f2fac44b

+ 45 - 6
docs/en_US/rules/sinks/plugin/sql.md

@@ -27,12 +27,13 @@ for example, if he only wants mysql, then he can build with build tag `mysql`.
 
 
 ## Properties
 ## Properties
 
 
-| Property name | Optional | Description                                                                                                                                                   |
-|---------------|----------|---------------------------------------------------------------------------------------------------------------------------------------------------------------|
-| url           | false    | The url of the target database                                                                                                                                |
-| table         | false    | The table name of the result                                                                                                                                  |
-| fields        | false    | The fields to be inserted to. The result map and the database should both have these fields. If not specified, all fields in the result map will be inserted. |
- 
+| Property name  | Optional | Description                                                                                                                                                   |
+|----------------|----------|---------------------------------------------------------------------------------------------------------------------------------------------------------------|
+| url            | false    | The url of the target database                                                                                                                                |
+| table          | false    | The table name of the result                                                                                                                                  |
+| fields         | false    | The fields to be inserted to. The result map and the database should both have these fields. If not specified, all fields in the result map will be inserted. |
+| tableDataField | true     | Write the nested values of the tableDataField into database.                                                                                                  |
+
 ## Sample usage
 ## Sample usage
 
 
 Below is a sample for using sql to get the target data and set to mysql database 
 Below is a sample for using sql to get the target data and set to mysql database 
@@ -55,3 +56,41 @@ Below is a sample for using sql to get the target data and set to mysql database
 }
 }
 ```
 ```
 
 
+
+Write values of tableDataField into database:
+
+The following configuration will write telemetry field's values into database
+
+```json
+{
+  "telemetry": [{
+    "temperature": 32.32,
+    "humidity": 80.8,
+    "ts": 1388082430
+  },{
+    "temperature": 34.32,
+    "humidity": 81.8,
+    "ts": 1388082440
+  }]
+}
+```
+
+```json lines
+{
+  "id": "rule",
+  "sql": "SELECT telemetry FROM dataStream",
+  "actions": [
+    {
+      "log": {
+      },
+      "sql": {
+        "url": "mysql://user:test@140.210.204.147/user?parseTime=true",
+        "table": "test",
+        "fields": ["temperature","humidity"],
+        "tableDataField":  "telemetry",
+      }
+    }
+  ]
+}
+```
+

+ 52 - 13
docs/en_US/rules/sinks/plugin/tdengine.md

@@ -13,19 +13,20 @@ Since the operation of the tdengine plug-in depends on the tdengine client, for
 
 
 As the tdengine database requires a timestamp field in the table, the user must inform the timestamp field name of the data table (required tsFieldName). The user can choose whether to provide timestamp data. If not (provideTs=false), the content of the timestamp field is automatically generated by the tdengine database.
 As the tdengine database requires a timestamp field in the table, the user must inform the timestamp field name of the data table (required tsFieldName). The user can choose whether to provide timestamp data. If not (provideTs=false), the content of the timestamp field is automatically generated by the tdengine database.
 
 
-| Name        | Type     | Optional | Description                                                                                                                                                      |
-|-------------|----------|----------|------------------------------------------------------------------------------------------------------------------------------------------------------------------|
-| host        | string   | true     | Database host, it must be a host name aka. [FQDN](https://www.taosdata.com/blog/2020/09/11/1824.html). An IP address is invalid. The default value is localhost. |
-| port        | int      | false    | Database port                                                                                                                                                    |
-| user        | string   | true     | Username, default to `root`.                                                                                                                                     |
-| password    | string   | true     | Password, default to `taosdata`.                                                                                                                                 |
-| database    | string   | false    | Database name.                                                                                                                                                   |
-| table       | string   | false    | Table Name, could be a [dynamic property](../../overview.md#dynamic-properties).                                                                                 |
-| fields      | []string | true     | The fields to be inserted to. The result map and the database should both have these fields. If not specified, all fields in the result map will be inserted.    |
-| provideTs   | Bool     | true     | Whether the user provides a timestamp field, default to false.                                                                                                   |
-| tsFieldName | String   | true     | Timestamp field name                                                                                                                                             |
-| sTable      | String   | true     | The super table to be use, could be a [dynamic property](../../overview.md#dynamic-properties).                                                                  |
-| tagFields   | []String | true     | The result fields to be used as the tag values in order. If sTable is specified, this is required.                                                               |
+| Name           | Type     | Optional | Description                                                                                                                                                      |
+|----------------|----------|----------|------------------------------------------------------------------------------------------------------------------------------------------------------------------|
+| host           | string   | true     | Database host, it must be a host name aka. [FQDN](https://www.taosdata.com/blog/2020/09/11/1824.html). An IP address is invalid. The default value is localhost. |
+| port           | int      | false    | Database port                                                                                                                                                    |
+| user           | string   | true     | Username, default to `root`.                                                                                                                                     |
+| password       | string   | true     | Password, default to `taosdata`.                                                                                                                                 |
+| database       | string   | false    | Database name.                                                                                                                                                   |
+| table          | string   | false    | Table Name, could be a [dynamic property](../../overview.md#dynamic-properties).                                                                                 |
+| fields         | []string | true     | The fields to be inserted to. The result map and the database should both have these fields. If not specified, all fields in the result map will be inserted.    |
+| provideTs      | Bool     | true     | Whether the user provides a timestamp field, default to false.                                                                                                   |
+| tsFieldName    | String   | true     | Timestamp field name                                                                                                                                             |
+| tagFields      | []String | true     | The result fields to be used as the tag values in order. If sTable is specified, this is required.                                                               |
+| sTable         | String   | true     | The super table to be use, could be a [dynamic property](../../overview.md#dynamic-properties).                                                                  |
+| tableDataField | String   | true     | Write the nested values of the tableDataField into database.                                                                                                     |
 
 
 ## Operation example
 ## Operation example
 
 
@@ -80,3 +81,41 @@ Write into dynamic table:
 }
 }
 ```
 ```
 
 
+
+Write values of tableDataField into database:
+
+The following configuration will write telemetry field's values into database 
+
+```json
+{
+  "telemetry": [{
+    "temperature": 32.32,
+    "humidity": 80.8,
+    "f3": "f3tagValue",
+    "f4": "f4tagValue",
+    "ts": 1388082430
+  },{
+    "temperature": 34.32,
+    "humidity": 81.8,
+    "f3": "f3tagValue",
+    "f4": "f4tagValue",
+    "ts": 1388082440
+  }]
+}
+```
+
+```json lines
+{
+  "tdengine": {
+    "host":        "hostname",
+    "port":        6030,
+    "database":    "dab",
+    "table":       "tableName", // dynamic value, get from the table field of the result
+    "tsfieldname": "ts",
+    "fields":      []string{"temperature", "humidity"}, // Write f1, f2 fields in result into f1, f2 columns in the db
+    "sTable":      "myStable", // super table name, also allow dynamic
+    "tableDataField":      "telemetry", // write values of telemetry field into database
+    "tagFields":   []string{"f3","f4"} // Write f3, f4 fields' values in the result as tags in order
+  }
+}
+```

+ 46 - 6
docs/zh_CN/rules/sinks/plugin/sql.md

@@ -24,12 +24,13 @@
 
 
 ## Properties
 ## Properties
 
 
-| Property name | Optional | Description                                 |
-|---------------|----------|---------------------------------------------|
-| url           | false    | 目标数据库的url                                   |
-| table         | false    | 结果的表名                                       |
-| fields        | false    | 要插入的字段。结果映射和数据库都应该有这些字段。如果未指定,将插入结果映射中的所有字段 |
- 
+| Property name  | Optional | Description                                 |
+|----------------|----------|---------------------------------------------|
+| url            | false    | 目标数据库的url                                   |
+| table          | false    | 结果的表名                                       |
+| fields         | false    | 要插入的字段。结果映射和数据库都应该有这些字段。如果未指定,将插入结果映射中的所有字段 |
+| tableDataField | false    | 将 tableDataField 的嵌套值写入数据库。                 |
+
 ## 使用样例
 ## 使用样例
 
 
 下面是一个获取目标数据并写入 mysql 数据库的示例
 下面是一个获取目标数据并写入 mysql 数据库的示例
@@ -52,3 +53,42 @@
 }
 }
 ```
 ```
 
 
+根据 tableDataField 配置将结果写入数据库:
+
+以下配置将 telemetry 字段的对应值写入数据库
+
+```json
+{
+  "telemetry": [{
+    "temperature": 32.32,
+    "humidity": 80.8,
+    "ts": 1388082430
+  },{
+    "temperature": 34.32,
+    "humidity": 81.8,
+    "ts": 1388082440
+  }]
+}
+```
+
+```json lines
+{
+  "id": "rule",
+  "sql": "SELECT telemetry FROM dataStream",
+  "actions": [
+    {
+      "log": {
+      },
+      "sql": {
+        "url": "mysql://user:test@140.210.204.147/user?parseTime=true",
+        "table": "test",
+        "fields": ["temperature","humidity"],
+        "tableDataField":  "telemetry",
+      }
+    }
+  ]
+}
+```
+
+
+

+ 54 - 13
docs/zh_CN/rules/sinks/plugin/tdengine.md

@@ -13,19 +13,20 @@ go build -trimpath -modfile extensions.mod --buildmode=plugin -o plugins/sinks/T
 
 
 由于 tdengine 数据库要求表中必须有时间戳字段,所以用户必须告知数据表的时间戳字段名称(必填tsFieldName)。用户可以选择是否提供时间戳数据,若不提供(provideTs=false),时间戳字段的内容由 tdengine 数据库自动生成。
 由于 tdengine 数据库要求表中必须有时间戳字段,所以用户必须告知数据表的时间戳字段名称(必填tsFieldName)。用户可以选择是否提供时间戳数据,若不提供(provideTs=false),时间戳字段的内容由 tdengine 数据库自动生成。
 
 
-| 名称          | 类型       | 是否必填 | 释义                                                                                                    |
-|-------------|----------|------|-------------------------------------------------------------------------------------------------------|
-| host        | string   | 否    | 数据库域名,其值必须为域名,即 [FQDN](https://www.taosdata.com/blog/2020/09/11/1824.html),不能为 IP 地址。其默认值为 localhost。 |
-| port        | int      | 是    | 数据库端口                                                                                                 |
-| user        | string   | 否    | 用户名,默认值为 `root` 。                                                                                     |
-| password    | string   | 否    | 密码,默认值为 `taosdata` 。                                                                                  |
-| database    | string   | 是    | 数据库名                                                                                                  |
-| table       | string   | 是    | 表名,可设置[动态属性](../../overview.md#动态属性)。                                                                 |
-| fields      | []string | 否    | 将要插入的表字段集合。sink 收到的数据和数据库表中均有该字段。若为设置,则所有结果字段写入数据库。                                                   |
-| provideTs   | Bool     | 否    | 用户是否提供时间戳字段,默认为否。                                                                                     |
-| tsFieldName | String   | 是    | 时间戳字段名称                                                                                               |
-| sTable      | String   | 否    | 使用的超级表,可设置[动态属性](../../overview.md#动态属性)。                                                             |
-| tagFields   | []String | 否    | 结果中作为标签的字段。若设置 sTable 属性,则该属性必填。                                                                      |
+| 名称             | 类型       | 是否必填 | 释义                                                                                                    |
+|----------------|----------|--|-------------------------------------------------------------------------------------------------------|
+| host           | string   | 否 | 数据库域名,其值必须为域名,即 [FQDN](https://www.taosdata.com/blog/2020/09/11/1824.html),不能为 IP 地址。其默认值为 localhost。 |
+| port           | int      | 是 | 数据库端口                                                                                                 |
+| user           | string   | 否 | 用户名,默认值为 `root` 。                                                                                     |
+| password       | string   | 否 | 密码,默认值为 `taosdata` 。                                                                                  |
+| database       | string   | 是 | 数据库名                                                                                                  |
+| table          | string   | 是 | 表名,可设置[动态属性](../../overview.md#动态属性)。                                                                 |
+| fields         | []string | 否 | 将要插入的表字段集合。sink 收到的数据和数据库表中均有该字段。若为设置,则所有结果字段写入数据库。                                                   |
+| provideTs      | Bool     | 否 | 用户是否提供时间戳字段,默认为否。                                                                                     |
+| tsFieldName    | String   | 是 | 时间戳字段名称                                                                                               |
+| sTable         | String   | 否 | 使用的超级表,可设置[动态属性](../../overview.md#动态属性)。                                                             |
+| tagFields      | []String | 否 | 结果中作为标签的字段。若设置 sTable 属性,则该属性必填。                                                                      |
+| tableDataField | String   | 否 | 将 tableDataField 的嵌套值写入数据库。                                          |
 
 
 ## 操作示例
 ## 操作示例
 
 
@@ -81,3 +82,43 @@ curl --location --request POST 'http://127.0.0.1:9081/rules' --header 'Content-T
 ```
 ```
 
 
 
 
+
+根据 tableDataField 配置将结果写入数据库:
+
+以下配置将 telemetry 字段的对应值写入数据库
+
+```json
+{
+  "telemetry": [{
+    "temperature": 32.32,
+    "humidity": 80.8,
+    "f3": "f3tagValue",
+    "f4": "f4tagValue",
+    "ts": 1388082430
+  },{
+    "temperature": 34.32,
+    "humidity": 81.8,
+    "f3": "f3tagValue",
+    "f4": "f4tagValue",
+    "ts": 1388082440
+  }]
+}
+```
+
+```json lines
+{
+  "tdengine": {
+    "host":        "hostname",
+    "port":        6030,
+    "database":    "dab",
+    "table":       "tableName", // dynamic value, get from the table field of the result
+    "tsfieldname": "ts",
+    "fields":      []string{"temperature", "humidity"}, // Write f1, f2 fields in result into f1, f2 columns in the db
+    "sTable":      "myStable", // super table name, also allow dynamic
+    "tableDataField":      "telemetry", // write values of telemetry field into database
+    "tagFields":   []string{"f3","f4"} // Write f3, f4 fields' values in the result as tags in order
+  }
+}
+```
+
+