This topic describes how to use Function Compute-based edge applications to store device data to open source time series databases. In this example, an InfluxDB database is specified.

Prerequisites

  • An edge instance is created. For more information about how to create an edge instance, see Build environments for Link IoT Edge Pro Edition.
  • A LightSensor device is created and assigned to the edge instance. For more information about how to create the LightSensor product and device, see Sample drivers.
  • A container image-based edge application is created. For more information about how to create a container image-based edge application, see Use container images to create edge applications.

    The following two tables describe parameters to be specified.

    Table 1. Application parameters
    Parameter Description
    Application Type Select Container Image.
    Repository Type Select Public Repository.
    Image Address Enter influxdb:latest.
    Application Version The version of the database. Example: 1.7.9.
    Table 2. Container parameters
    Parameter Description
    Host Mode Select No.
    Network Port Mapping Configure the following port mappings for network connections.

    First port mapping:

    • Host Port: 8083
    • Container Port: 8083
    • Protocol Type: TCP

    Second port mapping:

    • Host Port: 8086
    • Container Port: 8086
    • Protocol Type: TCP
    Privilege Mode Select Yes.
    Volume Mapping Configure the volume mapping as follows.
    • Source Path: /tmp/influxdb
    • Destination Path: /var/lib/influxdb
    • Read/Write Permissions: Read/Write

Background information

In IoT scenarios such as buildings, industries, and agriculture, various sensors on site are responsible for collecting data regarding temperature, humidity, and air quality in real time. The data that is continuously updated over time is called time series data. To perform big data analysis and present data on a dashboard, you must store the data to time series databases.

Link IoT Edge provides the function compute capability to help you store modeled device data to time series databases. Link IoT Edge eliminates your need of secondary development. You only need to configure a message route for a Function Compute-based edge application and deploy the application.

The following example describes how to store data that the LightSensor device generates to an InfluxDB database based on timestamps.

1. Create a function

  1. Download the following function package:
  2. Log on to the Function Compute console.
    If you have not activated Function Compute, read the terms, select I have read and agree, and then click Activate Now.
  3. Optional. In the left-side navigation pane, click Services and Functions. On the Services and Functions page, click Create Service. On the Create Service page, set the parameters as required and click Submit.
    The Service Name parameter is required. In this example, the Service Name parameter is set to EdgeFC. You can set other parameters based on your needs.
    Note If the EdgeFC service has been created for other scenarios or applications, you do not need to recreate the service.
  4. After you create the service, you must create a function in the service. On the Services and Functions page, click Create Function. On the Create Function page, click Configure and Deploy in the Event Function section.
  5. Configure the following primary parameters of the function.
    Parameter Description
    Service Name The service where the function resides. Select EdgeFC.
    Function Name Enter influxdbStore.
    Runtime The runtime environment of the function. Select python3.
    Function Handler The handler of the function. Use the default value index.handler.
    Memory The size of memory that is required to execute the function. Select 512MB.
    Timeout The timeout period of the function. Enter 10. Unit: seconds.
    Single Instance Concurrency The number of concurrent requests that can be processed by an instance. Use the default value.

    You can set other parameters based on your needs or leave them unspecified. For more information, see What is Function Compute?

    Confirm the function parameters and click Create.

  6. After the function is created, the function details page appears. On the Code tab, select Upload Zip File and click Select File to upload the fcApp_influxdb_v1.0.zip package that you downloaded in step 1. Then, click Save.
    After the package is uploaded, you can view the source code on the In-line Edit tab.The In-line Edit tab
    Note The influxdbStore sample code includes the following three operations.
    1. Parse product keys and device names from the event parameter that the device submits and create table names based on product keys and device names.
      event_json = json.loads(event)
      if(event_json["topic"].startswith('/sys'))
        pk, dn, attribute = parseTopic(event_json['topic'])
    2. Parse the payload parameter. This parameter indicates the properties and events of the device.
      
      payload_json = json.loads(event_json["payload"])
    3. Encapsulate property or event data based on the data format that is supported by InfluxDB. For more information about the data format, see Data format. This operation depends on the InfluxDB client API that is built by using Python.

2. Assign the function to the edge instance

  1. In the left-side navigation pane, click Applications.
  2. Create a Function Compute-based edge application from the function that is created in step 1: Create a function. For more information about how to create a Function Compute-based edge application, see Use Function Compute to create edge applications.

    The following table describes the application parameters.

    Parameter Description
    Application Name The name of the application. Example: influxdbStore.
    Application Type Select Function Compute.
    Region Select the region where the service is deployed.
    Service Select EdgeFC.
    Function Select influxdbStore.
    Authorization Select AliyunIOTAccessingFCRole.
    Application Version The version of the application. You must specify a unique version number for a single application.

    The following table describes the function parameters.

    Parameter Description
    Running Mode Two running modes are available. In this example, select Continuous. The application runs immediately after being deployed.
    Memory Limit (MB) The maximum memory that is available for running the function. Unit: MB. Enter 512. If the memory that is used by the function exceeds the limit, the edge application is forced to restart.
    Timeout Limit (Seconds) The maximum processing period after the function receives an event. Use the default value 5. If the function does not return the result within the specified period, the edge application is forced to restart.
    Scheduled Execution Use the default value Close.
    Environmental Variables Click + Add Environment Variable and refer to the following table to add an environment variable.
    Table 3. Environment variables
    Variable Value
    host The endpoint for accessing InfluxDB.
    port The port for accessing InfluxDB. Default value: 8086.
    username The username for accessing InfluxDB. Default value: root.
    password The password for accessing InfluxDB. Default value: root.
    database The name of the InfluxDB database. Default value: example.

    You do not need to specify other parameters.

  3. In the left-side navigation pane, click Edge Instances.
  4. Find the created edge instance and click View in the Actions column.
  5. On the Instance Details page, click the Edge Applications tab. On the Edge Applications tab, click Assign Application.
  6. Assign the influxdbStore function to the edge instance, and click Close.

