Apache Kafka
HDFS
Parquet
Data Processing
Big Data Analytics

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.

Practice system design

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:

  1. Consume data from Kafka: Data is read from a Kafka topic.
  2. Process/Transform data (optional): Depending on requirements, the data might be cleaned, filtered, or transformed.
  3. 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:

python
1from pyspark.sql import SparkSession
2
3spark = SparkSession.builder \
4    .appName("KafkaToHDFS") \
5    .getOrCreate()
6
7df = spark \
8  .readStream \
9  .format("kafka") \
10  .option("kafka.bootstrap.servers", "host1:port1,host2:port2") \
11  .option("subscribe", "topic_name") \
12  .load()

Step 2: Data Processing

This step is application-specific and may involve cleaning, aggregating, or transforming the data as per requirements:

python
transformed_df = df.selectExpr("CAST(key AS STRING)", "CAST(value AS STRING)")

Step 3: Writing to HDFS in Parquet Format

Now, configure the data to be written to the HDFS in Parquet format:

python
1query = transformed_df \
2    .writeStream \
3    .outputMode("append") \
4    .format("parquet") \
5    .option("path", "hdfs://path/to/output/dir") \
6    .option("checkpointLocation", "hdfs://path/to/checkpoint/dir") \
7    .start()
8
9query.awaitTermination()

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

FeatureBenefit
Columnar StorageEfficient data compression and encoding. Quicker query performance as only relevant data is read.
Schema EvolutionSupports changes in the data schema over time without rewriting old data.
IntegrationWidely 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
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.