Spark structured streaming app reading from multiple Kafka topics
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Apache Spark Structured Streaming is an efficient and scalable streaming platform that handles real-time data processing with ease. One of its powerful features includes integration with Apache Kafka, a popular distributed streaming platform. In this article, we delve into how a Spark Structured Streaming application can read data from multiple Kafka topics. This capability is crucial for scenarios where data is segmented into various topics based on their categorization or source but needs to be processed in a unified manner.
Understanding Spark Structured Streaming
Structured Streaming is a scalable and fault-tolerant stream processing engine built on the Spark SQL engine. It enables high-throughput, fault-tolerant processing of streaming data and interacts smoothly with complex data formats and storage systems. Structured Streaming provides a DataFrame API to define streaming computations with the same ease and expressiveness as batch processing.
Working with Kafka in Spark
Apache Kafka is a distributed streaming platform capable of handling trillions of events a day. Integrating Spark with Kafka allows Spark to read data directly from Kafka. Kafka data is typically categorized into multiple topics where each topic might correspond to a specific type of event or data source.
Reading from Multiple Kafka Topics
When using Spark Structured Streaming to consume data from Kafka, you can subscribe to multiple topics. There are two common approaches:
- List of Topics: Simply provide a list of topics to subscribe to.
- Pattern Matching: Use a pattern to subscribe to a set of topics that match the pattern.
Let’s explore these approaches with code examples.
Examples
Subscribing to Multiple Topics by Listing Them:
This example creates a DataFrame from a Kafka source that subscribes to topic1, topic2, and topic3.
Using Regex Pattern to Subscribe:
This approach is useful when you want to subscribe to topics dynamically, especially when you have a naming convention for topics where names have incremental or coded patterns.
Processing the Data
After reading the data, it can be processed using the standard DataFrame operations.
This example decodes the key and value from Kafka's byte array format and then performs a simple count per key.
Key Considerations
| Aspect | Consideration |
| Fault tolerance | Structured Streaming provides fault tolerance through checkpointing and write-ahead logs. |
| Event ordering | Maintains read order per partition but across partitions order is not guaranteed. |
| Scalability | Both Kafka and Spark Streaming scale well horizontally. |
| Data serialization | Common formats are byte arrays or Avro; ensure the consumer can deserialize appropriately. |
Conclusion
Integrating Spark Structured Streaming with Kafka, especially for reading from multiple topics, provides a powerful toolset for real-time analytics and event-driven architectures. Whether using explicit topic subscriptions or pattern matching, Spark makes it efficient to process large volumes of data across different topic streams.
This setup suits various use cases, from real-time monitoring systems, log aggregation solutions, to complex event processing in financial or IoT domains. As always, testing and tuning configurations specific to your use case will be critical to success.

