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?

Expertโฑ 3 Minutesโญ 810 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