3. Configure message routing

For more information about how to add a message route and set parameters, see Configure message routing.

  1. On the Instance Details page, click the Message Routing tab. On this tab, click Assign Route to add a message route from the LightSenor device to Function Compute.
  2. Specify the following parameters based on prompted instructions, and click OK.
    Parameter Description
    Route Name The name of the message route.
    Message Source Select Device and choose LightSensor > LightSensor.
    Message Topic Filter Select All.
    Message Destination Select Function Compute. Then, select EdgeFC/influxdbStore.

4. Deploy the edge instance

  1. On the Instance Details page, click Deploy in the upper right corner. In the message that appears, click OK to assign resources such as sub-devices and Function Compute-based edge applications to the edge instance.
  2. Log on to your gateway and run the tail -f /linkedge/run/logger/fc-base/influxdbStore/log.INFO command to view the logs and check the running status of the application.
    Code snippets

Data query

After data is stored to the InfluxDB database, you can query the stored data by calling InfluxDB API operations. The following examples describe how to query data. For more information, see the InfluxDB API documentation.

  • Query the values of a specific property at all time points based on the product dimension. The result lists all stored property values in chronological order.
    curl -G 'http://localhost:8086/query? db=example&u=root&p=root' --data-urlencode "epoch=ms" -- data-urlencode 'q=SELECT * FROM "the unique identifier of a property" WHERE "_productKey" = $pk and "_dataType" = $type' --data-urlencode 'params={"pk": "ProductKey", "type": "property"}'
    
    //Response
    {
        "results": [{
            "statement_id": 0,
            "series": [{
                "name": "temp",
                "columns": ["time", "_dataType", "_deviceName", "_productKey", "value"],
                "values": [
                    [1570782663561, "property", "0987654321", "ProductKey", 100.1],
                    [1570782688527, "property", "0987654321", "ProductKey", 100.2],
                    [1570782851708, "property", "0987654321", "ProductKey", 104.4]
                ]
            }]
        }]
    }
  • Query the values of a specific property at all time points based on the device dimension. The result lists all stored property values in chronological order.
    curl -G 'http://localhost:8086/query? db=example&u=root&p=root' --data-urlencode "epoch=ms" -- data-urlencode 'q=SELECT * FROM "the unique identifier of a property" WHERE "_deviceName" = $dn and "_dataType" = $type' --data-urlencode 'params={"dn": "DeviceName ", "type": "property"}'
  • Query the values of a specific property within a specified time range based on the device dimension. The result lists all stored property values in chronological order.
    curl -G 'http://localhost:8086/query? db=example&u=root&p=root' --data-urlencode "epoch=ms" -- data-urlencode 'q=SELECT * FROM "the unique identifier of a property" WHERE "_productKey" = $pk and "_dataType" = $type and "time" > $timestamp' --data-urlencode 'params={"pk": "ProductKey", "type": "property", "timestamp": "2019-10-11T09:03:15.611Z"}'

Data format

The property and event data of devices is stored in the following format in InfluxDB.

  • Single-value property
    [
        {
          "measurement": "temp",            //Property name
          "tags": {                         //Property tags
            "_productKey": "1234567890",    //Product key
            "_deviceName": "0987654321",    //Device name
            "_dataType"  : "property"       //You can specify property or event for this parameter.
          },
          "time": 1346846400000,            //Timestamp. Unit: ms.
          "fields": {
            "value": 123                    //Property value
          }
        }
        ...
    ]
  • Muti-value properties
    [
        {
          "measurement": "temp",            //Property name
          "tags": {                         //Property tags
            "_productKey": "1234567890",    //Product key
            "_deviceName": "0987654321",    //Device name
            "_dataType"  : "property"       //You can specify property or event for this parameter.
          },
          "time": 1346846400000,            //Timestamp. Unit: ms.
          "fields": {
            "aa": 12,                       //Property value
            "bb": 12,                       //Property value
            "cc": 12,                       //Property value
            "dd": 12,                       //Property value
          }
        }
        ...
    ]
  • Events
    [
        {
          "measurement": "event123",        //Event name
          "tags": {                         //Property tags
            "_productKey": "1234567890",    //Product key
            "_deviceName": "0987654321",    //Device name
            "_dataType"  : "property"       //You can specify property or event for this parameter.
          },
          "time": 1346846400000,            //Timestamp. Unit: ms.
          "fields": {
            "speed" : 20.8,
            "level" : 4,
            "direction" : "East",
          }
        }
        ...
    ]