-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSumReducer.java
More file actions
50 lines (43 loc) · 1.47 KB
/
SumReducer.java
File metadata and controls
50 lines (43 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package edu.easternct.bigdata;
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
/*
* To define a reduce function for your MapReduce job, subclass
* the Reducer class and override the reduce method.
* The class definition requires four parameters:
* The data type of the input key (which is the output key type
* from the mapper)
* The data type of the input value (which is the output value
* type from the mapper)
* The data type of the output key
* The data type of the output value
*/
public class SumReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
/*
* The reduce method runs once for each key received from
* the shuffle and sort phase of the MapReduce framework.
* The method receives a key of type Text, a set of values of type
* IntWritable, and a Context object.
*/
@Override
public void reduce(Text key, Iterable<IntWritable> values, Context context)
throws IOException, InterruptedException {
int wordCount = 0;
/*
* For each value in the set of values passed to us by the mapper:
*/
for (IntWritable value : values) {
/*
* Add the value to the word count counter for this key.
*/
wordCount += value.get();
}
/*
* Call the write method on the Context object to emit a key
* and a value from the reduce method.
*/
context.write(key, new IntWritable(wordCount));
}
}