You can configure a parser to forward processed data to Lindorm. This topic describes the data forwarding process. In this example, a Thing Specification Language (TSL) communication topic is used as a source topic.
Prerequisites
An IoT Platform instance whose data can be forwarded to Lindorm is created in a region that supports data forwarding to Lindorm. For more information about the regions that support data forwarding to Lindorm, see Regions.
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.
A Lindorm instance is created in the Lindorm console, and the settings are configured based on your business requirements. For more information, see Create an instance.
ImportantOnly intra-region data forwarding is supported. The Lindorm instance must reside in the same region as the IoT Platform instance.
Only pay-as-you-go and subscription Lindorm instances are supported. When you purchase a Lindorm instance, you must set the Time Series Engine Nodes parameter in the Select Engine Type section to a value that is greater than or equal to 1.
LindormTSDB SDK for native Java is integrated. A time series database, a table, and an account and the corresponding password that are used to manage the database are created. The settings that allow time series data to be stored across multiple Lindorm instances are configured. For more information, see Tutorial: Use LindormTSDB SDK for Java to connect to and use LindormTSDB.
Background information
After a data destination is configured, the system automatically configures the following settings to forward device data to Lindorm by using the rules engine of IoT Platform.
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) in which the Lindorm instance is deployed. The default name of the security group starts with sg-nsm-.
In this example, the following data is written to the time series database:
timestamp: Call the timestamp() function to obtain the time when a device submits data.
tags: Call the
deviceName()
function to obtain the DeviceName. The{"device_id": deviceName(), "region": "cn-shanghai"}
tag is written to the database.fields: Use JSONPath expressions to obtain the values of the Temperature and Humidity properties that are submitted by the device. The values are written to the database.
Limits
You can use only the new version of the data forwarding feature to forward data to Lindorm.
Create a data destination
- Log on to the IoT Platform console.
On the Overview page, click All environment. On the All environment tab, find the instance that you want to manage and click the instance ID or instance name.
- In the left-side navigation pane, choose .
In the upper-right corner of the Data Forwarding page, click Go to New Version to go to the new version.
NoteIf you have performed this step, the Data Forwarding page of the new version appears after you choose Message Forwarding > Data Forwarding.
Click Data Destination. On the Data Destination tab, click Create Data Destination.
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.
NoteIf IoT Platform is already authorized, skip this step.
Region
Specify the region where the IoT Platform instance resides.
Lindorm Instance
Specify the Lindorm instance that you created in a VPC.
Database
Specify the name of the database that you want to use to receive data. The database must belong to the Lindorm instance.
Table Name
Specify the name of the table that you want to use to receive data. The table must belong to the database.
Account
Specify the name of the account and the corresponding password that you want to use to manage the time series database.
For more information about database management accounts and passwords, see User and permission management.
Password
Configure and start a parser
Create a parser named DataParser. For more information, see 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 code editor, enter a script. For more information about how 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 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);
NoteIf the data type of the corresponding table field in the Lindorm database is Double but the data type of the humidity value that is submitted by the device is INT, you must use the following code to change the data type of the field to Double to ensure successful data forwarding:
varfields={ "temperature":t, "humidity":{ "type":"DOUBLE", "value":h } };
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:
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.
Simulate the device to submit TSL data and view the data forwarding result. For more information, see Device simulation.
After the data that is submitted by the simulated device is forwarded to the Lindorm time series database, you can call the query operation of a LindormTSDB SDK to obtain the forwarded data.