Save data to InfluxDB

Setup a InfluxDB database, taking Mac OSX for instance:

  1. $ docker pull influxdb
  2. $ git clone -b v1.0.0 https://github.com/palkan/influx_udp.git
  3. $ cd influx_udp
  4. $ docker run --name=influxdb --rm -d -p 8086:8086 -p 8089:8089/udp -v ${PWD}/files/influxdb.conf:/etc/influxdb/influxdb.conf:ro -e INFLUXDB_DB=db influxdb:latest

Create a rule:

Go to EMQX DashboardSave data to InfluxDB - 图1 (opens new window), select the “rule” tab on the menu to the left.

Select “message.publish”, then type in the following SQL:

  1. SELECT
  2. payload.host as host,
  3. payload.location as location,
  4. payload.internal as internal,
  5. payload.external as external
  6. FROM
  7. "message.publish"

image

Bind an action:

Click on the “+ Add” button under “Action Handler”, and then select “Data to InfluxDB” in the pop-up dialog window.

image

Fill in the parameters required by the action:

Six parameters is required by action “Data to InfluxDB” :

1). Measurement: Measurement of the data point.

2). Field Keys: Which fields can be used as fields of data point.

3). Tags Keys. Which fields can be used as tags of data point.

4). Timestamp Key. Which fields can be used as timestamp of data point.

5). Set Timestamp. Whether to generate a timestamp if ‘Timestamp Key’ is not configured.

image

6). Bind a resource to the action. Since the dropdown list “Resource” is empty for now, we create a new resource by clicking on the “New Resource” to the top right, and then select “InfluxDB”:

image

Configure the resource:

Keep all the configs as default, and click on the “Testing Connection” button to make sure the connection can be created successfully, and then click on the “Create” button.

image

Back to the “Actions” dialog, and then click on the “Confirm” button.

image

Back to the creating rule page, then click on “Create” button. The rule we created will be show in the rule list:

image

We have finished, testing the rule by sending an MQTT message to emqx:

  1. > Topic: "t/1"
  2. >
  3. > QoS: 0
  4. >
  5. > Retained: false
  6. >
  7. > Payload:
  8. > {"host":"serverA","location":"roomA","internal":25,"external":37}

Then inspect the InfluxDB table, verify a new record has been inserted:

  1. $ docker exec -it influxdb influx
  2. > use db
  3. Using database db
  4. > select * from "temperature"
  5. name: temperature
  6. time external host internal location
  7. ---- -------- ---- -------- --------
  8. 1561535778444457348 35 serverA 25 roomA

And from the rule list, verify that the “Matched” column has increased to 1:

image