/
GradingReducer.java
38 lines (33 loc) · 1.32 KB
/
GradingReducer.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.grading;
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 GradingMapper)
// E: Reducer Output Key, F: Reducer Output Value
public class GradingReducer 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 studentId = key.toString();
int totalScore = 0;
for (IntWritable value : values) {
int score = value.get();
totalScore += score;
}
context.write(new Text(studentId), new IntWritable(totalScore));
// or you can use the following instead
//context.write(key, new IntWritable(totalScore));
}
@Override
protected void cleanup(Context context) {
System.out.println("Reduce" + String.valueOf(this.reduceNumber) + ": End");
}
}