Kafka streams - KSQL - Split messages and publish to another topic
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Apache Kafka is a popular distributed streaming platform that provides high-throughput, fault-tolerance, and low-latency capabilities for processing real-time data. Within the Kafka ecosystem, Kafka Streams and KSQL are powerful tools designed for transforming and processing data directly within Kafka clusters. In this article, we explore how to split messages using KSQL, a stream processing language for Kafka, and then publish the results to another Kafka topic.
Overview of Kafka Streams and KSQL
Kafka Streams is a client library for building applications and microservices where the input and output data are stored in Kafka topics. It offers functional simplicity and operational scalability for processing and analyzing data in real time.
KSQL (Kafka SQL), on the other hand, is a stream processing framework that enables users to write SQL-like queries to interact directly with Kafka. It simplifies the process of reading, writing, and processing real-time data streams in Kafka.
Use Case: Splitting Messages and Publishing to Another Topic
Consider a scenario where incoming messages contain multiple fields in a single string, separated by commas, and you need to split these fields to process them separately before sending them to different topics based on specific conditions or transformations.
Step-by-Step Implementation
1. Setting Up Kafka Environment
First, ensure that your Kafka and KSQL servers are up and running. You will need topics to read the input data and store the output.
2. Creating Input and Output Topics
3. Sample Input Data
Assume the input data in original_messages topic looks like this:
4. Writing a KSQL Query to Split the Messages
Launch the KSQL CLI and write a KSQL query to split the incoming messages:
Explanation of the KSQL Query
- Step 1: A stream
original_streamis created directly from the Kafka topicoriginal_messages. - Step 2: Another stream
split_streamis created by splitting the incoming messages based on the comma delimiter. - Step 3:
filtered_streamselects records whereageis greater than 18. - Step 4:
output_streamre-streams filtered data to a new topicsplit_messagesin JSON format.
5. Consuming the Output
After executing the above commands in KSQL, the split_messages topic will have messages in a structured JSON format. These can be used for further processing or directly consumed by other applications.
Abstracting Complexity
By writing SQL-like scripts with KSQL, we abstract the complex programming part involved in Kafka Streams and make the processing pipelines easy to understand and maintain.
Summary Table: Key Components and Responsibilities
| Component | Responsibility |
original_messages | Kafka topic to receive raw, comma-delimited messages. |
original_stream | Stream to intake raw data from original_messages. |
split_stream | Stream to split comma-delimited data fields. |
filtered_stream | Stream to filter data (e.g., age greater than 18). |
split_messages | Output Kafka topic receiving processed JSON data. |
Conclusion
Using KSQL for message processing in Kafka allows for more intuitive, declarative, and SQL-like data handling for streaming data, simplifying the design and operational management of real-time data pipelines. By splitting messages and redirecting them to different topics based on their content, Kafka and KSQL offer a robust solution for modern data-driven applications that require real-time responsiveness.

