|
| 1 | +(amqp-tutorial)= |
| 2 | + |
| 3 | +# Load data from an AMQP queue into CrateDB |
| 4 | + |
| 5 | +The tutorial will walk you through starting the [RabbitMQ] AMQP broker |
| 6 | +and CrateDB, publishing JSON data to an AMQP queue, consuming and relaying |
| 7 | +it into a CrateDB table continuously, and validating that the data has |
| 8 | +been stored successfully. |
| 9 | +The data transfer is supported by the [LorryStream AMQP 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 AMQP broker such as |
| 18 | +Apache Qpid, Apache ActiveMQ, IBM MQ, or Solace. Azure Event Hubs and Azure Service |
| 19 | +Bus speak AMQP as well, but with protocol and authentication specifics; adjust |
| 20 | +settings accordingly. |
| 21 | + |
| 22 | +Create a shared network. |
| 23 | +```shell |
| 24 | +docker network create cratedb-demo |
| 25 | +``` |
| 26 | + |
| 27 | +Start CrateDB. |
| 28 | +```shell |
| 29 | +docker run --name=cratedb --rm --network=cratedb-demo \ |
| 30 | + --publish=4200:4200 --publish=5432:5432 \ |
| 31 | + --env=CRATE_HEAP_SIZE=2g docker.io/crate -Cdiscovery.type=single-node |
| 32 | +``` |
| 33 | + |
| 34 | +Start RabbitMQ. |
| 35 | +```shell |
| 36 | +docker run --name=rabbitmq --rm --network=cratedb-demo \ |
| 37 | + --publish=5672:5672 docker.io/rabbitmq:3 |
| 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 AMQP 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 (e.g., `~/.profile` or `~/.zshrc`). |
| 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 amqpcat="docker run --rm -i --network=cratedb-demo docker.io/cloudamqp/amqpcat amqpcat" |
| 53 | +``` |
| 54 | +::: |
| 55 | +:::{tab-item} Windows PowerShell |
| 56 | +To make the settings persistent, add them to your PowerShell profile (`$PROFILE`). |
| 57 | +```powershell |
| 58 | +function crash { docker run --rm -it --network=cratedb-demo ghcr.io/crate/cratedb-toolkit crash @args } |
| 59 | +function lorry { docker run --rm -i --network=cratedb-demo ghcr.io/daq-tools/lorrystream lorry @args } |
| 60 | +function amqpcat { docker run --rm -i --network=cratedb-demo docker.io/cloudamqp/amqpcat amqpcat @args } |
| 61 | +``` |
| 62 | +::: |
| 63 | +:::{tab-item} Windows Command |
| 64 | +```shell |
| 65 | +doskey crash=docker run --rm -it --network=cratedb-demo ghcr.io/crate/cratedb-toolkit crash $* |
| 66 | +doskey lorry=docker run --rm -i --network=cratedb-demo ghcr.io/daq-tools/lorrystream lorry $* |
| 67 | +doskey amqpcat=docker run --rm -i --network=cratedb-demo docker.io/cloudamqp/amqpcat amqpcat $* |
| 68 | +``` |
| 69 | +::: |
| 70 | + |
| 71 | +:::: |
| 72 | + |
| 73 | +## Usage |
| 74 | + |
| 75 | +Invoke the data transfer pipeline. |
| 76 | +```shell |
| 77 | +lorry relay \ |
| 78 | + "amqp://guest:guest@rabbitmq:5672/%2F?exchange=default&queue=default&routing-key=testdrive&setup=exchange,queue,bind&content-type=json" \ |
| 79 | + "crate://cratedb/?table=testdrive" |
| 80 | +``` |
| 81 | + |
| 82 | +Publish a JSON message to AMQP. |
| 83 | +```shell |
| 84 | +echo '{"temperature": 42.84, "humidity": 83.1}' | \ |
| 85 | + amqpcat --producer --uri='amqp://guest:guest@rabbitmq:5672/%2F' \ |
| 86 | + --exchange=default --queue=default --routing-key=testdrive |
| 87 | +``` |
| 88 | + |
| 89 | +Inspect data stored in CrateDB. |
| 90 | +```shell |
| 91 | +crash --hosts cratedb -c "SELECT * FROM testdrive" |
| 92 | +``` |
| 93 | + |
| 94 | + |
| 95 | +[LorryStream AMQP source]: https://lorrystream.readthedocs.io/source/amqp.html |
| 96 | +[RabbitMQ]: https://www.rabbitmq.com/ |
0 commit comments