Forward data to Lindorm

更新时间:
复制 MD 格式

Configure a data forwarding parser to send processed device data to Lindorm. This example uses a Thing Specification Language (TSL) communication topic as the 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 Add a data source.

  • A Lindorm instance is created in the Lindorm console, and the settings are configured based on your business requirements. Only pay-as-you-go and subscription Lindorm instances are supported.

  • The LindormTSDB SDK for native Java is integrated. A time series database, a table, and an account with the corresponding password for managing the database are created. Time series data storage across multiple Lindorm instances is configured. For more information, see Tutorial: Use the Java Native SDK to connect to and use LindormTSDB.

Background information

After a data destination is configured, the system automatically applies the following settings to forward device data to Lindorm through the IoT Platform rules engine.

  • IoT Platform occupies two IP addresses of the vSwitch to which the Lindorm instance is connected. The IPv4 CIDR block of the vSwitch is added to the whitelist of the Lindorm instance.

  • A managed security group is created in the virtual private cloud (VPC) where the Lindorm instance is deployed. The default security group name starts with sg-nsm-.

In this example, the following data is written to the time series database:

  • timestamp: Calls the timestamp() function to obtain the time when a device submits data.

  • tags: Calls the deviceName() function to obtain the DeviceName. The {"device_id": deviceName(), "region": "cn-shanghai"} tag is written to the database.

  • fields: Uses JSONPath expressions to obtain the Temperature and Humidity property values submitted by the device. These values are written to the database.

Limitations

  • Only the new version of the data forwarding feature supports forwarding data to Lindorm. Currently, only the time-series engine of Lindorm is supported as a destination.

  • Supported regions

    • This feature is available only in specific regions where IoT Platform is hosted. For a detailed list of supported regions, see Feature availability by region.

    • Your Lindorm instance must be deployed in the same region as your IoT Platform instance.

    Note

    In the China (Shanghai) region, this feature is not available in Availability Zone B (cn-shanghai-b). It is available in Availability Zones F (cn-shanghai-f) and G (cn-shanghai-g).

Create a data destination

  1. Log on to the IoT Platform console.

  2. On the Overview page, 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. On the Data Forwarding page, click Go to New Version in the upper-right corner.

    Note

    If you have performed this operation before, you are automatically redirected to the new version of the Data Forwarding page.

  5. Click the Data destination tab, and then click Create data destination.

  6. In the Create Data Destination dialog box, enter a name for the data destination. Example: DataPurpose. Configure other parameters and click OK. The following table describes the parameters.

    Parameter

    Description

    Operation

    Select Store Data to ApsaraDB for Lindorm.

    Authorize

    Click Authorize. The system automatically creates a role and a policy that authorize IoT Platform to write data to Lindorm. For more information, see AliyunServiceRoleForIoTRuleengineLindorm.

    Note

    If IoT Platform is already authorized, skip this step.

    Region

    The region where the IoT Platform instance resides.

    Engine Type

    Select Time Series Engine.

    Lindorm Instance

    The Lindorm instance that you created in a VPC.

    Database

    The name of the database to receive data. The database must belong to the Lindorm instance.

    Table Name

    The name of the table to receive data. The table must belong to the database.

    Account

    The account name and password for managing the time series database.

    For more information about database management accounts and passwords, see User and permission management.

    Password

Configure and start a parser

  1. Create a parser, such as DataParser. For more information, see Step 1: Create a parser.

  2. On the Resolver Details page, associate the data source.

    1. In the configuration wizard, under Data Source, click Associate Data Source.

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

  3. On the Resolver Details page, you can associate data destinations.

    1. In the configuration wizard, click Data Destination. Then, in the upper-right corner of the Data Destination list, click Associate Data Destination.

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

    3. In the data destination list, find the Data Destination ID and record it. For example, 1000.

      You will need this Data Destination ID in the parsing script.

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

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

    For more information about function parameters, see Function list.

    // Call the payload() function to obtain the data that is submitted by a device 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;
    
    var fields = {"temperature":t, "humidity":h};
    var tags = {"device_id": deviceName(), "region": "cn-shanghai"};
    var timestamp = timestamp();
    // Forward the submitted TSL data. 
    writeLindorm(1000, timestamp, tags, fields);
    Note

    If the data type of the corresponding table field in the Lindorm database is Double but the device submits humidity values as INT, use the following code to convert the field to Double for successful data forwarding:

    varfields={        
               "temperature":t,        
               "humidity":{            
                  "type":"DOUBLE",            
                  "value":h        
               }    
    };
  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 is executed:

    调试结果

  7. Click Publish.

  8. Return to the Resolver tab on the Data Forwarding page and click Start for the DataParser resolver.

  9. Simulate the device to submit TSL data and view the data forwarding result. For more information, see Device emulator.

    After the simulated device data is forwarded to the Lindorm time series database, you can call the query operation of a LindormTSDB SDK to retrieve the forwarded data.