Actions vs Transformations

Spark tutorial · PySpark.in

Transformations and Actions in PySpark

PySpark provides a powerful DataFrame API for processing large data sets in a distributed way. Every operation on a DataFrame falls into one of two categories: transformations or actions. Understanding the difference is essential for writing efficient Spark applications and avoiding common performance pitfalls.

What are Transformations?

A transformation produces a new DataFrame from an existing one. Examples include filtering rows, selecting columns, grouping, joining and sorting. Transformations do not execute immediately. Instead of acting on the data right away, Spark records the steps to be performed and builds a logical execution plan. According to Spark documentation, transformations are lazy: they are not executed until an action is triggered, and they build a plan that Spark later optimizes. Lazy evaluation means Spark can group multiple operations together, reduce the number of data scans and rearrange the order of steps for better performance

Lazy Evaluation:

Let's try to undertand this with a simple example:

Examples of Transformations

Transformation

What it does

select()

Select specific columns

filter() / where()

Filter rows based on condition

withColumn()

Create or modify a column

drop()

Remove columns

groupBy()

Group rows for aggregation

join()

Join multiple DataFrames

orderBy()

Sorting rows

dropDuplicates()

Remove duplicate rows

Example of Transformation (No Execution Yet)

PLAINTEXT
1df2 = df.filter(df.salary > 50000)
2print(df2)
▶ Output will appear here.

What are Actions?

Actions are operations that trigger the execution of transformations and return a value to the driver program. They are the operations that actually initiate the computation. Actions are operations that force Spark to execute the logical plan created by transformations and return results to the driver or external storage.

Examples of Actions

Action

What it does

show()

Displays data in tabular format

count()

Returns total number of rows

collect()

Brings all records to driver

first() / head()

Returns first row(s)

take(n)

Returns n rows

toPandas()

Converts to Pandas DataFrame

write()

Write to storage (S3, DB, Delta)

Example of Action (Triggers Execution)

PLAINTEXT
1df2 = df.filter(df.salary > 50000)
2print(df2.take(2))
▶ Output will appear here.

Article content

Let's understand Lazy Evaluation and Directed Acyclic Graphs (DAG) with the example depicted in the above picture.

We start with source data, which can be in any format such as CSV or JSON and can originate from any source. This data is stored in DF1 as the source DataFrame.

In step 1, a filter transformation is applied to DF1. Since a filter is a transformation, it doesn't execute immediately. Instead, it creates a logical plan and updates the Directed Acyclic Graph (DAG). At the end of this transformation, a new DataFrame, DF2, is created. Since DataFrames are immutable, all the data inside DF1 remains unchanged, but a new DataFrame is created with filtered records.

Similarly, in step 2, we manipulate the data by concatenating two columns. Since this is also a transformation, it doesn't execute immediately but updates the DAG. At the end, another DataFrame, DF3, is created.

Now, suppose we perform an action by writing DF3 to an Amazon S3 bucket. This action refers to the DAG to check all the transformations and traverses to transformation 1, starting to execute all transformations. It's important to note that not all transformations will necessarily execute at once. The Spark Engine will determine the most cost-effective logical plan to achieve the action. Due to this characteristic, it's considered Lazily evaluated.

There are two kinds of transformations:

1) Narrow Transformation:

Article content

2) Wide Transformation/ Shuffle:

Article content

Understanding DAG

The Directed Acyclic Graph (DAG) is a fundamental concept in the context of Apache Spark, representing the logical flow of operations within a Spark job. Understanding the DAG is crucial for comprehending how Spark processes data and optimizing the execution plan. Here’s a breakdown of the key aspects related to the DAG:

1. Logical Execution Plan:

2. DAG Visualization:

3. Stages:

4. Tasks:

5. Dependencies:

6. Optimizations:

7. DAG and Lazy Evaluation:

8. Performance Tuning:

9. Example:

More Spark tutorials

All tutorials · Try the free PySpark compiler · Practice challenges