Skip to main content
PYSPARK • LESSON 262

Multi-Level Hive-Style Partitioning

How can we partition a dataset by year, month, and region to create a standardized directory layout on object storage?

Expert3 Minutes810 XP
🤔 THE QUESTION

How can we partition a dataset by year, month, and region to create a standardized directory layout on object storage?

💡 WHAT IS IT?

partitionBy('year', 'month', 'region') writes files into nested subdirectories enabling fine-grained partition pruning during reads.

🎯 WHAT IS IT USED FOR?

Structuring global event logs and sales transactions for rapid regional and temporal slice queries.

💻 EXAMPLE
df_partitioned = df.withColumn("year", year("event_date")).withColumn("month", month("event_date"))
df_partitioned.write.partitionBy("year", "month", "region").mode("append").parquet("s3://lakehouse/silver/events/")

🎯 Mission Objectives

Practice typing production-grade PySpark code for Multi-Level Hive-Style Partitioning.

  • Multi-level partitionBy()
  • Hive-style directory hierarchy
  • Query partition pruning