Skip to main content
PYSPARK โ€ข LESSON 245

Zero-Shuffle Partition Reduction with coalesce()

How can we reduce the number of output files before saving to Cloud Storage without causing an expensive network shuffle?

Expertโฑ 3 Minutesโญ 840 XP
๐Ÿค” THE QUESTION

How can we reduce the number of output files before saving to Cloud Storage without causing an expensive network shuffle?

๐Ÿ’ก WHAT IS IT?

coalesce(n) merges adjacent partitions locally on executors, reducing file counts with zero network shuffle overhead.

๐ŸŽฏ WHAT IS IT USED FOR?

Preventing the small file problem when saving filtered datasets to Amazon S3 or Google Cloud Storage.

๐Ÿ’ป EXAMPLE
df_output = df_filtered.coalesce(10)
df_output.write.mode("overwrite").parquet("s3://bucket/output/")

๐ŸŽฏ Mission Objectives

Practice typing production-grade PySpark code for Zero-Shuffle Partition Reduction with coalesce().

  • โœ“ coalesce(n) zero-shuffle
  • โœ“ Adjacent partition merging
  • โœ“ Small file problem prevention