What does the repartition() function do in Apache Spark?
The repartition() function reshuffles the data across the cluster to create the specified number of partitions. It can increase or decrease partitions and involves a full shuffle, which can be expensive.
Click to reveal answer
beginner
How does coalesce() differ from repartition()?
coalesce() reduces the number of partitions without a full shuffle by merging existing partitions. It is more efficient than repartition when decreasing partitions but cannot increase partitions unless shuffle is set to true.
Click to reveal answer
intermediate
When should you use repartition() instead of coalesce()?
Use repartition() when you want to increase the number of partitions or when you need a full shuffle to evenly distribute data for better parallelism.
Click to reveal answer
intermediate
What is a shuffle in Apache Spark and why is it important in repartitioning?
A shuffle is the process of redistributing data across partitions and nodes. It is important in repartitioning because it moves data to balance partitions but can be costly in time and resources.
Click to reveal answer
intermediate
Explain the performance impact of using coalesce() versus repartition().
coalesce() is faster and uses less resources because it avoids a full shuffle by merging partitions. repartition() is slower due to the shuffle but provides better data distribution when increasing partitions.
Click to reveal answer
Which function should you use to increase the number of partitions in Spark?
Acoalesce()
Bfilter()
Cmap()
Drepartition()
✗ Incorrect
repartition() can increase or decrease partitions by shuffling data, while coalesce() only reduces partitions unless shuffle is true.
What is the main cost associated with using repartition()?
AFull shuffle of data
BNo cost, it is free
COnly merges partitions
DDeletes data
✗ Incorrect
Repartition causes a full shuffle, which moves data across the cluster and can be expensive.
Which method is more efficient when reducing partitions without needing a shuffle?
AgroupBy()
Brepartition()
Ccoalesce()
Djoin()
✗ Incorrect
coalesce() reduces partitions efficiently by merging without a shuffle.
Can coalesce() increase the number of partitions?
ANo
BYes
COnly if shuffle is true
DOnly with repartition()
✗ Incorrect
coalesce() cannot increase partitions; it only reduces them unless shuffle is set to true, which is not common.
Why might repartition() improve parallelism?
ABecause it merges partitions
BBecause it evenly redistributes data across partitions
CBecause it deletes partitions
DBecause it caches data
✗ Incorrect
Repartition evenly redistributes data, which can improve parallel processing.
Describe the differences between repartition() and coalesce() in Apache Spark and when to use each.
Think about shuffle and partition count changes.
You got /4 concepts.
Explain what a shuffle is in Spark and why it matters for partition tuning.
Consider data movement and performance.
You got /4 concepts.
Practice
(1/5)
1. Which statement best describes the difference between repartition and coalesce in Apache Spark?
easy
A. coalesce only increases partitions with shuffle; repartition only decreases partitions without shuffle.
B. coalesce can increase or decrease partitions with shuffle; repartition only decreases partitions without shuffle.
C. repartition only increases partitions without shuffle; coalesce only decreases partitions with shuffle.
D. repartition can increase or decrease partitions with shuffle; coalesce only decreases partitions without shuffle.
Solution
Step 1: Understand repartition behavior
repartition reshuffles data and can increase or decrease the number of partitions, causing a full shuffle.
Step 2: Understand coalesce behavior
coalesce reduces the number of partitions without a full shuffle, making it faster but it cannot increase partitions.
Final Answer:
repartition can increase or decrease partitions with shuffle; coalesce only decreases partitions without shuffle. -> Option D
Hint: Repartition reshuffles; coalesce only reduces partitions [OK]
Common Mistakes:
Thinking coalesce can increase partitions
Assuming repartition does not shuffle
Confusing which method is faster
2. Which of the following is the correct syntax to reduce the number of partitions of a DataFrame df to 5 using coalesce?
easy
A. df.repartition(5)
B. df.coalesce(5)
C. df.coalesce().numPartitions(5)
D. df.reducePartitions(5)
Solution
Step 1: Recall coalesce syntax
The correct method to reduce partitions is df.coalesce(numPartitions).
Step 2: Identify correct usage
df.coalesce(5) uses df.coalesce(5), which correctly reduces partitions to 5.
Final Answer:
df.coalesce(5) -> Option B
Quick Check:
coalesce syntax = df.coalesce(number) [OK]
Hint: Use df.coalesce(number) to reduce partitions [OK]
Common Mistakes:
Using repartition instead of coalesce to reduce partitions
Calling non-existent methods like reducePartitions
Incorrect method chaining syntax
3. Given the following Spark code, what will be the number of partitions after execution?
val df = spark.range(1000).repartition(10)
val df2 = df.coalesce(4)
medium
A. 4 partitions
B. 10 partitions
C. 14 partitions
D. 1000 partitions
Solution
Step 1: Analyze initial repartition
df is repartitioned to 10 partitions explicitly.
Step 2: Apply coalesce
df2 uses coalesce(4) which reduces partitions from 10 to 4 without shuffle.
Final Answer:
4 partitions -> Option A
Quick Check:
repartition to 10, then coalesce to 4 = 4 partitions [OK]
Hint: Coalesce reduces partitions to given number [OK]
Common Mistakes:
Assuming partitions add up (10 + 4 = 14)
Thinking coalesce increases partitions
Confusing initial partition count with final
4. You want to increase the number of partitions of a DataFrame df from 4 to 8. You try df.coalesce(8) but it does not increase partitions. What is the issue?
medium
A. You need to call df.coalesce(8).shuffle() to increase partitions.
B. coalesce requires shuffle to increase partitions.
C. coalesce cannot increase partitions; use repartition instead.
D. The DataFrame must be cached before increasing partitions.
Solution
Step 1: Understand coalesce limitations
coalesce only reduces partitions and does not increase them.
Step 2: Choose correct method to increase partitions
To increase partitions, repartition must be used as it performs a shuffle.
Final Answer:
coalesce cannot increase partitions; use repartition instead. -> Option C
Quick Check:
coalesce only reduces, repartition reshuffles [OK]
Hint: Use repartition to increase partitions, not coalesce [OK]
Common Mistakes:
Trying to increase partitions with coalesce
Assuming coalesce triggers shuffle
Believing caching affects partition count
5. You have a large DataFrame with 100 partitions. You want to reduce partitions to 10 for faster processing but want to avoid a full shuffle to save time. Which method should you use and why?
hard
A. Use df.coalesce(10) because it reduces partitions without a full shuffle.
B. Use df.repartition(10) because it reduces partitions without shuffle.
C. Use df.repartition(10) because it avoids shuffle and reduces partitions.
D. Use df.coalesce(10) because it increases partitions with shuffle.
Solution
Step 1: Identify partition reduction without shuffle
coalesce reduces partitions without a full shuffle, making it faster.
Step 2: Compare with repartition
repartition always causes a full shuffle, which is slower but can increase or decrease partitions.
Final Answer:
Use df.coalesce(10) because it reduces partitions without a full shuffle. -> Option A
Quick Check:
Reduce partitions fast = coalesce without shuffle [OK]
Hint: Coalesce reduces partitions fast without shuffle [OK]
Common Mistakes:
Using repartition and expecting no shuffle
Thinking coalesce increases partitions
Confusing shuffle behavior of methods
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.