Read Kafka topic in a Spark batch job
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Kafka is a widely used distributed streaming platform that specializes in providing a high-throughput, fault-tolerant method for managing data feeds. Apache Spark, on the other hand, is an open-source unified analytics engine for large-scale data processing. Integrating Kafka with Spark enables the processing of real-time data streams in addition to batch data processing. This article focuses on how to read data from a Kafka topic in a Spark batch job, an essential capability for businesses that need to process historical data stored in Kafka.
Understanding Kafka and Spark Integration
Spark provides a Kafka integration module, spark-sql-kafka-0-10, which supports reading from and writing to Kafka. The integration allows Spark to process data directly from Kafka topics both in streaming and batch modes. The primary class responsible for this integration is DataFrameReader for reading operations.
Setting Up Spark with Kafka
To begin with, ensure that your Spark cluster has the necessary Kafka dependencies. The Kafka integration package can be included in your Spark application using the --packages option during the submission of your Spark job:
Reading from a Kafka Topic in a Batch Job
Reading from a Kafka topic in a batch job involves setting up a DataFrameReader to load data from Kafka as a DataFrame. Here is an example code snippet using PySpark, the Python API for Spark:
In the above example:
kafka.bootstrap.serversspecifies the Kafka server's address.subscribesets the Kafka topic from which to read.startingOffsetsandendingOffsetsdefine the range of data to read. In this case, it's from the very beginning to the most recent data available, making it suitable for batch processing.
Key Configuration Options
- startingOffsets: Can be either
earliest,latest(not useful in batch scenarios), or a JSON string specifying offsets. - endingOffsets: Can be
latest, or a JSON string specifying the last offset to fetch.
Processing Data
Once the data is read into a DataFrame, it can be processed using Spark’s DataFrame API. For instance, you may want to decode the binary Kafka records into string format if they are simple text messages:
Summary Table
| Feature | Description |
| Data format | Kafka's data is read into Spark as binary; requires decoding. |
| Integration library | spark-sql-kafka-0-10 |
| Configurations | Options for server details, topic subscription, offset specs. |
| Output | Returns a DataFrame that can be further processed. |
Conclusion
This method enables batch processing of historical Kafka topic data, which is beneficial for analytical and reporting purposes. The seamless integration between Kafka and Spark allows businesses to leverage both streaming and batch data processing capabilities to make more data-driven decisions.
Related reading
- Read keys only from Kafka
- readinessProbe (k8s) for kafka statefulset causes bad deployment
- Reading a topic of kafka with react
- Reading Avro messages from Kafka with Spark 2.0.2 (structured streaming)
- Read sharded output from Hadoop job from DistributedCache
- Read timed out Httpfs HDFS
- Reading data from _transaction_state topic in Kafka 0.11.0.1
- Reading from multiple queues, RabbitMQ

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.