Fetching data from RabbitMQ to Yandex Managed Service for ClickHouse®
- Getting started
- Set up integration with RabbitMQ for the Managed Service for ClickHouse® cluster
- In the Managed Service for ClickHouse® cluster, create a RabbitMQ table
- Send the test data to the RabbitMQ queue
- Check that the test data is present in the Managed Service for ClickHouse® cluster table
- Delete the resources you created
You can supply data from RabbitMQ to a Managed Service for ClickHouse® cluster in real time. Managed Service for ClickHouse® will automatically insert the data routed into particular exchange points of the specified RabbitMQ queues into a RabbitMQ
To set up data delivery from RabbitMQ to Managed Service for ClickHouse®:
- Set up integration with RabbitMQ for the Managed Service for ClickHouse® cluster.
- In the Managed Service for ClickHouse® cluster, create a RabbitMQ table.
- Send the test data to the RabbitMQ queue.
- Check that the test data is present in the Managed Service for ClickHouse® cluster table.
If you no longer need the resources you created, delete them.
Getting started
Prepare the infrastructure
-
Create a Managed Service for ClickHouse® cluster in any suitable configuration with the
db1
database. To connect to the cluster from the user's local machine instead of the Yandex Cloud cloud network, enable public access to the cluster hosts when creating it.Note
You can set up RabbitMQ integration when creating a cluster. In this article, integration will be configured later.
-
Create a virtual machineforRabbitMQ. To connect to the cluster from the user's local machine rather than doing so from the Yandex Cloud network, enable public access when creating it.
-
If you do not have Terraform yet, install it.
-
Get the authentication credentials. You can add them to environment variables or specify them later in the provider configuration file.
-
Configure and initialize a provider. There is no need to create a provider configuration file manually, you can download it
. -
Place the configuration file in a separate working directory and specify the parameter values. If you did not add the authentication credentials to environment variables, specify them in the configuration file.
-
Download the clickhouse-cluster-and-vm-for-rabbitmq.tf
configuration file to the same working directory.This file describes:
- Network.
- Subnet.
- Default security group and rules required to connect to the cluster and VM from the internet.
- Managed Service for ClickHouse® cluster.
- Virtual machine.
-
Specify the following in the
clickhouse-cluster-and-vm-for-rabbitmq.tf
file:- Username and password that will be used to access the Managed Service for ClickHouse® cluster.
- ID of the public Ubuntu image without GPU for the VM.
- Username and path to the public key file for accessing the virtual machine. By default, the specified username is ignored in the image used. A user with the
ubuntu
username is created instead. Use it to connect to the instance.
-
Check that the Terraform configuration files are correct using this command:
terraform validate
If there are any errors in the configuration files, Terraform will point them out.
-
Create the required infrastructure:
-
Run the command to view planned changes:
terraform plan
If the resource configuration descriptions are correct, the terminal will display a list of the resources to modify and their parameters. This is a test step. No resources are updated.
-
If you are happy with the planned changes, apply them:
-
Run the command:
terraform apply
-
Confirm the update of resources.
-
Wait for the operation to complete.
-
All the required resources will be created in the specified folder. You can check resource availability and their settings in the management console
. -
Configure additional settings
-
Connect to a virtual machine over SSH.
-
Install RabbitMQ:
sudo apt update && sudo apt install rabbitmq-server --yes
-
Create a user for RabbitMQ:
sudo rabbitmqctl add_user <username> <password>
-
Grant this user permissions to connect to the server:
sudo rabbitmqctl set_permissions -p / <username> ".*" ".*" ".*" && \ sudo rabbitmqctl set_topic_permissions -p / <username> amq.topic "cars" "cars"
-
-
Install the
amqp-publish
andamqp-declare-queue
utilities to work with RabbitMQ and jq for stream processing JSON files:sudo apt update && sudo apt install amqp-tools --yes && sudo apt-get install jq --yes
-
Check if you can create a queue named
cars
in RabbitMQ usingamqp-declare-queue
:amqp-declare-queue \ --url=amqp://<username>:<password>@<IP_address_or_FQDN_of_the_RabbitMQ_server>:5672 \ --queue=cars
-
Install the
clickhouse-client
utility to connect to the database in the Managed Service for ClickHouse® cluster.-
Connect the DEB repository
ClickHouse®:sudo apt update && sudo apt install --yes apt-transport-https ca-certificates dirmngr && \ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv E0C56BD4 && \ echo "deb https://packages.clickhouse.com/deb stable main" | sudo tee \ /etc/apt/sources.list.d/clickhouse.list
-
Install the dependencies:
sudo apt update && sudo apt install clickhouse-client --yes
-
Download the configuration file for
clickhouse-client
:mkdir -p ~/.clickhouse-client && \ wget "https://storage.yandexcloud.net/doc-files/clickhouse-client.conf.example" \ --output-document ~/.clickhouse-client/config.xml
Check that you can use
clickhouse-client
to connect to the Managed Service for ClickHouse® cluster over SSL. -
Set up integration with RabbitMQ for the Managed Service for ClickHouse® cluster
In the Managed Service for ClickHouse® cluster settings, specify the username and password for RabbitMQ authentication in DBMS settings → Rabbitmq.
Add the clickhouse.config.rabbitmq
block with the username and password for RabbitMQ authentication to the cluster description:
resource "yandex_mdb_clickhouse_cluster" "clickhouse-cluster" {
...
clickhouse {
...
config {
rabbitmq {
username = "<username>"
password = "<password>"
}
}
...
}
}
In the Managed Service for ClickHouse® cluster, create a RabbitMQ table
Let's assume you put the following JSON car sensor data into the RabbitMQ queue named cars
at the exchange point named exchange
:
-
device_id
: String ID of the device. -
datetime
: Date and time when the data was generated, inYYYY-MM-DD HH:MM:SS
format. -
Car coordinates:
latitude
: Latitude.longitude
: Longitude.altitude
: Altitude above sea level.
-
speed
: Current speed. -
battery_voltage
: Battery voltage (for electric cars; for cars with internal combustion engine, this parameter isnull
). -
cabin_temperature
: Temperature inside the car. -
fuel_level
: Fuel level (for cars with internal combustion engine; for electric cars, this parameter isnull
).
This data will be transmitted as RabbitMQ messages. Each message will contain a JSON object as a string in the following format:
{"device_id":"iv9a94th6rzt********","datetime":"2020-06-05 17:27:00","latitude":"55.70329032","longitude":"37.65472196","altitude":"427.5","speed":"0","battery_voltage":"23.5","cabin_temperature":"17","fuel_level":null}
The Managed Service for ClickHouse® cluster will use the JSONEachRow format
Create a table in the Managed Service for ClickHouse® cluster to accept data from RabbitMQ:
-
Connect to the
db1
database in the Managed Service for ClickHouse® cluster usingclickhouse-client
. -
Run the following query:
CREATE TABLE IF NOT EXISTS db1.cars ( device_id String, datetime DateTime, latitude Float32, longitude Float32, altitude Float32, speed Float32, battery_voltage Nullable(Float32), cabin_temperature Float32, fuel_level Nullable(Float32) ) ENGINE = RabbitMQ SETTINGS rabbitmq_host_port = '<internal_IP_address_of_VM_with_RabbitMQ>:5672', rabbitmq_routing_key_list = 'cars', rabbitmq_exchange_name = 'exchange', rabbitmq_format = 'JSONEachRow';
This table will be automatically filled with messages read from the cars
queue at RabbitMQ's exchange point named exchange
. When reading the data, Managed Service for ClickHouse® uses the authentication credentials provided earlier.
Send the test data to the RabbitMQ queue
-
Create a
sample.json
file with test data:{ "device_id": "iv9a94th6rzt********", "datetime": "2020-06-05 17:27:00", "latitude": 55.70329032, "longitude": 37.65472196, "altitude": 427.5, "speed": 0, "battery_voltage": 23.5, "cabin_temperature": 17, "fuel_level": null } { "device_id": "rhibbh3y08qm********", "datetime": "2020-06-06 09:49:54", "latitude": 55.71294467, "longitude": 37.66542005, "altitude": 429.13, "speed": 55.5, "battery_voltage": null, "cabin_temperature": 18, "fuel_level": 32 } { "device_id": "iv9a94th6rzt********", "datetime": "2020-06-07 15:00:10", "latitude": 55.70985913, "longitude": 37.62141918, "altitude": 417.0, "speed": 15.7, "battery_voltage": 10.3, "cabin_temperature": 17, "fuel_level": null }
-
Send the data from the
sample.json
file to the previously createdcars
queue of the exchange point namedexchange
usingjq
andamqp-publish
.jq \ --raw-output \ --compact-output . ./sample.json |\ amqp-publish \ --url=amqp://<RabbitMQ_username>:<password>@<IP_address_or_FQDN_of_the_RabbitMQ_server>:5672 \ --routing-key=cars \ --exchange=exchange
Check that the test data is present in the Managed Service for ClickHouse® cluster table
To access the data, use a materialized view (MATERIALIZED VIEW
). When a materialized view is added to a RabbitMQ
table, it starts collecting data in the background. This allows you to continuously receive messages from RabbitMQ and convert them to the required format using SELECT
.
Note
Although you can read data directly from the table, we recommend using a materialized view, because every message from the queue can be read by ClickHouse® only once.
To create a materialized view for the db1.cars
table:
-
Connect to the
db1
database in the Managed Service for ClickHouse® cluster usingclickhouse-client
. -
Run the following queries:
CREATE TABLE IF NOT EXISTS db1.cars_data_source ( device_id String, datetime DateTime, latitude Float32, longitude Float32, altitude Float32, speed Float32, battery_voltage Nullable(Float32), cabin_temperature Float32, fuel_level Nullable(Float32) ) ENGINE MergeTree() ORDER BY device_id; CREATE MATERIALIZED VIEW db1.cars_view TO db1.cars_data_source AS SELECT * FROM db1.cars;
To get all the data from the db1.cars_view
materialized view:
-
Connect to the
db1
database in the Managed Service for ClickHouse® cluster usingclickhouse-client
. -
Run the following query:
SELECT * FROM db1.cars_view;
The query will return a table with data sent to RabbitMQ.
Delete the resources you created
Delete the resources you no longer need to avoid paying for them:
- Delete the Yandex Managed Service for ClickHouse® cluster.
- Delete the virtual machine.
- If you reserved public static IP addresses, release and delete them.
-
In the terminal window, go to the directory containing the infrastructure plan.
Warning
Make sure the directory has no Terraform manifests with the resources you want to keep. Terraform deletes all resources that were created using the manifests in the current directory.
-
Delete resources:
-
Run this command:
terraform destroy
-
Confirm deleting the resources and wait for the operation to complete.
All the resources described in the Terraform manifests will be deleted.
-
ClickHouse® is a registered trademark of ClickHouse, Inc