Forward data to ApsaraDB RDS
The data forwarding feature lets you parse and process device data by using scripts and functions, and then forward the data to an ApsaraDB RDS (RDS) instance for storage.This topic uses a Thing Specification Language model data reporting topic as an example to describe the complete data forwarding process.
How it works
The data forwarding feature sends device topic messages to a data table in an RDS instance database. For more information, see ApsaraDB RDS.
In the preceding figure:
-
Data source: For information about supported topic message types, see Data formats (for products and devices other than cloud gateways), Custom topics (for MQTT cloud gateways), Message forwarding topics (for GB/T 32960 cloud gateways), Message forwarding topics (for JT/T 808 cloud gateways), and Message forwarding topics (for SL 651 cloud gateways).
-
Data destination: A data table in a VPC-based ApsaraDB RDS for MySQL or ApsaraDB RDS for SQL Server instance that receives device data.
-
Resolver script: A script that uses data forwarding functions to forward topic data to an RDS data table.
-
Add data: Call the
writeRds(destinationId, data)function to add data to the RDS data table. -
Update data: Call the
updateRds(destinationId, data, condition, limit)function to update existing data in the RDS data table.
The
dataparameter supports only Map data, where the key is the database table column name and the value is the column value. For more information about the functions, see Function list. -
Limits
-
Region limits:
-
Data forwarding to ApsaraDB RDS is supported only for IoT Platform instances in specific regions. For more information about the supported regions, see Features available in different regions.
-
The RDS instance must be in the same region as the IoT Platform instance. For example, data from an IoT Platform instance in China (Shanghai) can be forwarded only to an RDS data table in China (Shanghai).
-
-
Data can be forwarded only to RDS instances in a virtual private cloud (VPC).
-
ApsaraDB RDS for MySQL and ApsaraDB RDS for SQL Server instances are supported.
-
Data forwarding is supported for both standard and high-privilege databases.
-
Only data in JSON format can be forwarded.
-
Both the new and previous versions of data forwarding support forwarding data to ApsaraDB RDS. For a previous-version example, see Forward data to ApsaraDB RDS (previous version).
-
For more information about message forwarding limits for an IoT Platform instance, see Limits.
Prerequisites
-
You have added a data source for the device topic. For example, you have created a data source named DataSource and added the Thing Specification Language model data reporting topic for a specific device. For more information, see Add a data source.
-
An ApsaraDB RDS for MySQL or ApsaraDB RDS for SQL Server instance in a VPC, located in the same region as your IoT Platform instance. A database and a table must be created in the instance. For more information about how to use ApsaraDB RDS, see the ApsaraDB RDS documentation.
Background information
After you configure the data destination, the IoT Platform rules engine automatically performs the following configurations to forward device data to ApsaraDB RDS.
-
IoT Platform consumes two IP addresses from the vSwitch where the ApsaraDB RDS instance resides. The IPv4 CIDR block of the vSwitch is added to the whitelist of the ApsaraDB RDS instance.
-
A managed security group is created in the VPC where the ApsaraDB RDS instance is located. The name of the security group starts with sg-nsm- by default.
Create a data destination
-
Log on to the IoT Platform console.
On the Overview page, find the instance that you want to manage and click the instance ID or instance name.
-
In the left-side navigation pane, choose .
-
On the Data Forwarding page, click Go to New Version in the upper-right corner.
NoteIf you have performed this operation before, you are automatically redirected to the new version of the Data Forwarding page.
- Click the Data Destination tab. On this tab, click Create Data Destination.
-
In the Create Data Destination dialog box, enter a name for the data destination, such as DataPurpose. Set the parameters as described in the following table, and then click OK.
Parameter
Description
Select Operation
Select Store Data to ApsaraDB RDS.
Region
The region where your IoT Platform instance resides. This parameter is fixed.
RDS Instance
Select the destination ApsaraDB RDS instance.
Database
Enter the name of the database.
NoteFor a high-privilege database, you must enter the database name manually.
Account
Enter the username for the ApsaraDB RDS instance. This account must have read and write permissions on the database. Otherwise, the rules engine cannot write data to the ApsaraDB RDS instance.
NoteAfter the rules engine obtains the account, it only writes data that matches the rule to the database and performs no other operations.
Password
Enter the password for the database account.
Table Name
Enter the name of the table that you created in the database. The rules engine writes data to this table.
Role
Select a RAM role that allows IoT Platform to write data to ApsaraDB RDS.
If no RAM role exists, click Create RAM Role to create a RAM role and authorization policy in the RAM console. Create a RAM role.
Configure and start the Resolver
Create a parser named DataParser. For more information, see Step 1: Create a parser.
On the Parser Details page, associate the parser with the created data source.
In the Data Source step of the wizard, click Associate Data Source.
In the dialog box that appears, select DataSource from the Data Source drop-down list, and then click OK.
On the Parser Details page, associate the parser with the created data destination.
Click Data Destination in the wizard. In the Data Destination section, click Associate Data Destination.
In the dialog box that appears, select DataPurpose from the Data Destination drop-down list, and then click OK.
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.
- On the Parser Details page, click Parser.
-
In the script editor, enter a Resolver script.
The Resolver script syntax is based on JavaScript. For more information about how to edit scripts, see Script syntax.
For more information about function parameters, see Function list.
-
Write data:
// Use the payload() function to get the message content reported by the device and convert it to the JSON format. var data = payload("json"); // Filter the reported temperature and humidity values. var h = data.items.Humidity.value; var t = data.items.Temperature.value; // The RDS table schema is id (auto-increment primary key), deviceName, temperature, humidity, and time. In the writeRds method, you can write values to the corresponding columns in the column:value format. // Set the rule to trigger when the temperature is greater than 38. if (t > 38) { writeRds(1000, {"deviceName":deviceName(), "temperature":t, "time":timestamp(), "humidity":h}); } -
Update data:
// Use the payload() function to get the message content reported by the device and convert it to the JSON format. var data = payload("json"); // Filter the reported temperature and humidity values. var h = data.items.Humidity.value; var t = data.items.Temperature.value; // The RDS table schema is id (auto-increment primary key), deviceName, temperature, humidity, and time. In the updateRds method, you can update values in the corresponding columns in the column:value format. // Specify the device name as the condition to update data. var condition = "deviceName = '" + deviceName() +"'"; // Update a row of data with the temperature and humidity for the specified device. If multiple rows of data meet the condition, the update starts from the first row of the result set by default. limit is set to 1, which means only the first row is updated. updateRds(1000, {"temperature":t, "time":timestamp(), "humidity":h}, condition, 1);
-
-
Click Debug. Follow the on-screen instructions to select a product and a device, enter the topic and payload data, and verify that the script runs as expected.
The following figure shows an example of the parameters:

