Skip to main content
The broadcast(df) function in Spark is used to explicitly broadcast a DataFrame or Dataset to all nodes in the cluster. Broadcasting is a technique used to optimize join operations by sending a small DataFrame to all worker nodes, reducing the amount of data shuffled across the network. This is particularly useful when joining a large DataFrame with a small DataFrame.

1. Syntax

PySpark:
Spark SQL:
  • There is no direct equivalent in Spark SQL, but you can use BROADCAST hint in SQL queries.

2. Key Features

  • Optimization: Reduces data shuffling by sending a small DataFrame to all worker nodes.
  • Efficiency: Improves the performance of join operations when one DataFrame is small.
  • Automatic Broadcasting: Spark automatically broadcasts small DataFrames (based on spark.sql.autoBroadcastJoinThreshold), but you can use broadcast() to explicitly control broadcasting.

3. Parameters

  • df: The DataFrame or Dataset to broadcast.

4. Examples

Example 1: Broadcasting a Small DataFrame for a Join

PySpark:
Spark SQL:
Output:

Example 2: Broadcasting a DataFrame with Aggregations

PySpark:
Output:

Example 3: Broadcasting a DataFrame with Filters

PySpark:
Output:

Example 4: Broadcasting a DataFrame with Complex Data

PySpark:
Output:

Example 5: Broadcasting a DataFrame with Multiple Joins

PySpark:
Output:

Example 6: Broadcasting a DataFrame with Custom Configuration

PySpark:
Output:

5. Common Use Cases

  • Joining a large DataFrame with a small DataFrame.
  • Optimizing performance by reducing network overhead.
  • Explicitly controlling broadcasting for better performance.

6. Performance Considerations

  • Memory Usage: Broadcasting a large DataFrame can lead to out-of-memory errors. Use it only for small DataFrames.
  • Automatic Broadcasting: Spark automatically broadcasts small DataFrames (based on spark.sql.autoBroadcastJoinThreshold), but you can use broadcast() to explicitly control broadcasting.

7. Key Takeaways

  1. The broadcast(df) function is used to explicitly broadcast a DataFrame or Dataset to all nodes in the cluster.
  2. Reduces data shuffling and improves the performance of join operations.
  3. Broadcasting is efficient for small DataFrames but should be avoided for large DataFrames due to memory constraints.