📖 Scenario: Imagine you work at a company that collects sales data every day. You want to analyze this data step-by-step to find total sales per product category. Apache Spark helps you do this efficiently by building a pipeline of transformations.
🎯 Goal: You will create a Spark DataFrame with sales data, define a filter condition, apply transformations to select and group data, and finally show the result. This will demonstrate how transformations build a processing pipeline in Spark.
📋 What You'll Learn
Create a Spark DataFrame with given sales data
Define a filter condition variable
Apply transformations: filter, select, groupBy, and sum
Show the final aggregated sales per category
💡 Why This Matters
🌍 Real World
Data engineers and data scientists use Spark pipelines to process large datasets efficiently by chaining transformations before running actions.
💼 Career
Understanding how transformations build pipelines is essential for optimizing Spark jobs and writing scalable data processing code.
Progress0 / 4 steps
1
Create the initial Spark DataFrame
Create a Spark DataFrame called sales_df with these exact rows: ("apple", "fruit", 10), ("banana", "fruit", 15), ("carrot", "vegetable", 7), ("broccoli", "vegetable", 5). The columns should be product, category, and quantity.
Apache Spark
Hint
Use spark.createDataFrame with a list of tuples and specify the column names as a list.
2
Define a filter condition
Create a variable called filter_category and set it to the string "fruit". This will be used to filter the DataFrame.
Apache Spark
Hint
Just assign the string "fruit" to the variable filter_category.
3
Apply transformations to build the pipeline
Use the filter_category variable to filter sales_df where category equals filter_category. Then select category and quantity columns. Group by category and sum the quantity column. Save the result in a new DataFrame called result_df.
Apache Spark
Hint
Use filter, then select, then groupBy with agg(sum(...)). Remember to import sum from pyspark.sql.functions.
4
Show the final result
Use print to display the contents of result_df by calling its show() method.
Apache Spark
Hint
Call result_df.show() to display the DataFrame contents.
Practice
(1/5)
1. What does it mean when we say Spark transformations are lazy?
easy
A. They do not run immediately but build a plan to run later
B. They run instantly as soon as they are called
C. They save data permanently after each step
D. They only work on small datasets
Solution
Step 1: Understand the meaning of lazy evaluation in Spark
Lazy means Spark does not execute transformations right away but waits until an action is called.
Step 2: Connect lazy evaluation to building a processing plan
Transformations build a pipeline or plan that Spark will run later when needed.
Final Answer:
They do not run immediately but build a plan to run later -> Option A
Quick Check:
Lazy evaluation = build plan, not run immediately [OK]
Hint: Lazy means wait to run until action triggers [OK]
Common Mistakes:
Thinking transformations run immediately
Confusing lazy with saving data
Believing transformations only work on small data
2. Which of the following is the correct way to chain two transformations in Spark?
easy
A. rdd.map(...); filter(...)
B. rdd.filter(...).map(...)()
C. rdd.filter(...).map
D. rdd.map(...).filter(...)
Solution
Step 1: Review correct chaining syntax in Spark
Transformations are chained by calling one after another with dot notation, like rdd.map(...).filter(...).
Step 2: Identify incorrect syntax in other options
rdd.map(...); filter(...) uses semicolon which breaks chaining, rdd.filter(...).map(...)() has extra parentheses, rdd.filter(...).map misses parentheses after map.
Final Answer:
rdd.map(...).filter(...) -> Option D
Quick Check:
Chaining uses dot notation with parentheses [OK]
Hint: Use dot and parentheses to chain transformations [OK]
Common Mistakes:
Adding extra parentheses after chain
Using semicolons instead of chaining
Forgetting parentheses after function names
3. Given the code below, what will be the output after the action is called?
rdd = sc.parallelize([1, 2, 3, 4])
result = rdd.map(lambda x: x * 2).filter(lambda x: x > 4).collect()
medium
A. [2, 4, 6, 8]
B. [6, 8]
C. [1, 2, 3, 4]
D. Error due to missing action
Solution
Step 1: Apply the map transformation
Each element is multiplied by 2: [2, 4, 6, 8]
Step 2: Apply the filter transformation
Keep elements greater than 4: [6, 8]
Final Answer:
[6, 8] -> Option B
Quick Check:
Map then filter results in [6, 8] [OK]
Hint: Apply transformations step-by-step before action [OK]