The following output confirms that the script successfully wrote the data.

-
Click Publish.
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.
-
In the ApsaraDB RDS console, on the Data Security page, configure the whitelist. The rules engine requires a connection to ApsaraDB RDS. After you configure the data destination, the rules engine automatically adds its IP addresses to the ApsaraDB RDS whitelist. If the following IP addresses are not in the whitelist, you must add them manually.ApsaraDB RDS
-
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 -
Asia Pacific (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
In the left-side navigation pane for your ApsaraDB RDS instance, click Data Security. On the Whitelist settings tab, click Add whitelist group and add the IP address CIDR blocks for the corresponding IoT Platform region to the group.
-
What to do next
-
In the IoT Platform console, go to . On the IoT Platform Logs tab, you can view the operational logs for Device-to-Cloud Messages and Data Forwarding. For more information, see IoT Platform logs.
-
You can connect to the RDS instance and log on to the database to view the forwarded data. For more information, see Connect to an ApsaraDB for MySQL instance and Connect to an ApsaraDB for SQL Server instance.
References
-
To connect a device to IoT Platform and report data, see Device connection guide.
-
On the instance details page, check the available message forwarding TPS resources to verify that device messages can be forwarded to the RDS database. For more information, see View instance information and operational data. If resources are insufficient, upgrade the instance. For more information, see Upgrade an instance.
-
For frequently asked questions about ApsaraDB RDS databases, see FAQ about databases.