IoTDB支持MQTT v3.1(OASIS标准)协议。IoTDB服务器包括内置的MQTT服务,该服务允许远程设备将消息直接发送到IoTDB服务器。
客户端包准备
maven
<dependency>
<groupId>org.fusesource.mqtt-client</groupId>
<artifactId>mqtt-client</artifactId>
<version>1.12</version>
</dependency>
IoTDB开启内置MQTT服务
默认false,改为true,即可开启
程序测试传值
public static void main(String[] args) throws Exception{
MQTT mqtt = new MQTT();
mqtt.setHost("127.0.0.1", 1883);
mqtt.setUserName("root");
mqtt.setPassword("root");
BlockingConnection connection = mqtt.blockingConnection();
connection.connect();
String payload = "{\n" +
"\t\"device\": \"root.test.wf01.wt01\",\n" +
"\t\"timestamp\": 1611108000000,\n" +
"\t\"measurements\": [\"temperature\", \"status\", \"hardware\"],\n" +
"\t\"values\": [21.2, true, \"hello\"]\n" +
"}";
connection.publish("root.test.wf01.wt01",payload.getBytes(), QoS.AT_LEAST_ONCE,false);
connection.disconnect();
}