A repository containing everything I used for my Kafka Streams presentation at Cora
The following instructions are a guide of how to setup the kafkastreams application present in this very repository, in order to run some tests for yourself and see how a Kafka broker behave by manually running command line scripts to feed data to it or read data from it.
At the end, there's a section for ksqlDB that uses the same broker as above, which can be used to test the basic concepts of ksqlDB and understand where and why it can be useful for your real world application.
Mind you, this is a very basic introduction of Kafka Streams and ksqlDB. For a deeper understaing, check the references section at the bottom of the page.
In order to set up the environment and run tests, just follow the steps below:
In order to start it up, simply run docker on the repository's root directory.
docker-compose up
There are two main scripts that will be used to prepare the whole environment, one for the external machine and one for the docker broker instance. Follow the instructions below to make it work:
- From the repository's root directory, navigate to the
brokerfolder, located at./streams/broker; - Run the
prepare.shscript; - Run the
_prepare.shscript that's inside thebrokerinstance;
cd streams/broker
./prepare.sh
docker-compose exec broker /bin/sh -c ". /home/appuser/_prepare.sh"
Navigate to the application directory located at ./streams/app/kafkastreams and run it with gradle via CLI, or import the project to your favorite IDE and run it via the KafkastreamsApplication.kt file.
cd streams/app/kafkastreams
./gradlew bootRun
After doing that, you are good to play around with the application endpoints and open an interactive shell inside the broker instance and use the helper scripts.
The main objective of ksqlDB is to untie the Kafka Streams operations from a specific programming language. It creates an abstraction layer between the broker and the application by providing endpoints for the stream operations.
The following scripts are located under the ./ksqldb/rest directory.
In order to run tests on the ksqlDB you can use the following scripts:
./ksql.sh QUERY: runs the given query on the local/ksqlendpoint (withauto-offset-resetset toearliest);./query.sh QUERY: runs the given query on the local/queryendpoint.
For more information about the endpoints, refer to: https://docs.ksqldb.io/en/latest/developer-guide/api/
The app.sh script is an example of how to execute multiple statements that depends on the previous statement's result. It does that by using the commandSequenceNumber value, returned by the statement executed via ksqlDB rest API.
Those are the operations that it will execute:
- Create a stream called
lorem_stream_scriptfrom thelorem-ipsumtopic, whose content of each row is a lorem ipsum sentence; - Create a stream called
lorem_cnt_stream, with the sentence length and the sentence itself, pulled from thelorem_stream_scriptstream; - Lastly, it will run a SELECT statement on the
lorem_cnt_streamstream and show its results.
./app.sh
For testing purposes, there's an easier way to run the SQL statements, which is by running an interactive shell.
docker exec -it ksqldb-cli ksql http://ksqldb-server:8088
- GET
/kafka-streams/filter: runKafkaStreamsfilter operations; - GET
/kafka-streams/aggregate: runsKafkaStreamsaggregation operations; - GET
/kafka-streams/table: runsKTableoperations; - GET
/kafka-streams/join: runsKStreamsjoin operations.
Those are meant to be run inside the broker instance.
- Create a topic:
./create TOPIC_NAME; - Delete a topic:
./delete TOPIC_NAME; - List created topic:
./list-topics.sh; - Produce in a topic with a null key:
./produce.sh TOPIC_NAME; - Consume a topic with a null key:
./consume.sh TOPIC_NAME [OPTIONS]; - Produce in a topic with a non-null (formatted:
key:value):./keyed-produce.sh TOPIC_NAME; - Consume a topic with a non-null key:
./keyed-consume.sh TOPIC_NAME [OPTIONS].
- Apache Kafka
- Kafka Brokers | Learn Apache Kafka with Conduktor
- Kafka in a Nutshell | Kevin Sookocheff
- How persistence works in an Apache Kafka deployment - IBM Developer
- Apache Kafka®: Basic Concepts, Architecture, and Examples