All Products
Search
Document Center

IoT Platform:Forward data to Message Queue for Apache RocketMQ

Last Updated:Nov 15, 2024

You can use the rules engine to forward data from IoT Platform to Message Queue for Apache RocketMQ for storage. This ensures high-reliability data transmission from devices to IoT Platform, Message Queue for Apache RocketMQ, and application servers. This topic describes the data forwarding process. In this example, a Thing Specification Language (TSL) communication topic is used as the source topic.

Prerequisites

  • An IoT Platform instance whose data can be forwarded to Message Queue for Apache RocketMQ is created in a region that supports data forwarding to Message Queue for Apache RocketMQ. For more information, 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.

  • An ApsaraMQ for RocketMQ instance and a topic that can be used to receive data are created. For more information, see Get started with ApsaraMQ for RocketMQ.

    Important
    • The ApsaraMQ for RocketMQ instance must reside in the same region as the IoT Platform instance for which you want to configure a data forwarding rule. This limit does not apply to existing data forwarding rules that are used to forward data across instances in different regions.

    • You can forward data to a topic of only an ApsaraMQ for RocketMQ V4.x instance or V5.x instance.

Usage notes

  • You can use the new or previous version of the data forwarding feature to forward data to Message Queue for Apache RocketMQ. For more information about how to use the previous version of the data forwarding feature, see Forward data to ApsaraMQ for RocketMQ.

  • If you specify Send Data to ApsaraMQ for RocketMQ when you configure a data destination, the following settings are automatically configured. After the configuration is complete, you can forward device data to your ApsaraMQ for RocketMQ V5.0 instance by using the rules engine of IoT Platform.

    • Two IP addresses of the vSwitch to which the ApsaraMQ for RocketMQ instance is connected are assigned to IoT Platform.

    • A managed security group is created in the virtual private cloud (VPC) in which the ApsaraMQ for RocketMQ instance resides. By default, the name of the security group starts with sg-nsm-.

Create a data destination

  1. Log on to the IoT Platform console.
  2. 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.

  3. In the left-side navigation pane, choose Message Forwarding > Data Forwarding.
  4. In the upper-right corner of the Data Forwarding page, click Go to New Version 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 Message Forwarding > Data Forwarding.

  5. Click Data Destination. On the Data Destination tab, click Create Data Destination.

  6. In the Create Data Destination dialog box, enter a data destination name. In this example, DataPurpose is used. Configure the parameters and click OK.

    数据流转到MQ

    Parameter

    Description

    Operation

    Select Send Data to ApsaraMQ for RocketMQ.

    Region

    Select the region where the ApsaraMQ for RocketMQ instance resides.

    Instance

    Select the ApsaraMQ for RocketMQ instance.

    You can click Create Instance to go to the ApsaraMQ for RocketMQ console and create an ApsaraMQ for RocketMQ instance. For more information, see the ApsaraMQ for RocketMQ documentation.

    Topic

    Select the ApsaraMQ for RocketMQ topic that you want to use to receive data from IoT Platform.

    You can click Create Topic to go to the ApsaraMQ for RocketMQ console and create an ApsaraMQ for RocketMQ topic.

    Grant permissions

    Grant IoT Platform the permissions to write data to ApsaraMQ for RocketMQ.

    If you do not have Resource Access Management (RAM) roles, click Create RAM Role to go to the RAM console, create a RAM role, and then grant permissions to the role. 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 modify a script, see Script syntax.

    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 to JSON-formatted data. 
    var data = payload("json");
    // Forward submitted TSL data. 
    writeMq(1000, data, "debug");
  6. Click Debugging. In the panel 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 runs as expected.

    调试结果

  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. In the Message Queue for Apache RocketMQ console, check whether the message is received.