Save data to TDengine

TDengineSave data to TDengine - 图1 (opens new window)
TaosData(https://www.taosdata.com) Enables efficient, real-time data ingestion, processing and monitoring of TB and even PB scale data per day, generated by billions of sensors and data collectors. TDengine can be widely applied to IoT, Industrial Internet, Connected Vehicles, DevOps, Energy , Finance and many other use-cases.

EMQX supports saving data to TDengine via Send to Web Server, and also provides native TDengine drivers on EMQX Enterprise for direct saving.

Install TDengine by docker or Huawei CloudSave data to TDengine - 图2 (opens new window)

  1. docker run --name TDengine -d -p 6030:6030 -p 6035:6035 -p 6041:6041 -p 6030-6040:6030-6040/udp tdengine/tdengine

Execute cmd in docker:

  1. docker exec -it TDengine bash
  2. taos

Create database test

  1. create database test;

Create table t_mqtt_msg. More docsSave data to TDengine - 图3 (opens new window):

  1. USE test;
  2. CREATE TABLE t_mqtt_msg (
  3. ts timestamp,
  4. msgid NCHAR(64),
  5. mqtt_topic NCHAR(255),
  6. qos TINYINT,
  7. payload BINARY(1024),
  8. arrived timestamp
  9. );

Create Rule:

Open EMQX DashboardSave data to TDengine - 图4 (opens new window). Click Rule.

Write SQL:

  1. SELECT
  2. *,
  3. now_timestamp('millisecond') as ts
  4. FROM
  5. "#"

image

The subsequent action creation operation can be selected flexibly depending on your EMQX version.

Native SDK (EMQX Enterprise)

Add Action:

Find Action, click Add Action.

Click Data persist, Choice Data to TDengine.

Requires EMQX Enterprise 4.1.1 and later only.

Action parameters:

  1. SQL template. In this example we insert a data to TDengine, note that we should specify the database name in the SQL and the character type should be enclosed in single quotes, the SQL template:
  1. insert into test.t_mqtt_msg(ts, msgid, mqtt_topic, qos, payload, arrived) values (${ts}, '${id}', '${topic}', ${qos}, '${payload}', ${timestamp})
  1. Now that the resource drop-down box is empty, you can create a TDengine resource by clicking on Create in the upper right corner:

Create new TDengine Resource:
TDEngine Username: root
TDEngine password: taosdata

image

Click Confirm.

Return to the action screen and click “Confirm”.

image

Return to the rule screen and click “Create”.

Write by Web Server

To support development on different types of platforms, TDengine provides APIs that conform to REST design standards.

The RESTful ConnectorSave data to TDengine - 图8 (opens new window) provides the simplest way to connect, i.e. using HTTP requests carrying authentication information with the SQL operation to be performed TDengine.

Add Action:

Find Action, click Add Action.

Click Data persist, Choice Data to Web Server.

Data to Web Server parameters:

  1. Using of resources: Associated Web Server resources, you can click the Create button to create new resources.
  2. Method:POST
  3. Path: /rest/sql
  4. Headers: Authorization Basic, “root:taosdata” base64 encode is cm9vdDp0YW9zZGF0YQ==
  5. Body: Request Body. In this example we are inserting a piece of data into TDengine, which should carry the INSERT SQL within the request body, noting that we should specify the database name in the SQL and the character type should be enclosed in single quotes, with the message content template as follows
  1. -- Note: the topic is added as an identifier because in this example we will have two resources written to TDengine, and the identifier distinguishes between the data written natively and by the Web Server
  2. insert into test.t_mqtt_msg(ts, msgid, mqtt_topic, qos, payload, arrived) values (${ts}, '${id}', 'http server ${topic}', ${qos}, '${payload}', ${timestamp})

image

Create Web Server resource:

Web Server config:
Request URL is http://127.0.0.1:6041.
Please fill in the other fields according to the server settings. If encrypted transmission is configured, please fill in the certificate information. Only the default parameters are used in the example.
Return to the rule screen and click “Create”.

image

Test

In the rule list, click on the Rule ID link to preview the rule you just created.

image

The rule has been created, now send a data:

  1. Topic: "t/a"
  2. QoS: 1
  3. Payload: {"msg": "hello"}

Query and check:

  1. select * from t_mqtt_msg;

image