All Products
Search
Document Center

IoT Platform:Forward data to OTS instances of the TimeSeries model

Last Updated:Nov 23, 2023

You can use the data forwarding feature of the rules engine to forward data to Tablestore (OTS) instances of the TimeSeries model. This topic describes the data forwarding process. In this example, a Thing Specification Language (TSL) communication topic is used as a source topic.

Prerequisites

  • 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.

  • An OTS instance and a table that is used to receive data are created. For more information, see the "TimeSeries model" section in the OTS documentation.

    Important

    If you use an IoT Platform Enterprise Edition instance, the region where the OTS instance is deployed must be the same as the region of the IoT Platform instance.

Background information

The TimeSeries model is used to store time series data that is generated in scenarios such as IoT device monitoring, device data collection, and machine monitoring. The TimeSeries model supports automatic indexing of time series metadata and time series queries based on various composite conditions. For more information, see Overview.

In this example, the writeTableStoreTs() function is called and the following parameters are specified to write data to the time series table:

  • fields: Use JSONPath expressions to obtain the values of the Temperature and Humidity properties that are submitted by the device. Write the values to the time series table.

  • timeInUs: Call the timestamp() function to obtain a timestamp in milliseconds. The timestamp specifies the point in time at which the device submitted data. The value of the timeInUs parameter must be in microseconds. You can calculate the value of the timeInUs parameter by using the following formula: timestamp() × 1,000. Write the result to the table.

  • tags: Write {"region": "cn-shanghai"} to the table.

  • metricName: Write property to the table.

  • dataSource: Call the deviceName() function to obtain the DeviceName and then write the DeviceName to the table.

Limits

Only Enterprise Edition instances in the China (Shanghai) region allow you to use the new version of the data forwarding feature to forward data to OTS instances of the TimeSeries model.

Create a data destination

  1. Log on to the IoT Platform console.
  2. On the Overview tab, find the instance that you want to manage and click the instance name to go to the Instance Details page.

    选择实例

  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. Example: DataPurpose. Configure the parameters and click OK.

    The following table describes the parameters.

    Note

    You can forward only data in the JSON format.

    Parameter

    Description

    Operation

    Select Store Data to Tablestore.

    Region

    Select the region in which OTS is activated.

    Instance

    Select an OTS instance.

    You can click Create Instance to go to the OTS console and create an OTS instance of the TimeSeries model. For more information, see Create an instance of the TimeSeries model.

    Storage Class

    Select TimeSeries Model.

    Data Table

    Select the time series table that you want to use to receive data.

    You can click Create Data Table to go to the OTS console and create a time series table. For more information, see Create a time series table.

    Role

    Grant IoT Platform the permissions to write data to the time series table.

    If you do not have Resource Access Management (RAM) roles, click Create RAM Role to go to the RAM console, create a RAM role, and then grant permissions to the role. For more information, see Create a RAM role.

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.

  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.

    // Call the payload() function to obtain the data that is submitted by devices and convert the data into the JSON format. 
    var data = payload("json");
    // Obtain the submitted property values. 
    var h = data.items.Humidity.value;
    var t = data.items.Temperature.value;
    
    var datasource = deviceName();
    var fields = {"temperature":t, "humidity":h};
    var tags = {};
    tags.put("region", "cn-shanghai");
    var metricName = "property";
    var timeInUs = timestamp()*1000;
    
    // Call the writeTableStoreTS method to write the temperature and humidity values to the corresponding columns based on the point in time at which the values are submitted. 
    writeTableStoreTs(1000, timeInUs, metricName, tags, fields, datasource);

    You can also set the metricName parameter to Temperature or Humidity, and then write the corresponding data. Sample code:

    writeTableStoreTs(1000, [
        {
            "timeInUs": timestamp() * 1000,
            "metricName": "Temperature",
            "tags":tags,
            "fields":{"temperature": t},
            "dataSource": deviceName()
        },
        {
            "timeInUs": timestamp() * 1000,
            "metricName": "Humidity",
            "tags":tags,
            "fields":{"humidity": h},
            "dataSource": 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.调试数据

    If an output that is similar to the following information is returned, 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.

  9. After the data is pushed, log on to the OTS console. Go to the Data Editor page of the table that is used to receive data and check whether the specified data is received. For more information, see Query time series data.

    You can use the SQL query feature to perform complex queries and analysis on data in OTS in an efficient manner. For more information, see Use SQL to query time series data.数据管理