Database Reference
In-Depth Information
public static class
MaximumIntUDAFEvaluator
implements
UDAFEvaluator
{
private
IntWritable result
;
public
void
init
() {
result
=
null
;
}
public
boolean
iterate
(
IntWritable 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
() {
return
result
;
}
public
boolean
merge
(
IntWritable other
) {
return
iterate
(
other
);
}
public
IntWritable
terminate
() {
return
result
;
}
}
}
The class structure is slightly different from the one for UDFs. A UDAF must be a sub-
class of
org.apache.hadoop.hive.ql.exec.UDAF
(note the “A” in UDAF) and
contain one or more nested static classes implementing
org.apache.hadoop.hive.ql.exec.UDAFEvaluator
. In this example, there
is a single nested class,
MaximumIntUDAFEvaluator
, but we could add more evalu-
ators, such as
MaximumLongUDAFEvaluator
,
MaximumFloatUDAFEvaluator
,
and so on, to provide overloaded forms of the UDAF for finding the maximum of a col-
lection of longs, floats, and so on.