Data delivery in ksqlDB
- Getting started
- Set up Apache Kafka® integration for the ksqlDB database
- Review the format of the data coming from Managed Service for Apache Kafka®
- Create a table in ksqlDB to capture the data stream from the Apache Kafka® topic
- Get test data from the Managed Service for Apache Kafka® cluster
- Write the test data to ksqlDB
- Check for records in Apache Kafka® topics
- Delete the resources you created
ksqlDB is a database designed for stream processing messages from Apache Kafka® topics. Working with message streams in ksqlDB is similar to working with tables in a regular database. The ksqlDB table is automatically updated with data from a topic, and the data that you add to the ksqlDB table is sent to the Apache Kafka® topic. You can learn more in the ksqlDB documentation
To set up data delivery from Managed Service for Apache Kafka® to ksqlDB:
- Set up Apache Kafka® integration for the ksqlDB database.
- Review the format of the data coming from Managed Service for Apache Kafka®.
- Create a table in ksqlDB to capture the data stream from the Apache Kafka® topic.
- Get test data from the Managed Service for Apache Kafka® cluster
- Write the test data to ksqlDB.
- Check that the test data is present in the Apache Kafka® topic.
If you no longer need the resources you created, delete them.
Getting started
-
Create a Managed Service for Apache Kafka® cluster in any suitable configuration.
-
If the ksqlDB server is hosted on the internet, create a Managed Service for Apache Kafka® cluster with public access.
-
If the ksqlDB server is hosted in Yandex Cloud, create a Managed Service for Apache Kafka® cluster on the same cloud network as ksqlDB.
-
-
Create topics in a Managed Service for Apache Kafka® cluster:
- Service topic named
_confluent-ksql-default__command_topic
with the following settings:- Replication factor:
1
- Number of partitions:
1
- Log cleanup policy:
Delete
- Log retention, ms:
-1
- Minimum number of in-sync replicas:
1
- Replication factor:
- Service topic named
default_ksql_processing_log
for writing ksqlDB logs. The topic may have any settings. - Data storage topic named
locations
. The topic may have any settings.
- Service topic named
-
Create a user named
ksql
and assign it the ACCESS_ROLE_PRODUCER andACCESS_ROLE_CONSUMER
roles for all topics. -
Check that you can connect to the ksqlDB server.
-
Install the
kafkacat
utility on the ksqlDB server and check that you can use it to connect to the Managed Service for Apache Kafka® cluster over SSL. -
Install the jq
utility for stream processing JSON files on the ksqlDB server.
Set up Apache Kafka® integration for the ksqlDB database
-
Connect to the ksqlDB server.
-
Add the server's SSL certificate to the Java trusted certificate store (Java Key Store) so that ksqlDB can use this certificate for secure connections to the cluster hosts. Make sure to set the password using the
-storepass
parameter for additional storage protection:cd /etc/ksqldb && \ sudo keytool -importcert -alias YandexCA -file /usr/local/share/ca-certificates/Yandex/YandexInternalRootCA.crt \ -keystore ssl -storepass <certificate_store_password> \ --noprompt
-
In the ksqlDB
/etc/ksqldb/ksql-server.properties
configuration file, specify the credentials to authenticate in the Managed Service for Apache Kafka® cluster:bootstrap.servers=<FQDN_of_broker_1>:9091,...,<FQDN_of_broker_N>:9091 sasl.mechanism=SCRAM-SHA-512 security.protocol=SASL_SSL ssl.truststore.location=/etc/ksqldb/ssl ssl.truststore.password=<certificate_store_password> sasl.jaas.config=org.apache.kafka.common.security.scram.ScramLoginModule required username="ksql" password="<ksql_user_password>";
For info on how to get a broker host's FQDN, see this guide.
You can request the cluster name with a list of clusters in the folder.
-
In the ksqlDB logging configuration file
/etc/ksqldb/ksql-server.properties
, configure logging to a Managed Service for Apache Kafka® cluster topic:log4j.appender.kafka_appender=org.apache.kafka.log4jappender.KafkaLog4jAppender log4j.appender.kafka_appender.layout=io.confluent.common.logging.log4j.StructuredJsonLayout log4j.appender.kafka_appender.BrokerList=<FQDN_of_broker_1>:9091,...,<FQDN_of_broker_N>:9091 log4j.appender.kafka_appender.Topic=default_ksql_processing_log log4j.logger.io.confluent.ksql=INFO,kafka_appender log4j.appender.kafka_appender.clientJaasConf=org.apache.kafka.common.security.scram.ScramLoginModule required username="ksql" password="<ksql_user_password>"; log4j.appender.kafka_appender.SecurityProtocol=SASL_SSL log4j.appender.kafka_appender.SaslMechanism=SCRAM-SHA-512 log4j.appender.kafka_appender.SslTruststoreLocation=/etc/ksqldb/ssl log4j.appender.kafka_appender.SslTruststorePassword=<certificate_store_password>
-
Restart the ksqlDB service with the command below:
sudo systemctl restart confluent-ksqldb.service
Review the format of the data coming from Managed Service for Apache Kafka®
Processing data streams from Managed Service for Apache Kafka® depends on the data representation format in Apache Kafka® messages.
In the example, geodata is written to the Apache Kafka® locations
topic in JSON format:
profileId
: Identifier.latitude
: Latitude.longitude
: Longitude.
This data will be transmitted as Apache Kafka® messages. Each message will contain a JSON object as a string in the following format:
{"profileId": "c2309eec", "latitude": 37.7877, "longitude": -122.4205}
ksqlDB stores the values of the corresponding parameters from Apache Kafka® messages in a three-column table.
Next, we are going to configure the fields of a ksqlDB data stream table.
Create a table in ksqlDB to capture the data stream from the Apache Kafka® topic
Create a table in ksqlDB for writing data from the Apache Kafka® topic. The table structure matches the format of the data from Managed Service for Apache Kafka®:
-
Connect to the ksqlDB server.
-
Run the
ksql
client with the command:ksql http://0.0.0.0:8088
-
Run the following query:
CREATE STREAM riderLocations ( profileId VARCHAR, latitude DOUBLE, longitude DOUBLE ) WITH ( kafka_topic='locations', value_format='json', partitions=<number_of_locations_topic_partitions> );
This data stream table will be automatically populated with messages from the Managed Service for Apache Kafka® cluster
locations
topic. KsqlDB uses theksql
user settings to read messages.For more information about creating a data stream table in the ksqlDB engine, see the ksqlDB documentation
. -
Run the following query:
SELECT * FROM riderLocations WHERE GEO_DISTANCE(latitude, longitude, 37.4133, -122.1162) <= 5 EMIT CHANGES;
The query waits for real-time data to appear in the table.
Get test data from the Managed Service for Apache Kafka® cluster
-
Connect to the ksqlDB server.
-
Create a
sample.json
file with the following test data:{ "profileId": "c2309eec", "latitude": 37.7877, "longitude": -122.4205 } { "profileId": "4ab5cbad", "latitude": 37.3952, "longitude": -122.0813 } { "profileId": "4a7c7b41", "latitude": 37.4049, "longitude": -122.0822 }
-
Send the
sample.json
file to thelocations
topic of the Managed Service for Apache Kafka® cluster usingjq
andkafkacat
:jq -rc . sample.json | kafkacat -P \ -b <FQDN_of_broker_1>:9091,...,<FQDN_of_broker_N>:9091> \ -t locations \ -X security.protocol=SASL_SSL \ -X sasl.mechanisms=SCRAM-SHA-512 \ -X sasl.username=ksql \ -X sasl.password="<ksql_user_password>" \ -X ssl.ca.location=/usr/local/share/ca-certificates/Yandex/YandexInternalRootCA.crt -Z
Information is sent using the
ksql
user. To learn more about setting up an SSL certificate and working withkafkacat
, see Connecting to an Apache Kafka® cluster from applications. -
Make sure that the session displays the data sent to the topic:
+--------------------------+--------------------------+------------------------+ |PROFILEID |LATITUDE |LONGITUDE | +--------------------------+--------------------------+------------------------+ |4ab5cbad |37.3952 |-122.0813 | |4a7c7b41 |37.4049 |-122.0822 |
Data is read using the ksql
user.
Write the test data to ksqlDB
-
Connect to the ksqlDB server.
-
Run the
ksql
client with the command:ksql http://0.0.0.0:8088
-
Insert the test data into the
riderLocations
table:INSERT INTO riderLocations (profileId, latitude, longitude) VALUES ('18f4ea86', 37.3903, -122.0643); INSERT INTO riderLocations (profileId, latitude, longitude) VALUES ('8b6eae59', 37.3944, -122.0813); INSERT INTO riderLocations (profileId, latitude, longitude) VALUES ('4ddad000', 37.7857, -122.4011);
This data is sent synchronously to the Apache Kafka®
locations
topic using theksql
user.
Check for records in Apache Kafka® topics
-
Check the messages in the
locations
topic of the Managed Service for Apache Kafka® cluster usingkafkacat
and theksql
user:kafkacat -C \ -b <FQDN_of_broker_1>:9091,...,<FQDN_of_broker_N>:9091 \ -t locations \ -X security.protocol=SASL_SSL \ -X sasl.mechanisms=SCRAM-SHA-512 \ -X sasl.username=ksql \ -X sasl.password="<ksql_user_password>" \ -X ssl.ca.location=/usr/local/share/ca-certificates/Yandex/YandexInternalRootCA.crt -Z -K:
-
Make sure that the console displays the messages you inserted into the table.
-
Check the messages in the
default_ksql_processing_log
topic of the Managed Service for Apache Kafka® cluster usingkafkacat
and theksql
user:kafkacat -C \ -b <FQDN_of_broker_1>:9091,...,<FQDN_of_broker_N>:9091 \ -t default_ksql_processing_log \ -X security.protocol=SASL_SSL \ -X sasl.mechanisms=SCRAM-SHA-512 \ -X sasl.username=ksql \ -X sasl.password="<ksql_user_password>" \ -X ssl.ca.location=/usr/local/share/ca-certificates/Yandex/YandexInternalRootCA.crt -Z -K:
-
Make sure the console displays ksqlDB log records.
Delete the resources you created
Some resources are not free of charge. To avoid paying for them, delete the resources you no longer need:
- Delete the virtual machine.
- If you reserved a public static IP for your virtual machine, delete it.
- Delete the Managed Service for Apache Kafka® cluster.