The example is to show how to send data to IoTDB from a mqtt client.
enable_mqtt_service=true in iotdb-datanode.properties)CREATE DATABASE root.sg and create time timeseries CREATE TIMESERIES root.sg.d1.s1 WITH DATATYPE=DOUBLE, ENCODING=PLAIN.org.apache.iotdb.mqtt.MQTTClient to run the mqtt client and send events to server.Set enable_mqtt_service=true and mqtt_payload_formatter=json in conf/iotdb-system.properties, then start the local IoTDB instance.
Create the destination in an IoTDB CLI session using the tree SQL dialect.
CREATE DATABASE root.sg; CREATE TIMESERIES root.sg.d1.s1 WITH DATATYPE=DOUBLE, ENCODING=PLAIN;
Install the MQTT client with python -m pip install paho-mqtt. Publish a reading to the local MQTT port. Set IOTDB_USER and IOTDB_PASSWORD in your shell to the credentials you use in the IoTDB CLI. Keep the MQTT client ID non-empty. The MQTT account needs WRITE_DATA on root.sg.d1.s1.
import json import time from os import environ from uuid import uuid4 from paho.mqtt.publish import single reading = { "device": "root.sg.d1", "timestamp": time.time_ns() // 1_000_000, "measurements": ["s1"], "values": [37.5], } print(reading["timestamp"]) single( "root.sg.d1.s1", payload=json.dumps(reading), hostname="127.0.0.1", port=1883, client_id=f"iotdb-{uuid4().hex[:12]}", qos=1, auth={"username": environ["IOTDB_USER"], "password": environ["IOTDB_PASSWORD"]}, )
Replace <timestamp> with the printed timestamp, then run this CLI query:
SELECT s1 FROM root.sg.d1 WHERE time = <timestamp>;
Check that this row contains 37.5. If IoTDB is still processing the message, run the same query again. Returning from single() does not confirm that IoTDB stored the reading; filtering by timestamp avoids reading an earlier value.