diff --git a/example/mqtt/README.md b/example/mqtt/README.md index 421dcd8b7263..139e03ae36fe 100644 --- a/example/mqtt/README.md +++ b/example/mqtt/README.md @@ -31,3 +31,50 @@ The example is to show how to send data to IoTDB from a mqtt client. * Launch the IoTDB server. * Setup database `CREATE DATABASE root.sg` and create time timeseries `CREATE TIMESERIES root.sg.d1.s1 WITH DATATYPE=DOUBLE, ENCODING=PLAIN`. * Run `org.apache.iotdb.mqtt.MQTTClient` to run the mqtt client and send events to server. + +## Python client (tree model) + +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. + +```sql +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. + +```python +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], +} + +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"]}, +) +``` + +Run `SELECT s1 FROM root.sg.d1;` in the CLI to verify that the reading +`37.5` was stored. If IoTDB is still processing the message, run the +query again to check for the value.