Read from Kafka and write to hdfs in parquet
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 scalable, fault-tolerant, and distributed event streaming platform, which is widely used for handling real-time data feeds. Hadoop Distributed File System (HDFS) is a scalable and durable file system used by Hadoop ecosystem components. Writing data from Kafka to HDFS in Parquet format is a common pattern used in data engineering and analytics to ensure data is stored efficiently for batch processing.
Understanding Kafka and HDFS
Apache Kafka is designed to handle real-time data streams. It organizes data into topics, which are further split into partitions. Each partition is an ordered, immutable sequence of records that is continually appended.
HDFS, on the other hand, is designed for storing very large files running on a cluster of commodity hardware. It achieves reliability by replicating the data across multiple nodes.
Why Parquet?
Parquet is a columnar storage file format available to any project in the Hadoop ecosystem. Parquet is optimized for use with complex data in bulk and delivers both high performance and efficient space utilization, with excellent compression and encoding schemes.
Workflow for Reading from Kafka and Writing to HDFS
The typical workflow involves the following steps:
- Consume data from Kafka: Data is read from a Kafka topic.
- Process/Transform data (optional): Depending on requirements, the data might be cleaned, filtered, or transformed.
- Write data to HDFS in Parquet format: Finally, the data is stored in HDFS as a Parquet file.
Technologies Used
- Apache Kafka for data ingestion.
- Apache Spark or Flink: These can be used for processing data streams.
- Hadoop HDFS: For storage.
- Apache Parquet: For the file format.
Detailed Walkthrough
Step 1: Setting Up Kafka Consumer
To read data from a Kafka topic, set up a Kafka consumer. In Spark, you can do this using Spark Structured Streaming as follows:
Step 2: Data Processing
This step is application-specific and may involve cleaning, aggregating, or transforming the data as per requirements:
Step 3: Writing to HDFS in Parquet Format
Now, configure the data to be written to the HDFS in Parquet format:
Best Practices
- Data Partitioning: It’s crucial to partition the data logically (e.g., by date or region) when writing to HDFS to enable efficient data queries and maintainability.
- Monitoring and Alerts: Integrate monitoring to track the health and performance of the streaming pipeline.
- Scalability Considerations: Ensure the Kafka consumer configurations and Spark cluster are scaled according to the load and throughput requirements.
Benefits of Using Parquet in HDFS
| Feature | Benefit |
| Columnar Storage | Efficient data compression and encoding. Quicker query performance as only relevant data is read. |
| Schema Evolution | Supports changes in the data schema over time without rewriting old data. |
| Integration | Widely supported in Apache Hadoop ecosystem tools and libraries. |
This approach enhances the system by streamlining data management, reducing storage cost, and supporting complex data analytical requirements effectively.
Related reading
- Read input stream twice
- Read json from Kafka and write json to other Kafka topic
- read kafka message starting from a specific offset using high level API
- Read Kafka topic in a Spark batch job
- Read sharded output from Hadoop job from DistributedCache
- Read timed out Httpfs HDFS
- Read keys only from Kafka
- readinessProbe (k8s) for kafka statefulset causes bad deployment

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.