zoukankan      html  css  js  c++  java
  • 计算平均数UDAF

    package com.suning.udf;
    
    
    import org.apache.hadoop.hive.ql.exec.UDAF;
    import org.apache.hadoop.hive.ql.exec.UDAFEvaluator;
    
    public class Avg extends UDAF {
        public static class AvgState {
            private long mCount;
            private double mSum;
        }
    
        public static class AvgEvaluator implements UDAFEvaluator {
            AvgState state;
            public AvgEvaluator() {
                super();
                state = new AvgState();
                init();
            }
    
    
            /**
             * init函数类似于构造函数,用于UDAF的初始化
             */
            public void init() {
                state.mSum = 0;
                state.mCount = 0;
            }
    
            /**
             * iterate接收传入的参数,并进行内部的轮转。其返回类型为boolean * * @param o * @return
             */
    
            public boolean iterate(Double o) {
                if (o != null) {
                    state.mSum += o;
                    state.mCount++;
                }
                return true;
            }
    
            /**
             * terminatePartial无参数,其为iterate函数遍历结束后,返回轮转数据, * terminatePartial类似于hadoop的Combiner * * @return
             */
    
            public AvgState terminatePartial() {
                // combiner
                return state.mCount == 0 ? null : state;
            }
    
            /**
             * merge接收terminatePartial的返回结果,进行数据merge操作,其返回类型为boolean * * @param o * @return
             */
    
            public boolean merge(AvgState avgState) {
                if (avgState != null) {
                    state.mCount += avgState.mCount;
                    state.mSum += avgState.mSum;
                }
                return true;
            }
    
            /**
             * terminate返回最终的聚集函数结果 * * @return
             */
            public Double terminate() {
                return state.mCount == 0 ? null : Double.valueOf(state.mSum / state.mCount);
            }
        }
    }
  • 相关阅读:
    Inno Setup进阶之事件函数(二)
    nginx日志相关指令(十二)
    linux下操作文件以分隔符切割并去重
    Inno Setup进阶之窗口初始化(一)
    nginx之CoreFunctionality(十一)
    nginx常量参数(十)
    Inno Setup安装添加条件语句到Run
    nginx命令行参数修改(九)
    nginx单位符号(八)
    nginx连接处理方式(七)
  • 原文地址:https://www.cnblogs.com/yin-fei/p/10879719.html
Copyright © 2011-2022 走看看