📖 Scenario: You work as a data analyst at a retail company. You want to analyze sales data using Apache Spark. Spark uses lazy evaluation, which means it waits to run your commands until it really needs to. This helps save time and resources.
🎯 Goal: Learn how to create a Spark DataFrame, apply transformations, and see when Spark actually runs the work using lazy evaluation.
📋 What You'll Learn
Create a Spark DataFrame with sales data
Define a filter condition as a configuration variable
Apply a filter transformation using lazy evaluation
Trigger the execution by showing the filtered data
💡 Why This Matters
🌍 Real World
Data analysts use lazy evaluation in Spark to write efficient data processing code that only runs when needed, saving time and computing resources.
💼 Career
Understanding lazy evaluation is key for roles like data engineer, data analyst, and data scientist working with big data tools like Apache Spark.
Progress0 / 4 steps
1
Create the sales DataFrame
Create a Spark DataFrame called sales_df with these exact rows: ("Store1", 100), ("Store2", 200), ("Store3", 150). Use columns named store and sales.
Apache Spark
Hint
Use spark.createDataFrame() with a list of tuples and a list of column names.
2
Set the sales threshold
Create a variable called threshold and set it to 150. This will be used to filter stores with sales above this number.
Apache Spark
Hint
Just assign the number 150 to a variable named threshold.
3
Filter the DataFrame using lazy evaluation
Create a new DataFrame called filtered_df by filtering sales_df to keep only rows where sales is greater than threshold. Use the DataFrame filter() method.
Apache Spark
Hint
Use filtered_df = sales_df.filter(sales_df.sales > threshold).
4
Show the filtered results
Use print() to display the result of filtered_df.show(). This will trigger Spark to run the filter and show the rows with sales above the threshold.
Apache Spark
Hint
Use filtered_df.show() inside a print() to display the filtered rows.
Practice
(1/5)
1. What does lazy evaluation mean in Apache Spark?
easy
A. Spark runs all transformations immediately when defined
B. Spark delays running transformations until an action is called
C. Spark only runs actions if there are no errors
D. Spark saves all data permanently after each step
Solution
Step 1: Understand lazy evaluation concept
Lazy evaluation means Spark does not run transformations right away but waits.
Step 2: Identify when Spark runs computations
Spark runs the actual work only when an action like collect() or count() is called.
Final Answer:
Spark delays running transformations until an action is called -> Option B
Quick Check:
Lazy evaluation = Delay until action [OK]
Hint: Remember: transformations wait, actions run [OK]
Common Mistakes:
Thinking transformations run immediately
Confusing actions with transformations
Believing Spark saves data after each step
2. Which of the following is the correct way to trigger execution in Spark?
easy
A. df.count()
B. df.select("name")
C. df.filter("age > 30")
D. df.withColumn("new", df["age"] + 1)
Solution
Step 1: Identify transformations vs actions
filter(), select(), and withColumn() are transformations (lazy).
Step 2: Recognize actions that trigger execution
count() is an action that forces Spark to run all prior transformations.
Final Answer:
df.count() -> Option A
Quick Check:
Action = count() triggers execution [OK]
Hint: Actions like count() run Spark jobs [OK]
Common Mistakes:
Choosing transformations as actions
Confusing withColumn as action
Thinking select triggers execution
3. What will be the output of this Spark code snippet?
val data = spark.range(1, 5)
val filtered = data.filter(_ % 2 == 0)
filtered.show()