📖 Scenario: You work at a small online store. You have a list of sales records showing the product name and the quantity sold. You want to find out the total quantity sold for each product.
🎯 Goal: Build a Spark program that sums the quantities sold for each product using reduce and aggregate actions.
📋 What You'll Learn
Create an RDD with the exact sales data given
Create a variable for the minimum quantity threshold
Use reduceByKey to sum quantities for each product
Print the final aggregated result
💡 Why This Matters
🌍 Real World
Summing sales quantities per product is a common task in retail analytics to understand product performance.
💼 Career
Data scientists and analysts often use reduce and aggregate actions in Spark to process large datasets efficiently.
Progress0 / 4 steps
1
Create the sales data RDD
Create a Spark RDD called sales_rdd from the list of tuples: [('apple', 10), ('banana', 5), ('apple', 3), ('banana', 7), ('orange', 8)].
Apache Spark
Hint
Use sc.parallelize() to create an RDD from the list of tuples.
2
Set the minimum quantity threshold
Create a variable called min_quantity and set it to 5.
Apache Spark
Hint
Just assign the number 5 to the variable min_quantity.
3
Sum quantities for each product using reduceByKey
Use reduceByKey on sales_rdd with a lambda function that adds two quantities to create a new RDD called total_sales_rdd.
Apache Spark
Hint
Use reduceByKey(lambda x, y: x + y) to add quantities for each product.
4
Print the total sales result
Print the list of tuples from total_sales_rdd.collect() to show the total quantity sold for each product.
Apache Spark
Hint
Use print(total_sales_rdd.collect()) to display the final sums.
Practice
(1/5)
1.
What does the reduce action do in Apache Spark?
easy
A. It filters elements based on a condition.
B. It creates a new RDD by mapping each element.
C. It combines all elements of an RDD using a specified function to produce a single result.
D. It sorts the elements in the RDD.
Solution
Step 1: Understand the purpose of reduce
The reduce action combines all elements of an RDD by applying a function that takes two elements and returns one, repeating until one result remains.
Step 2: Compare with other actions
Filtering selects elements, mapping transforms elements, and sorting orders elements, none of which combine all elements into one.
Final Answer:
It combines all elements of an RDD using a specified function to produce a single result. -> Option C
Quick Check:
Reduce = combine all elements [OK]
Hint: Reduce combines all data into one value using a function [OK]
Common Mistakes:
Confusing reduce with filter or map actions
Thinking reduce sorts data
Assuming reduce returns an RDD instead of a single value
2.
Which of the following is the correct syntax to use reduce on an RDD named rdd to sum all elements?
rdd.reduce(_____)
easy
A. lambda x: x * 2
B. lambda x, y: x + y
C. lambda x, y: x * y
D. lambda x: x + 1
Solution
Step 1: Identify the function signature for reduce
The function passed to reduce must take two arguments and return one combined result.
Step 2: Choose the function that sums two elements
Only lambda x, y: x + y takes two arguments and sums them, suitable for summing all elements.
Final Answer:
lambda x, y: x + y -> Option B
Quick Check:
Reduce needs two-arg function for combining [OK]
Hint: Reduce function must take two inputs and combine them [OK]
Common Mistakes:
Using single-argument lambda instead of two-argument
Using multiplication when sum is needed
Passing a function that does not return a single value