Kafka
JSON
Data Processing
Kafka Topics
Kafka Streaming

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.

Practice system design

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:

java
1Properties props = new Properties();
2props.setProperty("bootstrap.servers", "localhost:9092");
3props.setProperty("group.id", "test-group");
4props.setProperty("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
5props.setProperty("value.deserializer", "org.apache.kafka.common.serialization.JsonDeserializer");
6
7KafkaConsumer<String, JsonNode> consumer = new KafkaConsumer<>(props);
8consumer.subscribe(Arrays.asList("json_input_topic"));

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:

java
1StreamsBuilder builder = new StreamsBuilder();
2KStream<String, JsonNode> sourceStream = builder.stream("json_input_topic");
3
4KStream<String, JsonNode> processedStream = sourceStream.mapValues(value -> {
5    ObjectNode modifiedValue = (ObjectNode) value;
6    modifiedValue.put("importantField", value.get("importantField").asText().toUpperCase());
7    return modifiedValue;
8});
9
10processedStream.to("json_output_topic");
11KafkaStreams streams = new KafkaStreams(builder.build(), props);
12streams.start();

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:

java
1Properties props = new Properties();
2props.put("bootstrap.servers", "localhost:9092");
3props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
4props.put("value.serializer", "org.apache.kafka.common.serialization.JsonSerializer");
5
6KafkaProducer<String, JsonNode> producer = new KafkaProducer<>(props);
7JsonNode jsonNode = createSomeJsonNode();  // Assume a method that creates a JsonNode
8producer.send(new ProducerRecord<>("json_output_topic", jsonNode));
9producer.close();

Summary

Here’s a simple table summarizing the steps and components involved in each stage of the process:

StageComponentDescription
Read JSON from KafkaKafka ConsumerSubscribe and deserialize JSON from the topic.
Process JSONKafka StreamsPerform necessary transformations or processing.
Write JSON to KafkaKafka ProducerSerialize 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
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.