A transformation is an operation on an RDD or DataFrame that returns a new RDD or DataFrame. It is lazy, meaning it does not execute immediately but builds a plan for computation.
Click to reveal answer
beginner
What is an action in Apache Spark?
An action triggers the execution of the transformations and returns a result to the driver or writes data to storage. Actions cause Spark to compute the data.
Click to reveal answer
intermediate
Why are transformations called lazy in Spark?
Because Spark does not run transformations immediately. It waits until an action is called to optimize and execute the whole set of transformations together.
Click to reveal answer
beginner
Give an example of a transformation and an action in Spark.
Actions trigger computation, so they can be expensive. Minimizing actions and combining transformations helps Spark optimize execution and improve performance.
Click to reveal answer
Which of the following is a transformation in Spark?
Afilter()
Bcount()
Ccollect()
Dtake()
✗ Incorrect
filter() is a transformation that returns a new RDD or DataFrame without triggering computation.
What does an action in Spark do?
ACreates a new RDD lazily
BTriggers execution of transformations
CDefines a data schema
DCaches data in memory
✗ Incorrect
Actions trigger Spark to execute all pending transformations and return results or write data.
Why are transformations in Spark considered lazy?
AThey build a plan but do not execute until an action runs
BThey cache data
CThey write data to disk
DThey execute immediately
✗ Incorrect
Transformations only build a computation plan and wait for an action to trigger execution.
Which of these is an action in Spark?
Amap()
BflatMap()
Cfilter()
Dreduce()
✗ Incorrect
reduce() is an action that aggregates data and triggers computation.
What happens if you call multiple transformations without an action?
ASpark executes each transformation immediately
BSpark caches all data automatically
CNo computation happens until an action is called
DSpark throws an error
✗ Incorrect
Transformations are lazy and do not execute until an action triggers computation.
Explain the difference between transformations and actions in Apache Spark.
Think about when Spark actually runs the code.
You got /4 concepts.
Why is lazy evaluation important in Spark's transformations?
Consider how Spark saves time and resources.
You got /4 concepts.
Practice
(1/5)
1. Which of the following best describes a transformation in Apache Spark?
easy
A. It creates a new dataset from an existing one without running the job immediately.
B. It runs the job and returns the final result immediately.
C. It saves the dataset to an external storage system.
D. It deletes data from the dataset permanently.
Solution
Step 1: Understand what transformations do
Transformations in Spark create a new dataset from an existing one but do not execute the job immediately.
Step 2: Differentiate from actions
Actions trigger the execution and return results or save data, unlike transformations.
Final Answer:
It creates a new dataset from an existing one without running the job immediately. -> Option A
Quick Check:
Transformation = Lazy evaluation [OK]
Hint: Transformations are lazy; they don't run until an action triggers them [OK]
Common Mistakes:
Confusing transformations with actions
Thinking transformations execute immediately
Believing transformations save or delete data
2. Which of the following is the correct syntax to perform an action that counts the number of elements in an RDD named data?
easy
A. data.map()
B. data.count()
C. data.filter()
D. data.transform()
Solution
Step 1: Identify the action to count elements
The count() method is an action that returns the number of elements in an RDD.
Step 2: Check syntax correctness
Calling data.count() correctly triggers the action and returns the count.
Final Answer:
data.count() -> Option B
Quick Check:
Action to count = data.count() [OK]
Hint: Actions like count() end with parentheses and return results [OK]
Common Mistakes:
Using transformations like map() as actions
Calling non-existent methods like transform()
Forgetting parentheses on actions
3. Given the following Spark code, what will be the output?
rdd = sc.parallelize([1, 2, 3, 4])
mapped = rdd.map(lambda x: x * 2)
result = mapped.collect()
medium
A. An error because collect() is not an action
B. [1, 2, 3, 4]
C. [2, 4, 6, 8]
D. An empty list [] because map is lazy
Solution
Step 1: Understand the transformation
The map transformation multiplies each element by 2 but does not run immediately.
Step 2: Understand the action
The collect() action triggers execution and returns all elements as a list.
Final Answer:
[2, 4, 6, 8] -> Option C
Quick Check:
Transformation + collect() = output list [OK]
Hint: collect() runs the job and returns all data as a list [OK]
Common Mistakes:
Thinking map() runs immediately
Believing collect() returns nothing
Confusing transformations with actions
4. Identify the error in the following Spark code snippet: