QuestionQ53

VerifiedTroubleshooting and Tuning Apache Spark DataFrame API Applications.

An engineer has two DataFrames, df1 and df2, where df1 is small and df2 is large. To optimize the join, the engineer uses a broadcast join:

from pyspark.sql.functions import broadcast  
  
result = df2.join(broadcast(df1), on='id', how='inner')  

What is the purpose of using broadcast() in this scenario?

Explanation

Broadcasting replicates the smaller dataframe to all executor nodes, so partitions of the large dataframe can join with it locally. This avoids shuffling the large dataframe and reduces shuffle operations.

Community Discussion

No comments yet. Be the first to start the discussion!