MQTT Protocol

MQTTMQTT - 图1 (opens new window) is a machine-to-machine (M2M)/“Internet of Things” connectivity protocol. It was designed as an extremely lightweight publish/subscribe messaging transport. It is useful for connections with remote locations where a small code footprint is required and/or network bandwidth is at a premium.

IoTDB supports the MQTT v3.1(an OASIS Standard) protocol. IoTDB server includes a built-in MQTT service that allows remote devices send messages into IoTDB server directly.

MQTT - 图2

Built-in MQTT Service

The Built-in MQTT Service provide the ability of direct connection to IoTDB through MQTT. It listen the publish messages from MQTT clients and then write the data into storage immediately. The MQTT topic corresponds to IoTDB timeseries. The messages payload can be format to events by PayloadFormatter which loaded by java SPI, and the default implementation is JSONPayloadFormatter. The default json formatter support two json format, and the following is an MQTT message payload example:

  1. {
  2. "device":"root.sg.d1",
  3. "timestamp":1586076045524,
  4. "measurements":["s1","s2"],
  5. "values":[0.530635,0.530635]
  6. }

or

  1. {
  2. "device":"root.sg.d1",
  3. "timestamps":[1586076045524,1586076065526],
  4. "measurements":["s1","s2"],
  5. "values":[[0.530635,0.530635], [0.530655,0.530695]]
  6. }

MQTT - 图3

MQTT Configurations

The IoTDB MQTT service load configurations from ${IOTDB_HOME}/${IOTDB_CONF}/iotdb-engine.properties by default.

Configurations are as follows:

NAMEDESCRIPTIONDEFAULT
enable_mqtt_servicewhether to enable the mqtt servicefalse
mqtt_hostthe mqtt service binding host0.0.0.0
mqtt_portthe mqtt service binding port1883
mqtt_handler_pool_sizethe handler pool size for handing the mqtt messages1
mqtt_payload_formatterthe mqtt message payload formatterjson
mqtt_max_message_sizethe max mqtt message size in byte1048576

Coding Examples

The following is an example which a mqtt client send messages to IoTDB server.

  1. MQTT mqtt = new MQTT();
  2. mqtt.setHost("127.0.0.1", 1883);
  3. mqtt.setUserName("root");
  4. mqtt.setPassword("root");
  5. BlockingConnection connection = mqtt.blockingConnection();
  6. connection.connect();
  7. Random random = new Random();
  8. for (int i = 0; i < 10; i++) {
  9. String payload = String.format("{\n" +
  10. "\"device\":\"root.sg.d1\",\n" +
  11. "\"timestamp\":%d,\n" +
  12. "\"measurements\":[\"s1\"],\n" +
  13. "\"values\":[%f]\n" +
  14. "}", System.currentTimeMillis(), random.nextDouble());
  15. connection.publish("root.sg.d1.s1", payload.getBytes(), QoS.AT_LEAST_ONCE, false);
  16. }
  17. connection.disconnect();

Customize your MQTT Message Format

If you do not like the above Json format, you can customize your MQTT Message format by just writing several lines of codes. An example can be found in example/mqtt-customize project.

Steps:

  • Create a java project, and add dependency:
  1. <dependency>
  2. <groupId>org.apache.iotdb</groupId>
  3. <artifactId>iotdb-server</artifactId>
  4. <version>${project.version}</version>
  5. </dependency>
  • Define your implementation which implements org.apache.iotdb.db.mqtt.PayloadFormatter.java e.g.,
  1. package org.apache.iotdb.mqtt.server;
  2. import io.netty.buffer.ByteBuf;
  3. import org.apache.iotdb.db.protocol.mqtt.Message;
  4. import org.apache.iotdb.db.protocol.mqtt.PayloadFormatter;
  5. import java.nio.charset.StandardCharsets;
  6. import java.util.ArrayList;
  7. import java.util.Arrays;
  8. import java.util.List;
  9. public class CustomizedJsonPayloadFormatter implements PayloadFormatter {
  10. @Override
  11. public List<Message> format(ByteBuf payload) {
  12. // Suppose the payload is a json format
  13. if (payload == null) {
  14. return null;
  15. }
  16. String json = payload.toString(StandardCharsets.UTF_8);
  17. // parse data from the json and generate Messages and put them into List<Meesage> ret
  18. List<Message> ret = new ArrayList<>();
  19. // this is just an example, so we just generate some Messages directly
  20. for (int i = 0; i < 2; i++) {
  21. long ts = i;
  22. Message message = new Message();
  23. message.setDevice("d" + i);
  24. message.setTimestamp(ts);
  25. message.setMeasurements(Arrays.asList("s1", "s2"));
  26. message.setValues(Arrays.asList("4.0" + i, "5.0" + i));
  27. ret.add(message);
  28. }
  29. return ret;
  30. }
  31. @Override
  32. public String getName() {
  33. // set the value of mqtt_payload_formatter in iotdb-engine.properties as the following string:
  34. return "CustomizedJson";
  35. }
  36. }
  • modify the file in src/main/resources/META-INF/services/org.apache.iotdb.db.mqtt.PayloadFormatter: clean the file and put your implementation class name into the file. In this example, the content is: org.apache.iotdb.mqtt.server.CustomizedJsonPayloadFormatter
  • compile your implementation as a jar file: mvn package -DskipTests

Then, in your server:

  • put the jar into your server’s lib folder
  • Update configuration to enable MQTT service. (enable_mqtt_service=true in conf/iotdb-engine.properties)
  • Set the value of mqtt_payload_formatter in conf/iotdb-engine.properties as the value of getName() in your implementation , in this example, the value is CustomizedJson
  • Launch the IoTDB server.
  • Now IoTDB will use your implementation to parse the MQTT message.

More: the message format can be anything you want. For example, if it is a binary format, just use payload.forEachByte() or payload.array to get bytes content.