You can use the data forwarding feature of the rules engine to forward processed data to ApsaraDB RDS instances for storage.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.
  • An ApsaraDB RDS instance, a database, and a table are created in the region where your IoT Platform instance resides. The database engine of the instance must be MySQL or Microsoft SQL Server and the network type of the instance must be VPC. For more information, see the ApsaraDB RDS documentation.

Limits

  • Data forwarding is supported between IoT Platform instances and ApsaraDB RDS instances that reside in the same region. For example, you can forward the data of an IoT Platform instance in the China (Shanghai) region to an ApsaraDB RDS table that resides only in the China (Shanghai) region.
  • You can forward data to ApsaraDB RDS instances that resides only in VPCs.
  • You can forward data to MySQL and SQL Server instances.
  • Data forwarding is supported between standard databases and privileged databases.
  • You can forward data only in the JSON format.

Create a data destination

  1. Log on to the IoT Platform console.
  2. On the Overview page, find the instance and click the instance name to go to the Instance Details page.
    Notice Enterprise Edition instances are available only in the Japan (Tokyo) region. If your IoT Platform is not activated in the Japan (Tokyo) region, skip this step.
    Overview
  3. In the left-side navigation pane, choose Rules Engine > Data Forwarding.
  4. 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.
  5. Click the Data Destination tab. On this tab, click Create Data Destination.
  6. In the Create Data Destination dialog box, enter a data destination name. In this example, enter DataPurpose. Set the parameters and then click OK.
    Parameter Description
    Select Operation Select Save to RDS.
    Region Select the region where your IoT Platform instance resides.
    RDS Instance Select the ApsaraDB RDS instance.
    Database Enter the database name.
    Note If you use a privileged database, you must enter a database name.
    Account Enter the account of the ApsaraDB RDS instance. This account must have the read and write permissions on the database. Otherwise, the rules engine cannot write data to the ApsaraDB RDS instance.
    Note After the rules engine obtains the account, the rules engine writes only the data that matches the specified rule to the database. No extra operations are performed.
    Password Enter the password that is used to log on to the ApsaraDB RDS instance.
    Table Name Enter the name of the table that is created in the database. The rules engine writes data to the table.
    Role Authorize IoT Platform to write data to ApsaraDB RDS.

    If you have not created a Resource Access Management (RAM) role, click Create RAM Role to go to the RAM console, create a RAM role, and then grant permissions. 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 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"); 
    // Filter the submitted temperature and humidity values. 
    var h = data.items.Humidity.value;
    var t = data.items.Temperature.value;
    // An ApsaraDB RDS table includes the following columns: id (auto-increment primary key), deviceName, temperature, humidity, and time. You can use the writeRds() method to write values to columns by using the format of column:value. 
    // Set a rule that is triggered if the temperature value is greater than 38. 
    if (t > 38) { 
        writeRds(1000, {"devcieName":deviceName(), "temperature":t, "time":timestamp(), "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. Log on to the ApsaraDB RDS console. On the Data Security page, configure a whitelist or view whitelists. After you set the parameters, the rules engine adds the following IP addresses to the ApsaraDB RDS whitelist. Then, the rules engine can communicate with ApsaraDB RDS. If one of the following IP addresses is not displayed in the whitelist, you must manually add the IP address.
    • China (Shanghai): 100.104.53.192/26,100.104.148.64/26,100.104.6.192/26,100.104.143.128/26,100.104.76.0/24,100.104.73.128/26,100.104.200.64/26,100.104.40.64/26,100.104.3.0/26,100.104.29.128/26,100.104.121.0/26,100.104.84.64/26
    • Singapore (Singapore): 100.104.106.0/24
    • US (Silicon Valley): 100.104.8.0/24
    • US (Virginia): 100.104.133.64/26
    • Germany (Frankfurt): 100.104.160.192/26
    • Japan (Tokyo): 100.104.160.192/26
    ApsaraDB RDS database