public class Sum extendsAbstractGenericUDAFResolver {//创建log对象,用于抛出错误和异常
static final Log log = LogFactory.getLog(Sum.class.getName());//判断sql语句传入的参数的个数和类型,并将其返回相应的类型
@Overridepublic GenericUDAFEvaluator getEvaluator(TypeInfo[] info) throwsSemanticException {//判断参数的个数是否符合要求
if (info.length != 1) {throw new UDFArgumentTypeException(info.length - 1, "exactly one parameter expected");
}//判断传入的参数类型
if (info[0].getCategory() !=ObjectInspector.Category.PRIMITIVE) {throw new UDFArgumentTypeException(0, "only primitive argument is expected but " +info[0].getTypeName() + "is passed");
}//对传入的参数类型进行进一步的判断是否是我们需求的数据的类型
switch (((PrimitiveTypeInfo) info[0]).getPrimitiveCategory()) {caseBYTE:caseSHORT:caseINT:caseLONG:caseFLOAT:caseDOUBLE:return newSumRes();default:throw new UDFArgumentTypeException(0, "only numric type is expected but " + inf

本文介绍了如何在Hive中创建一个名为SumRes的自定义UDAF(用户定义聚合函数),用于对整数类型的数值进行求和操作。通过覆盖`GenericUDAFEvaluator`的方法,实现了在Map、Combiner和Reduce阶段的数据处理,包括初始化、迭代、合并和终止等功能。
最低0.47元/天 解锁文章
1111

被折叠的 条评论
为什么被折叠?



