/
WordCountReducer.java
38 lines (33 loc) · 1.32 KB
/
WordCountReducer.java
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
package ymatsubara.hadoop.example.wordcount;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
// Reducer<C, D, E, F>
// C: Reducer Input Key, D: Reducer Input Key (same to C and D in WordCountMapper)
// E: Reducer Output Key, F: Reducer Output Value
public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
private static int reduceNumber = 0;
@Override
protected void setup(Context context) {
this.reduceNumber++;
System.out.println("Reduce" + String.valueOf(this.reduceNumber) + ": Start");
}
// reduce(C, D, Context)
@Override
public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
String word = key.toString();
int totalCount = 0;
for (IntWritable value : values) {
int count = value.get();
totalCount += count;
}
context.write(new Text(word), new IntWritable(totalCount));
// or you can use the following instead
//context.write(key, new IntWritable(totalCount));
}
@Override
protected void cleanup(Context context) {
System.out.println("Reduce" + String.valueOf(this.reduceNumber) + ": End");
}
}