Skip to main content
PYSPARK โ€ข LESSON 172

Reshuffling Partitions with repartition()

How do we rebalance a dataset into 200 uniform partitions hashed by customer_id to eliminate data skew?

Expertโฑ 2 Minutesโญ 950 XP
๐Ÿค” THE QUESTION

How do we rebalance a dataset into 200 uniform partitions hashed by customer_id to eliminate data skew?

๐Ÿ’ก WHAT IS IT?

repartition(numPartitions, *cols) performs a full cluster shuffle to evenly redistribute records.

๐ŸŽฏ WHAT IS IT USED FOR?

Eliminating data skew, increasing parallelism before heavy joins, and balancing executor workloads.

๐Ÿ’ป EXAMPLE
df_repartitioned = df.repartition(200, "customer_id")

๐ŸŽฏ Mission Objectives

Practice typing production-grade PySpark code for Reshuffling Partitions with repartition().

  • โœ“ Execute full shuffle repartitioning
  • โœ“ Hash partition by customer_id
  • โœ“ Eliminate data skew across executors