How to use Spark Structured Streaming with Kafka Direct Stream?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Spark Structured Streaming is an advanced and scalable stream processing engine built on the Spark SQL engine. It offers high-level abstractions for stream processing, as well as Spark’s own advantages in fast computational capabilities and easy integration with complex data ecosystems. One common use case is integrating Spark Structured Streaming with Apache Kafka, a distributed streaming platform capable of handling trillions of events a day.
Understanding Spark Structured Streaming and Kafka Integration
Apache Kafka serves as a robust queue capable of handling high-throughput data streams. It integrates well with Apache Spark for real-time analytics and event processing. The integration allows Spark to read data directly from Kafka and apply complex transformations and analytical algorithms to the streaming data.
Spark Structured Streaming treats stream processing as an incremental batch processing task, abstracting most of the complex details, allowing developers to focus on business logic. The model simplifies stream processing by treating it like a table which gets appended data continuously.
Setting Up the Environment
To use Spark Structured Streaming with Kafka, you need:
- Apache Spark (preferably 2.x or later)
- Apache Kafka (0.10.x or later)
- A compatible build tool (like SBT for Scala or Maven for Java)
Assuming Kafka and Spark are installed and configured correctly, the next step is setting up your Spark application to connect with Kafka.
Integrating Spark Structured Streaming with Kafka
1. Create a Kafka Source for Streaming Queries
You can define a Kafka source by specifying the details about the Kafka broker and the topics to subscribe to. Below is an example in Scala that creates a DataFrame representing the Kafka stream:
2. Data Processing
Once you have the data as a DataFrame, you can use the powerful Spark SQL engine to manipulate the streaming data similar to a static DataFrame:
3. Writing the Result to a Sink
After processing, you can write the results back to Kafka, a file system, or any database. Below, the processed data is written to the console for demonstration:
Key Configuration Parameters and Recommendations
| Parameter | Purpose | Recommended Setting |
| kafka.bootstrap.servers | Kafka cluster address | "localhost:9092" or actual server IPs |
| subscribe | Kafka topics to subscribe | "topic1,topic2" |
| startingOffsets | Point to start reading data | "latest" or "earliest" |
| failOnDataLoss | Whether to fail query when data is potentially lost | "true" |
Additional Tips and Best Practices
- Event Time Handling: Leverage event time processing capabilities in Spark to handle out-of-order data or late arriving data effectively.
- Watermarking: Use watermarking to specify how long to wait for late events.
- Scaling: Ensure that your Kafka and Spark clusters are configured to scale up according to the workload needs.
- Monitoring: Always monitor both Kafka and Spark to ensure that the streaming jobs are processing as expected without unexpected delays.
Conclusion
Using Spark Structured Streaming with Kafka Direct Stream allows developers to process large streams of data in real-time, leveraging Spark's analytical power and Kafka's scalability. By abstracting most of the lower level API complexities related to fault tolerance and distributed processing, Spark Structured Streaming enables developers and data scientists to focus more on business logic and insights, making it a powerful tool in the arsenal for any data-driven organization.
Related reading
- How to use the Kafka Connect JDBC to source PostgreSQL with multiple schemas that contain tables with the same name?
- How to use the rabbitmq docker compose yml file to build docker image?
- How to use two Kerberos keytabs (for Kafka and Hadoop HDFS) from a Flink job on a Flink standalone cluster?
- How to view and set offsets.retention.minutes using kafka-configs
- How to write Kafka Producer in Scala
- How to write spark streaming DF to Kafka topic
- how to view kafka headers
- How to write a Dataset to Kafka topic?

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.