Kafka Streams
KSQL
Message Splitting
Topic Publishing
Data Streaming

Kafka streams - KSQL - Split messages and publish to another topic

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

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

bash
1# Create an input topic
2kafka-topics --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic original_messages
3
4# Create output topics
5kafka-topics --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic split_messages

3. Sample Input Data

Assume the input data in original_messages topic looks like this:

 
John,Doe,30
Jane,Doe,25

4. Writing a KSQL Query to Split the Messages

Launch the KSQL CLI and write a KSQL query to split the incoming messages:

sql
1CREATE STREAM original_stream (fullRecord STRING) WITH (KAFKA_TOPIC='original_messages', VALUE_FORMAT='DELIMITED');
2
3CREATE STREAM split_stream AS SELECT \
4  SPLIT(fullRecord, ',')[1] AS firstName, \
5  SPLIT(fullRecord, ',')[2] AS lastName, \
6  CAST(SPLIT(fullRecord, ',')[3] AS INTEGER) AS age \
7FROM original_stream;
8
9CREATE STREAM filtered_stream AS SELECT * FROM split_stream WHERE age > 18;
10
11CREATE STREAM output_stream WITH (KAFKA_TOPIC='split_messages', VALUE_FORMAT='JSON') AS SELECT * FROM filtered_stream;

Explanation of the KSQL Query

  • Step 1: A stream original_stream is created directly from the Kafka topic original_messages.
  • Step 2: Another stream split_stream is created by splitting the incoming messages based on the comma delimiter.
  • Step 3: filtered_stream selects records where age is greater than 18.
  • Step 4: output_stream re-streams filtered data to a new topic split_messages in 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

ComponentResponsibility
original_messagesKafka topic to receive raw, comma-delimited messages.
original_streamStream to intake raw data from original_messages.
split_streamStream to split comma-delimited data fields.
filtered_streamStream to filter data (e.g., age greater than 18).
split_messagesOutput 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.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.