Skip to main content
PYSPARK โ€ข LESSON 268

Bucketing Data with bucketBy() and sortBy()

How can we pre-bucket and pre-sort a table on storage so future joins on that key require zero cluster shuffling?

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

How can we pre-bucket and pre-sort a table on storage so future joins on that key require zero cluster shuffling?

๐Ÿ’ก WHAT IS IT?

bucketBy(numBuckets, col).sortBy(col) pre-shuffles data during write into fixed hash buckets for instant SortMergeJoins.

๐ŸŽฏ WHAT IS IT USED FOR?

Optimizing massive enterprise tables joined daily on customer_id or account_id in recurrent ETL pipelines.

๐Ÿ’ป EXAMPLE
df.write.bucketBy(16, "customer_id").sortBy("order_date").mode("overwrite").saveAsTable("default.bucketed_orders")

๐ŸŽฏ Mission Objectives

Practice typing production-grade PySpark code for Bucketing Data with bucketBy() and sortBy().

  • โœ“ bucketBy() and sortBy()
  • โœ“ Pre-bucketed storage layout
  • โœ“ Zero-shuffle SortMergeJoin