Apache Hudi: A Deep Dive with Python Code Examples

Apache Hudi: A Deep Dive with Python Code Examples

In today's data-driven world, real-time data processing and analytics have become crucial for businesses to stay competitive. Apache Hudi (Hadoop Upserts and Incremental) is an open-source data management framework that provides efficient data ingestion and real-time analytics on large-scale datasets stored in data lakes. In this blog, we'll explore Apache Hudi with a technical deep dive and Python code examples, using a business example for better clarity.

1. Introduction to Apache Hudi

Apache Hudi is designed to address the challenges associated with managing large-scale data lakes, such as data ingestion, updating, and querying. Hudi enables efficient data ingestion and provides support for both batch and real-time data processing.

Key Features of Apache Hudi

  1. Upserts (Insert/Update): Efficiently handle data updates and inserts with minimal overhead. Traditional data lakes struggle with updates, but Hudi's upsert capability ensures that the latest data is always available without requiring full rewrites of entire datasets.

  2. Incremental Pulls: Retrieve only the changed data since the last pull, which significantly optimizes data processing pipelines by reducing the amount of data that needs to be processed.

  3. Data Versioning: Manage different versions of data, allowing for easy rollback and temporal queries. This versioning is critical for ensuring data consistency and supporting use cases such as time travel queries.

  4. ACID Transactions: Ensure data consistency and reliability by providing atomic, consistent, isolated, and durable transactions on data lakes. This makes Hudi a robust choice for enterprise-grade applications.

  5. Compaction: Hudi offers a compaction mechanism that optimizes storage and query performance. This process merges smaller data files into larger ones, reducing the overhead associated with managing numerous small files.

  6. Schema Evolution: Handle changes in the data schema gracefully without disrupting the existing pipelines. This feature is particularly useful in dynamic environments where data models evolve over time.

  7. Integration with Big Data Ecosystem: Hudi integrates seamlessly with Apache Spark, Apache Hive, Apache Flink, and other big data tools, making it a versatile choice for diverse data engineering needs.

2. Business Use Case

Let's consider a business use case of an e-commerce platform that needs to manage and analyze user order data in real-time. The platform receives a high volume of orders every day, and it is essential to keep the data up-to-date and perform real-time analytics to track sales trends, inventory levels, and customer behavior.

3. Setting Up Apache Hudi

Before we dive into the code, let's set up the environment. We'll use PySpark and the Hudi library for this purpose.

# Install necessary libraries
pip install pyspark==3.1.2
pip install hudi-spark-bundle_2.12

4. Ingesting Data with Apache Hudi

Let's start by ingesting some order data into Apache Hudi. We'll create a DataFrame with sample order data and write it to a Hudi table.

from pyspark.sql import SparkSession
from pyspark.sql.functions import col, lit
import datetime

# Initialize Spark session
spark = SparkSession.builder \
    .appName("HudiExample") \
    .config("spark.serializer", "org.apache.spark.serializer.KryoSerializer") \
    .config("spark.sql.hive.convertMetastoreParquet", "false") \

# Sample order data
order_data = [
    (1, "2023-10-01", "user_1", 100.0),
    (2, "2023-10-01", "user_2", 150.0),
    (3, "2023-10-02", "user_1", 200.0)

# Create DataFrame
columns = ["order_id", "order_date", "user_id", "amount"]
df = spark.createDataFrame(order_data, columns)

# Define Hudi options
hudi_options = {
    'hoodie.table.name': 'orders',
    'hoodie.datasource.write.storage.type': 'COPY_ON_WRITE',
    'hoodie.datasource.write.recordkey.field': 'order_id',
    'hoodie.datasource.write.partitionpath.field': 'order_date',
    'hoodie.datasource.write.precombine.field': 'order_date',
    'hoodie.datasource.hive_sync.enable': 'true',
    'hoodie.datasource.hive_sync.database': 'default',
    'hoodie.datasource.hive_sync.table': 'orders',
    'hoodie.datasource.hive_sync.partition_fields': 'order_date'

# Write DataFrame to Hudi table

print("Data ingested successfully.")

5. Querying Data with Apache Hudi

Now that we have ingested the order data, let's query the data to perform some analytics. We'll use the Hudi DataSource API to read the data.

# Read data from Hudi table
orders_df = spark.read.format("hudi").load("/path/to/hudi/orders/*")

# Show the ingested data

# Perform some analytics
# Calculate total sales
total_sales = orders_df.groupBy("order_date").sum("amount").withColumnRenamed("sum(amount)", "total_sales")

# Calculate sales by user
sales_by_user = orders_df.groupBy("user_id").sum("amount").withColumnRenamed("sum(amount)", "total_sales")

6. Security and Other Aspects

When working with large-scale data lakes, security and data governance are paramount. Apache Hudi provides several features to ensure your data is secure and compliant with regulatory requirements.


  1. Data Encryption: Hudi supports data encryption at rest to protect sensitive information from unauthorized access. By leveraging Hadoop's native encryption support, you can ensure that your data is encrypted before it is written to disk.

  2. Access Control: Integrate Hudi with Apache Ranger or Apache Sentry to manage fine-grained access control policies. This ensures that only authorized users and applications can access or modify the data.

  3. Audit Logging: Hudi can be integrated with log aggregation tools like Apache Kafka or Elasticsearch to maintain an audit trail of all data operations. This is crucial for compliance and forensic investigations.

  4. Data Masking: Implement data masking techniques to obfuscate sensitive information in datasets, ensuring that only authorized users can see the actual data.

Performance Optimization

  1. Compaction: As mentioned earlier, Hudi's compaction feature merges smaller data files into larger ones, optimizing storage and query performance. You can schedule compaction jobs based on your workload patterns.

  2. Indexing: Hudi supports various indexing techniques to speed up query performance. Bloom filters and columnar indexing are commonly used to reduce the amount of data scanned during queries.

  3. Caching: Leverage Spark's in-memory caching to speed up repeated queries on Hudi datasets. This can significantly reduce query latency for interactive analytics.

Monitoring and Management

  1. Metrics: Hudi provides a rich set of metrics that can be integrated with monitoring tools like Prometheus or Grafana. These metrics help you monitor the health and performance of your Hudi tables.

  2. Data Quality: Implement data quality checks using Apache Griffin or Deequ to ensure that the ingested data meets your quality standards. This helps in maintaining the reliability of your analytics.

  3. Schema Evolution: Hudi's support for schema evolution allows you to handle changes in the data schema without disrupting existing pipelines. This is particularly useful in dynamic environments where data models evolve over time.

7. Conclusion

In this blog, we have explored Apache Hudi and its capabilities to manage large-scale data lakes efficiently. We set up a Spark environment, ingested sample order data into a Hudi table, and performed some basic analytics. We also discussed the security aspects and performance optimizations that Apache Hudi offers.

Apache Hudi's ability to handle upserts, provide incremental pulls, and ensure data security makes it a powerful tool for real-time data processing and analytics. By leveraging Apache Hudi, businesses can ensure their data lakes are up-to-date, secure, and ready for real-time analytics, enabling them to make data-driven decisions quickly and effectively.

Feel free to dive deeper into Apache Hudi's documentation and explore more advanced features to further enhance your data engineering workflows.

If you have any questions or need further clarification, please let me know in the comments below!

Did you find this article valuable?

Support Harsh Daiya by becoming a sponsor. Any amount is appreciated!