Skip to main content
The foreach() function in Spark is used to apply a function to each row of a DataFrame or Dataset. It is an action that triggers the execution of the function on each element of the distributed dataset. Unlike transformations (e.g., map(), filter()), foreach() does not return a new DataFrame or Dataset but is used for side effects, such as writing data to an external system or printing rows.

1. Syntax

PySpark:
Spark SQL:
  • There is no direct equivalent in Spark SQL, but you can use foreach() in DataFrame/Dataset APIs.

2. Parameters

  • func: A function that takes a row (or element) as input and performs an operation on it. The function can be a lambda or a user-defined function.

3. Key Features

  • Action: foreach() is an action, meaning it triggers the execution of the Spark job.
  • Side Effects: It is typically used for side effects, such as writing data to an external system or printing rows.
  • Distributed Execution: The function is applied to each row in a distributed manner across the cluster.

4. Examples

Example 1: Printing Each Row

PySpark:
Output:

Example 2: Writing Rows to an External System

PySpark:
Output:

Example 3: Using foreach() with a User-Defined Function

PySpark:
Output:

Example 4: Writing Rows to a File

PySpark:
Output:
  • Each row is appended to output.txt.

Example 5: Sending Rows to a Message Queue

PySpark:
Output:

Example 6: Using foreach() with Accumulators

PySpark:
Output:

5. Common Use Cases

  • Writing data to external systems (e.g., databases, message queues).
  • Logging or printing rows for debugging purposes.
  • Performing custom operations on each row (e.g., sending notifications).

6. Performance Considerations

  • Execution Overhead: foreach() triggers the execution of the entire DataFrame lineage, so use it carefully for large datasets.
  • Distributed Execution: The function is applied to each row in a distributed manner, so ensure the function is efficient and thread-safe.
  • Side Effects: Since foreach() is used for side effects, ensure the function does not introduce unintended behavior (e.g., modifying shared state).

7. Key Takeaways

  1. The foreach() function is used to apply a function to each row of a DataFrame or Dataset for side effects.
  2. It is an action that triggers the execution of the Spark job.
  3. Since foreach() is an action, it triggers the execution of the entire DataFrame lineage. Use it judiciously for large datasets.
  4. In Spark SQL, similar functionality can be achieved using foreach() in DataFrame/Dataset APIs.