How to read records in JSON format from Kafka using Structured Streaming?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Apache Kafka, an open-source stream-processing software platform, is widely used for building real-time data pipelines and streaming applications. On the other hand, Apache Spark’s Structured Streaming is an efficient way to handle real-time data analytics. In this guide, we'll explore how to read records in JSON format from Kafka using Spark’s Structured Streaming.
Understanding Kafka and Structured Streaming Integration
Kafka acts as a broker between data producers and consumers, offering high throughput, built-in partitioning, replication, and fault tolerance. Structured Streaming, an extension to the Spark SQL engine, allows for scalable and fault-tolerant stream processing of live data streams.
Setting up the Environment
Before proceeding, ensure you have the following installed:
- Java 8 or higher
- Apache Spark (version 2.4.0 or later as it has better support for Kafka)
- Apache Kafka (matching the compatibility with Spark)
Steps to Read JSON from Kafka using Structured Streaming
1. Start the Kafka Server
First, ensure your Kafka server is running. If not, start Kafka by executing the relevant server-start command, typically something like:
2. Create a Kafka Topic
Create a topic (e.g., json_topic) where messages will be published:
3. Initialize Spark Session
Set up your Spark session to handle the processing:
4. Read Data from Kafka
Create a DataFrame to read the stream from Kafka. You specify the topic and the Kafka server's details (bootstrap servers):
This DataFrame will have columns like key, value (holding the JSON payload), topic, partition, and offset.
5. Parse JSON Data
Assuming the Kafka messages are in JSON format, you can use Spark's dynamic schema inference to parse the JSON from the value column:
In this snippet, schema should be defined according to the JSON structure you expect.
6. Processing and Querying
Post parsing, the jsonDf DataFrame can be queried or processed like any other DataFrame. For instance, to simply display the output, you can write:
Best Practices & Performance Considerations
- Schema Provisioning: Manually provide a schema for JSON parsing for better performance instead of relying on schema inference.
- Kafka Partitioning: Effective partitioning of Kafka topics aligns with the level of parallelism desired in your Spark application and can enormously impact performance.
- Fault-tolerance: Make sure to use checkpointing and write-ahead logs to handle failures gracefully.
Summary Table
| Key Component | Description | Considerations |
| Kafka Bootstrap Server | Server for connecting to Kafka | Specified in the readStream options |
| Topic Subscription | Defines Kafka topics to subscribe to | Can include multiple topics |
| DataFrame Parsing | Extracts and transforms JSON from Kafka messages | Define schema for effective parsing |
| Output Mode | Controls output to external systems or consoles | Append, complete, update options |
| Schema Provisioning | Defines the structure of JSON data | Crucial for parsing and performance |
With these simple steps, you would be well-equipped to handle streaming JSON data from Kafka using Apache Spark’s Structured Streaming, providing a robust setup for real-time data processing applications.

