|
| 1 | +(mqtt-tutorial)= |
| 2 | + |
| 3 | +# Load data from an MQTT topic into CrateDB |
| 4 | + |
| 5 | +The tutorial will walk you through starting the [Eclipse Mosquitto] broker and CrateDB, |
| 6 | +publishing JSON data to an MQTT topic, subscribing to the topic to relay |
| 7 | +data into a CrateDB table continuously, and validating that the data has |
| 8 | +been stored successfully. |
| 9 | +The data transfer is supported by the [LorryStream MQTT source] data |
| 10 | +pipeline element. |
| 11 | + |
| 12 | +## Prerequisites |
| 13 | + |
| 14 | +Docker is used for running all components. This approach works consistently |
| 15 | +across Linux, macOS, and Windows. |
| 16 | + |
| 17 | +Alternatively, you can use Podman. You can also use a different MQTT broker such as |
| 18 | +EMQX, HiveMQ, VerneMQ, or RabbitMQ. Azure IoT Hub speaks MQTT as well, but with |
| 19 | +protocol and authentication specifics; adjust settings accordingly. |
| 20 | + |
| 21 | +Create a shared network. |
| 22 | +```shell |
| 23 | +docker network create cratedb-demo |
| 24 | +``` |
| 25 | + |
| 26 | +Start CrateDB. |
| 27 | +```shell |
| 28 | +docker run --name=cratedb --rm -it --network=cratedb-demo \ |
| 29 | + --publish=4200:4200 --publish=5432:5432 \ |
| 30 | + --env=CRATE_HEAP_SIZE=2g docker.io/crate -Cdiscovery.type=single-node |
| 31 | +``` |
| 32 | + |
| 33 | +Start Mosquitto. |
| 34 | +```shell |
| 35 | +docker run --name=mosquitto --rm -it --network=cratedb-demo \ |
| 36 | + --publish=1883:1883 docker.io/eclipse-mosquitto \ |
| 37 | + mosquitto -c /mosquitto-no-auth.conf |
| 38 | +``` |
| 39 | +> Note: This broker configuration allows anonymous access for demonstration purposes only. |
| 40 | +> Do not expose it to untrusted networks. For production, configure authentication/TLS. |
| 41 | +
|
| 42 | +Prepare shortcuts for the CrateDB shell, LorryStream, and the Mosquitto client |
| 43 | +programs. |
| 44 | + |
| 45 | +::::{tab-set} |
| 46 | + |
| 47 | +:::{tab-item} Linux and macOS |
| 48 | +To make the settings persistent, add them to your Shell profile (`~/.profile`). |
| 49 | +```shell |
| 50 | +alias crash="docker run --rm -it --network=cratedb-demo ghcr.io/crate/cratedb-toolkit crash" |
| 51 | +alias lorry="docker run --rm -i --network=cratedb-demo ghcr.io/daq-tools/lorrystream lorry" |
| 52 | +alias mosquitto_pub="docker run --rm -i --network=cratedb-demo docker.io/eclipse-mosquitto mosquitto_pub" |
| 53 | +alias mosquitto_sub="docker run --rm -i --network=cratedb-demo docker.io/eclipse-mosquitto mosquitto_sub" |
| 54 | +``` |
| 55 | +::: |
| 56 | +:::{tab-item} Windows PowerShell |
| 57 | +To make the settings persistent, add them to your PowerShell profile (`$PROFILE`). |
| 58 | +```powershell |
| 59 | +function crash { docker run --rm -it --network=cratedb-demo ghcr.io/crate/cratedb-toolkit crash @args } |
| 60 | +function lorry { docker run --rm -i --network=cratedb-demo ghcr.io/daq-tools/lorrystream lorry @args } |
| 61 | +function mosquitto_pub { docker run --rm -i --network=cratedb-demo eclipse-mosquitto mosquitto_pub @args } |
| 62 | +function mosquitto_sub { docker run --rm -i --network=cratedb-demo eclipse-mosquitto mosquitto_sub @args } |
| 63 | +``` |
| 64 | +::: |
| 65 | +:::{tab-item} Windows Command |
| 66 | +```shell |
| 67 | +doskey crash=docker run --rm -it --network=cratedb-demo ghcr.io/crate/cratedb-toolkit crash $* |
| 68 | +doskey lorry=docker run --rm -i --network=cratedb-demo ghcr.io/daq-tools/lorrystream lorry $* |
| 69 | +doskey mosquitto_pub=docker run --rm -i --network=cratedb-demo eclipse-mosquitto mosquitto_pub $* |
| 70 | +doskey mosquitto_sub=docker run --rm -i --network=cratedb-demo eclipse-mosquitto mosquitto_sub $* |
| 71 | +``` |
| 72 | +::: |
| 73 | + |
| 74 | +:::: |
| 75 | + |
| 76 | +## Usage |
| 77 | + |
| 78 | +Subscribe to all MQTT topics on the broker to monitor any traffic. |
| 79 | +```shell |
| 80 | +mosquitto_sub -h mosquitto -t "#" -v |
| 81 | +``` |
| 82 | + |
| 83 | +Invoke the data transfer pipeline. |
| 84 | +```shell |
| 85 | +lorry relay \ |
| 86 | + "mqtt://mosquitto/testdrive/%23?content-type=json" \ |
| 87 | + "crate://cratedb/?table=testdrive" |
| 88 | +``` |
| 89 | + |
| 90 | +Publish a JSON message to an MQTT topic. |
| 91 | +```shell |
| 92 | +echo '{"temperature": 42.84, "humidity": 83.1}' | \ |
| 93 | + mosquitto_pub -h mosquitto -t testdrive/channel1 -s |
| 94 | +``` |
| 95 | + |
| 96 | +Inspect data stored in CrateDB. |
| 97 | +```shell |
| 98 | +crash --hosts cratedb -c "SELECT * FROM testdrive" |
| 99 | +``` |
| 100 | + |
| 101 | + |
| 102 | +[Eclipse Mosquitto]: https://mosquitto.org/ |
| 103 | +[LorryStream MQTT source]: https://lorrystream.readthedocs.io/source/mqtt.html |
0 commit comments