阿川CH
学海无涯,上栽上栽!
Toggle navigation
阿川CH
主页
归档
标签
UDAF:oneof(任意获得待聚合集中的一个)
Hive
Hive UDF
2018-08-31 18:51:22
0
0
0
cqc
Hive
Hive UDF
``` package cn.cqc3073.hive.udaf; import org.apache.hadoop.hive.ql.exec.Description; import org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException; import org.apache.hadoop.hive.ql.metadata.HiveException; import org.apache.hadoop.hive.ql.parse.SemanticException; import org.apache.hadoop.hive.ql.udf.generic.AbstractGenericUDAFResolver; import org.apache.hadoop.hive.ql.udf.generic.GenericUDAFEvaluator; import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector; import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorUtils; import org.apache.hadoop.hive.serde2.typeinfo.TypeInfo; /** * 任意获得待聚合集中的一个 * 支持复杂数据结构(map, array) * Create by chenqichuan on 2017/7/11. * */ @Description( name = "oneof", value = "_FUNC_(expr) - Returns one of value of expr" ) public class OneOfCollect extends AbstractGenericUDAFResolver{ @Override public GenericUDAFEvaluator getEvaluator(TypeInfo[] parameters) throws SemanticException { if(parameters.length != 1) { throw new UDFArgumentTypeException(parameters.length - 1, "Exactly one argument is expected."); } return new OneOfCollectEvaluator(); } public static class OneOfCollectEvaluator extends GenericUDAFEvaluator{ private ObjectInspector inputOI; private ObjectInspector outputOI; static class OneOfCollectAgg implements AggregationBuffer{ Object o; } @Override public ObjectInspector init(Mode m, ObjectInspector[] parameters) throws HiveException { super.init(m, parameters); assert parameters.length == 1; inputOI = parameters[0]; outputOI = ObjectInspectorUtils.getStandardObjectInspector(inputOI); return outputOI; } @Override public AggregationBuffer getNewAggregationBuffer() throws HiveException { AggregationBuffer result = new OneOfCollectAgg(); reset(result); return result; } @Override public void reset(AggregationBuffer aggregationBuffer) throws HiveException { ((OneOfCollectAgg)aggregationBuffer).o = null; } @Override public void iterate(AggregationBuffer aggregationBuffer, Object[] objects) throws HiveException { merge(aggregationBuffer, objects[0]); } private Object readValue(Object o){ return ObjectInspectorUtils.copyToStandardObject(o, inputOI); } @Override public Object terminatePartial(AggregationBuffer aggregationBuffer) throws HiveException { return terminate(aggregationBuffer); } @Override public void merge(AggregationBuffer aggregationBuffer, Object o) throws HiveException { if (o == null) { return; } OneOfCollectAgg agg = (OneOfCollectAgg)aggregationBuffer; if (agg.o == null){ agg.o = readValue(o); } } @Override public Object terminate(AggregationBuffer aggregationBuffer) throws HiveException { return ((OneOfCollectAgg)aggregationBuffer).o; } } } ```
上一篇:
HDFS小文件合并工具,支持snappy、orc格式
下一篇:
UDF:weekdiff(计算指定的两个指定的日期之前相差几个自然周)
文档导航