Skip to the page

PySpark interview question · medium

How are transformations and actions defined in Spark, and what are some examples?

The short answerIn Spark, transformations and actions are the two fundamental types of operations for processing distributed data.

The full answer

In Spark, transformations and actions are the two fundamental types of operations for processing distributed data. Understanding their nuances is key to efficient Spark usage:


Transformations

  • Transformations are operations that define how data should be processed. They are lazy, meaning they only build a logical execution plan without triggering computation.
  • Transformations are immutable—each transformation produces a new RDD, DataFrame, or Dataset rather than modifying the original.
  • Transformations are of two types:
    • Narrow Transformations: Operate on data within a single partition (e.g., map, filter). These are more efficient as they don't require data movement.
    • Wide Transformations: Require data to be shuffled across partitions (e.g., groupByKey, reduceByKey). These can be expensive due to network I/O.
  • Examples: map, flatMap, filter, groupByKey, reduceByKey.

Actions

  • Actions trigger the execution of transformations and produce results or write data to storage.
  • Actions collect data back to the driver or initiate computations on the cluster.
  • Be cautious with actions like collect, as they can overwhelm the driver if the dataset is too large to fit in memory.
  • Actions are the final step in a Spark job and determine the point where the execution plan is realized.
  • Examples: collect, count, take, saveAsTextFile, foreach.

Code Example:

from pyspark.sql import SparkSession

# Initialize a SparkSession
spark = SparkSession.builder.appName("Transformations and Actions").getOrCreate()

# Create an RDD from a list
data = [1, 2, 3, 4, 5]
rdd = spark.sparkContext.parallelize(data)

# Apply transformations
transformed_rdd = rdd.map(lambda x: x * 2).filter(lambda x: x > 5)

# Transformations like map and filter are lazy and won't execute immediately

# Trigger an action
result = transformed_rdd.collect()  # Action triggers execution and returns the result

print("Result:", result)

# Stop the SparkSession
spark.stop()

Explanation with Examples:

  1. Transformations in Code:

    • The map operation doubles each element of the input RDD, creating a new RDD.
    • The filter operation keeps only elements greater than 5, resulting in another new RDD.
    • These transformations build the computation plan but are not executed until an action is called.
  2. Action in Code:

    • The collect action triggers execution, processes the data through the DAG, and retrieves the result [6, 8, 10] to the driver.
  3. Nuances to Consider:

    • Lazy Execution: Allows Spark to optimize the entire DAG before execution.
    • Wide Transformations: Can cause shuffles, which are expensive; avoid them unless necessary.
    • Large Actions: Avoid collect or take on large datasets; use distributed operations like saveAsTextFile.

Transformations define the "what," while actions determine the "when," allowing Spark to efficiently manage and execute distributed computations.

Last updated 24 January 2025