/
Maximum.java
46 lines (37 loc) · 1.18 KB
/
Maximum.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
39
40
41
42
43
44
45
46
package com.hadoopbook.hive;
import org.apache.hadoop.hive.ql.exec.UDAF;
import org.apache.hadoop.hive.ql.exec.UDAFEvaluator;
import org.apache.hadoop.io.IntWritable;
public class Maximum extends UDAF {
public static class MaximumIntUDAFEvaluator implements UDAFEvaluator {
private IntWritable result;
public void init() {
System.err.printf("%s %s\n", hashCode(), "init");
result = null;
}
public boolean iterate(IntWritable value) {
System.err.printf("%s %s %s\n", hashCode(), "iterate", value);
if (value == null) {
return true;
}
if (result == null) {
result = new IntWritable(value.get());
} else {
result.set(Math.max(result.get(), value.get()));
}
return true;
}
public IntWritable terminatePartial() {
System.err.printf("%s %s\n", hashCode(), "terminatePartial");
return result;
}
public boolean merge(IntWritable other) {
System.err.printf("%s %s %s\n", hashCode(), "merge", other);
return iterate(other);
}
public IntWritable terminate() {
System.err.printf("%s %s\n", hashCode(), "terminate");
return result;
}
}
}