Kafka
Structured Streaming
JSON
Data Processing
Programming

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:

bash
bin/kafka-server-start.sh config/server.properties

2. Create a Kafka Topic

Create a topic (e.g., json_topic) where messages will be published:

bash
bin/kafka-topics.sh --create --topic json_topic --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1

3. Initialize Spark Session

Set up your Spark session to handle the processing:

scala
1import org.apache.spark.sql.SparkSession
2
3val spark = SparkSession.builder()
4  .appName("Kafka JSON Reader")
5  .master("local")
6  .getOrCreate()

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):

scala
1val df = spark
2  .readStream
3  .format("kafka")
4  .option("kafka.bootstrap.servers", "localhost:9092")
5  .option("subscribe", "json_topic")
6  .load()

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:

scala
1import org.apache.spark.sql.functions._
2
3val jsonDf = df.selectExpr("CAST(value AS STRING) as json_string")
4  .select(from_json(col("json_string"), schema).as("data"))
5  .select("data.*")

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:

scala
1jsonDf.writeStream
2  .outputMode("append")
3  .format("console")
4  .start()
5  .awaitTermination()

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 ComponentDescriptionConsiderations
Kafka Bootstrap ServerServer for connecting to KafkaSpecified in the readStream options
Topic SubscriptionDefines Kafka topics to subscribe toCan include multiple topics
DataFrame ParsingExtracts and transforms JSON from Kafka messagesDefine schema for effective parsing
Output ModeControls output to external systems or consolesAppend, complete, update options
Schema ProvisioningDefines the structure of JSON dataCrucial 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.


Course illustration
Course illustration

All Rights Reserved.