Apache Spark Big Data (50 Qs)•Part 2: Core Architecture & Implementation (Mid-Level)
What is the difference between `repartition()` and `coalesce()`?
senior LevelCode ExampleSystem Design
In Simple Words:
repartition(N): Reshuffles data evenly across N partitions using full network shuffle. Can increase or decrease partition count. High overhead, but balances skewed partitions.coalesce(N): Decreases the number of partitions without a full shuffle by merging adjacent partitions on the same worker node. Much faster, but can only decrease partition count.
python
# Best practice when writing results to disk:
# Avoid creating 200 tiny 50KB files:
df.coalesce(4).write.parquet("s3://bucket/output/")