Skip to main content
PYSPARK โ€ข LESSON 198

Optimized Partitioning, Coalesce & Parquet Export

How do we derive partition columns, coalesce partitions to prevent small files, and export to cloud storage with Snappy?

Productionโฑ 3 Minutesโญ 1850 XP
๐Ÿค” THE QUESTION

How do we derive partition columns, coalesce partitions to prevent small files, and export to cloud storage with Snappy?

๐Ÿ’ก WHAT IS IT?

Preparing gold-layer datasets for cloud export with date partitioning, file count optimization, and Snappy compression.

๐ŸŽฏ WHAT IS IT USED FOR?

Exporting analytical data marts to AWS S3, Google Cloud Storage, and Snowflake external stages.

๐Ÿ’ป EXAMPLE
from pyspark.sql.functions import col, month, year

mart_df = customer_360 \
    .withColumn("order_year", year(col("order_date"))) \
    .withColumn("order_month", month(col("order_date"))) \
    .coalesce(16)

mart_df.write \
    .mode("overwrite") \
    .partitionBy("order_year", "order_month") \
    .option("compression", "snappy") \
    .parquet("s3://warehouse/gold/customer_360")

๐ŸŽฏ Mission Objectives

Practice typing production-grade PySpark code for Optimized Partitioning, Coalesce & Parquet Export.

  • โœ“ Derive year and month partition keys
  • โœ“ Coalesce partition count to 16 files
  • โœ“ Export partitioned Snappy Parquet to cloud storage