InfluxDB Backend

    After EMQX version 3.1, a powerful rule engine is introduced to replace plug-ins. It is recommended that you use it. See Save data to InfluxDB to setup Save data to InfluxDB in rule engine.

    Config file: etc/plugins/emqx_backend_influxdb.conf:

    Parameters in hook rule:

    Example:

    1. ## Store PUBLISH message whose topic is "sensor/#"
    2. backend.influxdb.hook.message.publish.1 = {"topic": "sensor/#", "action": {"function": "on_message_publish"}, "pool": "pool1"}
    3. ## Store PUBLISH message whose topic is "stat/#"
    4. backend.influxdb.hook.message.publish.2 = {"topic": "stat/#", "action": {"function": "on_message_publish"}, "pool": "pool1"}

    Since MQTT Message cannot be written directly to InfluxDB, InfluxDB Backend provides an emqx_backend_influxdb.tmpl template file to convert MQTT Message to DataPoint that can be written to InfluxDB.

    Template file use Json format:

    • key - MQTT Topic, Json String, support wildcard characters
    • value - Template, Json Object, used to convert MQTT Message into measurement,tag_key=tag_value,... field_key=field_value,... timestamp and write to InfluxDB。
    1. {
    2. <Topic 1>: <Template 1>,
    3. <Topic 2>: <Template 2>
    4. }

    Template format:

    measurement and fields are required options, tags and timestamp are optional.

    All values (such as <Measurement>) can be configured directly in the template as a fixed value that data types supported depending on the table you define. More realistically, of course, you can access the data in the MQTT message through the placeholder we provide.

    Currently, we support placeholders as follows:

    $payload and $<Number>:

    You can directly use $content to obtain the complete message payload, you can use ["$payload", <Key>, ...] to get the data inside the message payload.

    In the case of array data type in Json, we introduced $0 and $<pos_integer>, $0 means to get all elements in the array, and $<pos_integer> means to get the <pos_integer>th element in the array.

    A simple example, ["$payload", "$0", "temp"] will get [20, 21] from [{"temp": 20}, {"temp": 21}], and ["$payload", "$1", "temp"] will only get 20.

    It is worth noting that when you use $0, we expect the number of data you get is same. Because we need to convert these arrays into multiple records and write it into InfluxDB, and when you have three pieces of data in one field and two in another, we won’t know how to combine the data for you.

    Example

    data/templates directory provides a sample template (emqx_backend_influxdb_example.tmpl, please remove the “_example” suffix from the filename when using it formally) for the user’s reference:

    1. "sample": {
    2. "measurement": "$topic",
    3. "tags": {
    4. "host": ["$payload", "data", "$0", "host"],
    5. "region": ["$payload", "data", "$0", "region"],
    6. "qos": "$qos",
    7. "clientid": "$clientid"
    8. "fields": {
    9. "temperature": ["$payload", "data", "$0", "temp"]
    10. },
    11. "timestamp": "$timestamp"
    12. }
    13. }

    When an MQTT Message whose Topic is “sample” has the following Payload:

    1. "data": [
    2. {
    3. "temp": 1,
    4. "host": "serverA",
    5. "region": "hangzhou"
    6. },
    7. {
    8. "temp": 2,
    9. "host": "serverB",
    10. "region": "ningbo"
    11. }
    12. ]
    13. }

    The data was finally encoded and written to InfluxDB as follows: