Apache Spark
Structured Streaming
Kafka Topics
Data Streaming
App Development

Spark structured streaming app reading from multiple Kafka topics

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

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:

  1. List of Topics: Simply provide a list of topics to subscribe to.
  2. 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:

scala
1import org.apache.spark.sql.SparkSession
2import org.apache.spark.sql.types.StructType
3
4val spark = SparkSession.builder()
5  .appName("KafkaMultiTopicRead")
6  .getOrCreate()
7
8val kafkaSourceDF = spark
9  .readStream
10  .format("kafka")
11  .option("kafka.bootstrap.servers", "host1:port1,host2:port2")
12  .option("subscribe", "topic1,topic2,topic3")
13  .load()

This example creates a DataFrame from a Kafka source that subscribes to topic1, topic2, and topic3.

Using Regex Pattern to Subscribe:

scala
1val kafkaSourceDF = spark
2  .readStream
3  .format("kafka")
4  .option("kafka.bootstrap.servers", "host1:port1,host2:port2")
5  .option("subscribePattern", "topic.*")
6  .load()

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.

scala
1import org.apache.spark.sql.functions._
2
3val transformedDF = kafkaSourceDF
4  .selectExpr("CAST(key AS STRING)", "CAST(value AS STRING)")
5  .groupBy("key")
6  .count()
7
8val query = transformedDF
9  .writeStream
10  .outputMode("complete")
11  .format("console")
12  .start()
13
14query.awaitTermination()

This example decodes the key and value from Kafka's byte array format and then performs a simple count per key.

Key Considerations

AspectConsideration
Fault toleranceStructured Streaming provides fault tolerance through checkpointing and write-ahead logs.
Event orderingMaintains read order per partition but across partitions order is not guaranteed.
ScalabilityBoth Kafka and Spark Streaming scale well horizontally.
Data serializationCommon 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.


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.