Skip to main content
PYSPARK โ€ข LESSON 265

Solving the Small File Problem via Compaction

How can we read thousands of small streaming output files and compact them into a clean set of optimized 128MB files?

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

How can we read thousands of small streaming output files and compact them into a clean set of optimized 128MB files?

๐Ÿ’ก WHAT IS IT?

Reading a directory of micro-files and writing back with coalesce() or repartition() compacts file fragmentation.

๐ŸŽฏ WHAT IS IT USED FOR?

Scheduled lakehouse maintenance jobs eliminating metastore overhead and slow S3 directory listing latencies.

๐Ÿ’ป EXAMPLE
df_fragmented = spark.read.parquet("s3://lakehouse/streaming_raw/")
df_compacted = df_fragmented.coalesce(10)
df_compacted.write.mode("overwrite").parquet("s3://lakehouse/compacted_raw/")

๐ŸŽฏ Mission Objectives

Practice typing production-grade PySpark code for Solving the Small File Problem via Compaction.

  • โœ“ Small file compaction
  • โœ“ coalesce() file consolidation
  • โœ“ Storage maintenance jobs