Read json from Kafka and write json to other Kafka topic
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
In many modern data architectures, Apache Kafka is a central component, facilitating robust messaging and streaming capabilities. Kafka is an open-source stream-processing software platform developed by the Apache Software Foundation, written in Scala and Java. One common use case in Kafka is the need to read JSON messages from one Kafka topic, process them, and then write the processed JSON messages to another Kafka topic.
This article will explore how to read JSON data from a Kafka topic, perform some processing, and then write the JSON output to another Kafka topic using Apache Kafka and its ecosystem. We'll look at using Kafka Consumers and Producers with Kafka Streams or Kafka Connect.
Reading JSON from Kafka
To read JSON messages from a Kafka topic, you will first need a Kafka Consumer configured to deserialize JSON data that Kafka stores in a binary format. The deserialization process converts these binary messages back into JSON or another human-readable format. Apache Kafka uses the concept of Deserializer to convert the binary data back to the original data format. Here's an example of setting up a Kafka Consumer in Java that reads JSON data:
In this setup, the consumer subscribes to the json_input_topic and uses JsonDeserializer to convert the binary JSON data into JsonNode objects, which can then be easily manipulated in Java.
Processing JSON Data
Once you have the JSON data read into your application, you might need to perform processing or transformations. This could involve changing the data structure, filtering out records, or merging it with other data.
You can process this JSON data using Kafka Streams, a client library for building applications and microservices where the input and output data are stored in Kafka clusters. Kafka Streams simplifies the process by providing methods to directly handle the data transformations.
Here's a basic example where Kafka Streams is used to uppercase the values of certain fields in each JSON message:
Writing JSON to Kafka
After processing, the final step is to write the JSON data back to a different Kafka topic. This is done using a Kafka Producer. Here’s how you might set up a Kafka Producer in Java to serialize and send JSON data:
Summary
Here’s a simple table summarizing the steps and components involved in each stage of the process:
| Stage | Component | Description |
| Read JSON from Kafka | Kafka Consumer | Subscribe and deserialize JSON from the topic. |
| Process JSON | Kafka Streams | Perform necessary transformations or processing. |
| Write JSON to Kafka | Kafka Producer | Serialize and publish the processed JSON to a topic. |
Enhancements and Considerations
- Error Handling and Monitoring: Implement error handling to manage deserialization failures or processing errors. Additionally, monitoring Kafka consumer lag and performance metrics can help identify issues early.
- Scalability and Performance: Kafka's distributed nature allows it to scale out. Partitioning of topics, adequate consumer configurations, and proper handling of offsets can dramatically impact the performance and scalability of your Kafka applications.
- Security: Ensure that access to Kafka topics is secured using ACLs, and data in transit could be encrypted using SSL/TLS.
Conclusion
Reading and writing JSON from and to Kafka topics are common operations in event-driven architectures. By using Kafka's robust ecosystem, developers can efficiently process streaming data. Whether it's through simple consumers and producers, or more complex stream processing applications, Kafka offers the tools necessary to handle real-time data processing at scale.
Related reading
- read kafka message starting from a specific offset using high level API
- Read Kafka topic in a Spark batch job
- Read keys only from Kafka
- readinessProbe (k8s) for kafka statefulset causes bad deployment
- Reading a topic of kafka with react
- Reading Avro messages from Kafka with Spark 2.0.2 (structured streaming)
- Reading data from _transaction_state topic in Kafka 0.11.0.1
- Reading from multiple queues, RabbitMQ

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.