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
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
2. Assign the function to the edge instance
3. Configure message routing
For more information about how to add a message route and set parameters, see Configure message routing.
4. Deploy the edge instance
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", } } ... ]