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

Prerequisites

  • A data source named DataSource is created and a TSL communication topic is added the data source. For more information, see Create a data source.
  • A Tablestore instance and a table that is used to receive data are created. For more information, see the Tablestore documentation.
    Notice If you use an IoT Platform instance of the Enterprise Edition, the region where the Tablestore instance resides must be the same as that of the IoT Platform instance.

Create a data destination

  1. Log on to the IoT Platform console.
  2. In the left-side navigation pane, choose Rules > Data Forwarding.
  3. Optional:On the Data Forwarding page, click Go to New Version in the upper-right corner 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 Rules > Data Forwarding.
  4. Click the Data Destination tab. On this tab, click Create Data Destination.
  5. In the Create Data Destination dialog box, enter a data destination name. In this example, enter DataPurpose. Set the parameters and then click OK.
    Note Only JSON-formatted data can be forwarded.
    Select Operation
    Parameter Description
    Select Operation Select Save to Tablestore.
    Region Select a region where Tablestore runs.
    Instance Select a Tablestore instance.

    To create an instance in the Tablestore console, click Create Instance. For more information, see the Tablestore documentation.

    Table Select a Tablestore table that is used to receive data.

    To create a table in the Tablestore console, click Create Table.

    Role Authorize IoT Platform to write data to Tablestore.

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 write a script, see Sample script.
    For more information about the function parameters, see Functions.
    // Use the payload() function to obtain the data that is submitted by devices and convert the data by using the JSON format. 
    var data = payload("json");
    // Obtain the submitted property values.
    var h = data.items.Humidity.value;
    var t = data.items.Temperature.value;
    // Add the deviceName and id primary keys to the table. You can use the writeTableStore() method to write values to columns by using the format of column:value.
    writeTableStore(1000, {"devcieName":deviceName(), "id":timestamp(), "temperature":t, "humidity":h});
  6. Click Debugging. In the dialog box that appears, select a product and a device, specify a topic, and enter payload data to verify whether the script can run properly.
    The following figure shows the parameters.Debugging example

    The following result indicates that the script is implemented.

    Debugging result
  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 Tablestore console. Go to the Data Editor page of the table that is used to receive data and check whether the specified data is received.
    Data Management