All Products
Search
Document Center

IoT Platform:Forward data to TSDB

最終更新日:Oct 23, 2023

You can configure data forwarding rules to forward processed data to Time Series Database (TSDB) for storage. This topic describes the data forwarding process. In this example, a Thing Specification Language (TSL) communication topic is used as a source topic.

Prerequisites

  • An IoT Platform instance whose data can be forwarded to TSDB is created in a region that supports data forwarding to TSDB. For more information, see Regions.

  • A data source named DataSource is created and a TSL communication topic is added to the data source. For more information, see Create a data source.

  • A TSDB instance that is deployed in a virtual private cloud (VPC) is created in the Japan (Tokyo) region. For more information, see TSDB documentation.

Background information

After you configure the data destination, the following configurations are automatically completed. Then, you can forward the device data to the TSDB instance by using the rules engine of IoT Platform.

  • IoT Platform occupies two IP addresses of the vSwitch where the TSDB instance resides. The IPv4 CIDR block of the vSwitch is added to the whitelist of the TSDB instance.
  • A managed security group is created in the VPC where the TSDB instance resides. The default name of the security group starts with sg-nsm-.

In this example, the following data is written to TSDB:

  • timestamp: Use the timestamp() function to obtain the time when a device submits data.
  • tag: Use the deviceName() function to obtain the DeviceName. The {"deviceName":deviceName()} tag is written to the database.
  • metric: Use JSONPath expressions to obtain the values of the Temperature and Humidity properties that are submitted by the device.

Limits

  • Data can be forwarded only within a region. For example, you can only forward data from an IoT Platform instance that is deployed in the Japan (Tokyo) region to a TSDB instance that is deployed in the Japan (Tokyo) region.
  • You can forward data only to a TSDB instance that is deployed in a VPC.
  • You can forward data only in the JSON format.
  • In the forwarded messages, all fields are written to TSDB as the metric, except for the fields that are configured as the timestamp and tag value. The data types of the metric parameter support numeric and string. If you use other data types, data may fail to be written to the database.

Usage notes

You can also use the new or previous version of the data forwarding feature to forward data to TSDB. For more information about how to use the previous version of the data forwarding feature, see UForward data to TSDB.

Create a data destination

  1. Log on to the IoT Platform console.
  2. On the Overview page, click All environment. On the All environment tab, find the instance that you want to manage and click the instance ID or instance name.

  3. In the left-side navigation pane, choose Message Forwarding > Data Forwarding.
  4. In the upper-right corner of the Data Forwarding page, click Go to New Version to go to the new version.

    Note

    If you have performed this step, the Data Forwarding page of the new version appears after you choose Message Forwarding > Data Forwarding.

  5. Click Data Destination. On the Data Destination tab, click Create Data Destination.

  6. In the Create Data Destination dialog box, enter a data destination name. In this example, DataPurpose is used. Configure the parameters and click OK.

    Parameter

    Description

    Operation

    Select Store Data to TSDB.

    Region

    Specify the region in which your IoT Platform instance resides.

    TSDB Instance

    Specify the TSDB instance that you have created in a VPC.

    metric Data Type

    Specify the data type of the metric parameter. Valid values: Numeric Type and String.

    You can click the 帮助 icon for help.

    Role

    Grant IoT Platform the permissions to write data to TSDB.

Configure and start a parser

  1. Create a parser named DataParser. For more information, see Create a parser.

  2. On the Parser Details page, associate the parser with the created data source.

    1. In the Data Source step of the wizard, click Associate Data Source.

    2. In the dialog box that appears, select DataSource from the Data Source drop-down list, and then click OK.

  3. On the Parser Details page, associate the parser with the created data destination.

    1. Click Data Destination in the wizard. In the Data Destination section, click Associate Data Destination.

    2. In the dialog box that appears, select DataPurpose from the Data Destination drop-down list, and then click OK.

    3. In the Data Destination section, view and save the data destination ID. In this example, the ID is 1000.

      When you write the parser script, you must use the data destination ID.

  4. On the parser details page, click Parser Script.

  5. In the code editor, enter a script. For more information about how to modify a script, see Script syntax.

    For more information about function parameters, see Functions.

    // Use the payload() function to obtain the data that is submitted by devices and convert the data to JSON-formatted data. 
    var data = payload("json");
    // Filter the submitted temperature and humidity values. 
    var h = data.items.Humidity.value;
    var t = data.items.Temperature.value;
    // Forward submitted TSL data. 
    writeTsdb(1000,timestamp(),"temperature", t , {"deviceName":deviceName()});
    writeTsdb(1000,timestamp(),"humidity", h , {"deviceName":deviceName()});
  6. Click Debugging. In the dialog box that appears, select a product and a device, specify a topic, and then enter payload data to check whether the script runs as expected.

    The following figure shows the parameters.示例

    The following result indicates that the script runs as expected.

    调试结果
  7. Click Publish.

  8. Go to the Parser tab of the Data Forwarding page. Find the DataParser parser and click Start in the Actions column to start the parser.