# hive_udf **Repository Path**: bravepig/hive_udf ## Basic Information - **Project Name**: hive_udf - **Description**: 创建UDF于UDAF - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2017-02-19 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ### 1. MaxIntUDAF.java ``` package com.learn.h; import org.apache.hadoop.hive.ql.exec.Description; import org.apache.hadoop.hive.ql.exec.UDAF; import org.apache.hadoop.hive.ql.exec.UDAFEvaluator; import org.apache.hadoop.io.IntWritable; @Description(name="my_max",value="_FUNC_(Int) - Returns the maxinum value of Int number") public class MaxIntUDAF extends UDAF{ public static class MaxNumberIntUDAFEvaluator implements UDAFEvaluator { private IntWritable result; public void init() { result = null; } public boolean iterate(IntWritable value) { if (value ==null) { return false; } 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; } } } ``` ### 2. MD5HashUDF.java ``` package com.learn.h; import org.apache.commons.codec.digest.DigestUtils; import org.apache.hadoop.hive.ql.exec.Description; import org.apache.hadoop.hive.ql.exec.UDF; @Description(name = "my_md5hash", value = "_FUNC_(str) - Returns the md5hex value of str") public class MD5HashUDF extends UDF { public String evaluate(String in) { return DigestUtils.md5Hex(in); } } ``` ### 3. hive命令行中执行:上传jar到classpath ``` add jar xx.jar ``` ### 4. hive命令行中执行:创建函数 ``` create temporary function my_md5hash as 'com.learn.h.MD5HashUDF'; create temporary function my_max as 'com.learn.h.MaxIntUDAF'; ``` ### 5. hive命令行中执行:验证函数 ``` select name,my_md5hash(name) from product; select my_max(pid) from product; ```