[MQTT](Message Queuing Telemetry Transport)(http://mqtt.org/) is a lightweight messaging protocol designed for IoT and low-bandwidth environments. It operates on a Publish/Subscribe (Pub/Sub) model, enabling efficient and reliable bidirectional communication between devices. Its core objectives are low power consumption, minimal bandwidth usage, and high real-time performance, making it ideal for unstable networks or resource-constrained scenarios (e.g., sensors, mobile devices).
IoTDB provides deep integration with the MQTT protocol, fully compliant with MQTT v3.1 (OASIS International Standard). The IoTDB server includes a built-in high-performance MQTT Broker module, eliminating the need for third-party middleware. Devices can directly write time-series data into the IoTDB storage engine via MQTT messages.
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 its json array. The following is an MQTT message payload example:
{ "device":"root.sg.d1", "timestamp":1586076045524, "measurements":["s1","s2"], "values":[0.530635,0.530635] }
or
{ "device":"root.sg.d1", "timestamps":[1586076045524,1586076065526], "measurements":["s1","s2"], "values":[[0.530635,0.530635], [0.530655,0.530695]] }
or json array of the above two.
The IoTDB MQTT service load configurations from ${IOTDB_HOME}/${IOTDB_CONF}/iotdb-system.properties by default.
Configurations are as follows:
| Property | DESCRIPTION | DEFAULT |
|---|---|---|
enable_mqtt_service | whether to enable the mqtt service | false |
mqtt_host | the mqtt service binding host | 127.0.0.1 |
mqtt_port | the mqtt service binding port | 1883 |
mqtt_handler_pool_size | the handler pool size for handing the mqtt messages | 1 |
mqtt_payload_formatter | Formatting method for MQTT message payloads. Options: json (tree model), line (table model). | json |
mqtt_max_message_size | the max mqtt message size in byte | 1048576 |
The following is an example which a mqtt client send messages to IoTDB server.
MQTT mqtt = new MQTT(); mqtt.setHost("127.0.0.1", 1883); mqtt.setUserName("root"); mqtt.setPassword("root"); BlockingConnection connection = mqtt.blockingConnection(); connection.connect(); Random random = new Random(); for (int i = 0; i < 10; i++) { String payload = String.format("{\n" + "\"device\":\"root.sg.d1\",\n" + "\"timestamp\":%d,\n" + "\"measurements\":[\"s1\"],\n" + "\"values\":[%f]\n" + "}", System.currentTimeMillis(), random.nextDouble()); connection.publish("root.sg.d1.s1", payload.getBytes(), QoS.AT_LEAST_ONCE, false); } connection.disconnect();
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:
<dependency> <groupId>org.apache.iotdb</groupId> <artifactId>iotdb-server</artifactId> <version>2.0.4-SNAPSHOT</version> </dependency>
org.apache.iotdb.db.protocol.mqtt.PayloadFormatter e.g.,package org.apache.iotdb.mqtt.server; import io.netty.buffer.ByteBuf; import org.apache.iotdb.db.protocol.mqtt.Message; import org.apache.iotdb.db.protocol.mqtt.PayloadFormatter; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class CustomizedJsonPayloadFormatter implements PayloadFormatter { @Override public List<Message> format(String topic, ByteBuf payload) { // Suppose the payload is a json format if (payload == null) { return null; } String json = payload.toString(StandardCharsets.UTF_8); // parse data from the json and generate Messages and put them into List<Meesage> ret List<Message> ret = new ArrayList<>(); // this is just an example, so we just generate some Messages directly for (int i = 0; i < 2; i++) { long ts = i; Message message = new Message(); message.setDevice("d" + i); message.setTimestamp(ts); message.setMeasurements(Arrays.asList("s1", "s2")); message.setValues(Arrays.asList("4.0" + i, "5.0" + i)); ret.add(message); } return ret; } @Override public String getName() { // set the value of mqtt_payload_formatter in iotdb-system.properties as the following string: return "CustomizedJson"; } }
src/main/resources/META-INF/services/org.apache.iotdb.db.protocol.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.CustomizedJsonPayloadFormattermvn package -DskipTestsThen, in your server:
enable_mqtt_service=true in conf/iotdb-system.properties)mqtt_payload_formatter in conf/iotdb-system.properties as the value of getName() in your implementation , in this example, the value is CustomizedJsonMore: 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.
To avoid compatibility issues caused by a default client_id, always explicitly supply a unique, non-empty client_id in every MQTT client. Behavior varies when the client_id is missing or empty. Common examples: