• HIVE函数UDAF 最大值


    Maximum 

    package udf.hive.myudf;
    
    
    
    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() {
    			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;
    		}
    	}
    
    }
    

      

    然后通过mvn打包。

    add JAR /home/cloudera/Desktop/myudf-0.0.3-SNAPSHOT.jar;

    CREATE TEMPORARY FUNCTION my_max2 AS 'udf.hive.myudf.Maximum';

    hive> select pid from prouct;
    OK
    1
    2
    3
    4



    select my_max2(oid) from prouct;

    OK
    4
    Time taken: 166.32 seconds, Fetched: 1 row(s)

  • 相关阅读:
    C# 接口
    C# 多态
    C# 继承
    C# 封装
    动态规划:从新手到专家
    hduoj题目分类
    4.2 最邻近规则分类(K-Nearest Neighbor)KNN算法应用
    警惕自增的陷阱(++)
    五大常用算法之四:回溯法
    算法java实现--回溯法--图的m着色问题
  • 原文地址:https://www.cnblogs.com/itxuexiwang/p/6263110.html
Copyright © 2020-2023  润新知