Reduce phase explained in Hadoop - Mini Project: Build & Apply
sales_data with these exact entries: 'apple': 10, 'banana': 5, 'apple': 7, 'orange': 3, 'banana': 8. Use a list of tuples to represent multiple sales records.Use a list of tuples like [('apple', 10), ('banana', 5), ...]
min_units and set it to 5 to filter products with total units sold less than this.Just write min_units = 5
reduced_data. Use a for loop with variables product and units to iterate over sales_data. Add the units sold to reduced_data[product], initializing to 0 if the product is not yet in the dictionary.Use a dictionary to sum units. Check if product key exists before adding.
for loop with variables product and total_units to iterate over reduced_data.items(). Inside the loop, use an if statement to check if total_units is greater than or equal to min_units. If yes, print the product and total units in the format: product: total_units.Print only products with total units 5 or more.
Practice
What is the main purpose of the Reduce phase in Hadoop MapReduce?
Solution
Step 1: Understand the role of Reduce phase
The Reduce phase takes the output from the Map phase and groups data by keys.Step 2: Identify the main function of Reduce
It combines or aggregates the values for each key to produce summarized results.Final Answer:
To combine and summarize data from the Map outputs by key -> Option AQuick Check:
Reduce phase = combine by key [OK]
- Confusing Reduce with Map phase
- Thinking Reduce splits data
- Assuming Reduce reads input files
Which of the following is the correct signature of a Reduce function in Hadoop MapReduce?
public void reduce(KEY key, Iterable<VALUE> values, Context context) throws IOException, InterruptedExceptionSolution
Step 1: Recall Reduce function signature
The Reduce function receives a key and an iterable list of values for that key.Step 2: Match the correct method signature
public void reduce(KEY key, Iterable<VALUE> values, Context context) throws IOException, InterruptedException correctly shows the reduce method with key, Iterable values, and context parameters.Final Answer:
public void reduce(KEY key, Iterable<VALUE> values, Context context) throws IOException, InterruptedException -> Option CQuick Check:
Reduce signature = key + Iterable values + context [OK]
- Confusing map and reduce method signatures
- Using single VALUE instead of Iterable<VALUE> in reduce
- Mixing map parameters with reduce function
Given the following Reduce function snippet, what will be the output for key = "apple" and values = [2, 3, 5]?
public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}
context.write(key, new IntWritable(sum));
}Solution
Step 1: Sum the values list
Values are 2, 3, and 5. Summing gives 2 + 3 + 5 = 10.Step 2: Output key with summed value
The context writes the key "apple" with the sum 10 as IntWritable.Final Answer:
(apple, 10) -> Option BQuick Check:
Sum of values = 10 [OK]
- Returning only first value instead of sum
- Returning count instead of sum
- Ignoring values and outputting zero
Identify the error in this Reduce function code snippet:
public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable val : values) {
sum = val.get();
}
context.write(key, new IntWritable(sum));
}Solution
Step 1: Analyze the for loop logic
The code assigns sum = val.get() inside the loop, overwriting sum each time.Step 2: Identify correct accumulation
Sum should accumulate values using sum += val.get() to add all values.Final Answer:
The sum variable is overwritten instead of accumulated -> Option DQuick Check:
Use += to accumulate sum, not = [OK]
- Using = instead of += in accumulation
- Confusing key type with error
- Thinking context.write is missing
You want to count the total occurrences of each word in a large text dataset using Hadoop MapReduce. Which of the following best describes what the Reduce phase should do?
Solution
Step 1: Understand MapReduce word count flow
Map phase outputs (word, 1) pairs for each word occurrence.Step 2: Role of Reduce phase in counting
Reduce receives each word as key and list of counts (1s), sums them to get total count.Final Answer:
Receive each word as key and list of counts as values, then sum counts to get total occurrences -> Option AQuick Check:
Reduce sums counts per word [OK]
- Confusing Map and Reduce roles
- Thinking Reduce sorts data
- Assuming Reduce outputs raw data
Ready to prove it?
Pass the test and this pattern moves unknown
