Skip to main content
The join() command in Spark is used to combine two DataFrames based on a common column or key. It is similar to SQL joins and allows you to perform various types of joins, such as inner, outer, left, right, and cross joins. This is particularly useful when you need to combine datasets for analysis or processing.

1. Syntax

PySpark:
Spark SQL:

2. Parameters

  • df2: The DataFrame to join with.
  • join_condition: A condition specifying how the DataFrames should be joined (e.g., df1.col_name == df2.col_name).
  • join_type (optional): The type of join to perform. Default is inner. Options include:
    • inner: Returns rows with matching keys in both DataFrames.
    • outer/full: Returns all rows from both DataFrames, with null where there is no match.
    • left/left_outer: Returns all rows from the left DataFrame and matching rows from the right DataFrame.
    • right/right_outer: Returns all rows from the right DataFrame and matching rows from the left DataFrame.
    • cross: Returns the Cartesian product of both DataFrames.

3. Return Type

  • Returns a new DataFrame containing the combined data based on the join condition and type.

4. Examples

Example 1: Inner Join

PySpark:
Spark SQL:
Output:

Example 2: Left Join

PySpark:
Spark SQL:
Output:

Example 3: Right Join

PySpark:
Spark SQL:
Output:

Example 4: Full Outer Join

PySpark:
Spark SQL:
Output:

Example 5: Cross Join

PySpark:
Spark SQL:
Output:

Example 6: Joining on Multiple Columns

PySpark:
Spark SQL:
Output:

Example 7: Using Broadcast Join for Small DataFrames

PySpark:
Spark SQL:
Output:

5. Common Use Cases

  • Combining transactional data with master data (e.g., sales data with product data).
  • Enriching datasets by merging related information (e.g., customer data with order data).
  • Preparing data for machine learning by combining features from multiple tables.

6. Performance Considerations

  • Use join() judiciously on large datasets, as it involves shuffling and sorting, which can be expensive.
  • Consider using broadcast() for small DataFrames to optimize performance.
  • Use proper partitioning and indexing to improve join performance.

7. Key Takeaways

  1. The join() command is used to combine two DataFrames based on a common column or key.
  2. It supports various types of joins, including inner, outer, left, right, and cross joins.
  3. Joins can be resource-intensive for large datasets, as they involve shuffling and sorting.
  4. In Spark SQL, similar functionality can be achieved using JOIN clauses.
  5. Works efficiently on large datasets when combined with proper partitioning and caching.