hive udaf

求平均数 udaf函数
package com.hive.myhive.udaf;
import org.apache.hadoop.hive.ql.exec.UDAF;
import org.apache.hadoop.hive.ql.exec.UDAFEvaluator;

public class Avg extends UDAF{
	public static class AvgStruct
	{
		public  double num=0;
		public  int count=0;
	}
	public static class AvgHandle implements UDAFEvaluator 
	{
		private AvgStruct avgStruct=new AvgStruct();
		@Override
		public void init() {
//			super();
		}
		public boolean iterate(double s)
		{
			if(s!=0)
			{
				avgStruct.num+=s;
				avgStruct.count++;
			}
			return true;
		}
		public AvgStruct terminatePartial() {
			return avgStruct.count==0?null:avgStruct;
		}
		 public boolean merge(AvgStruct o) {
			 if(o!=null)
			 {
				 avgStruct.num+=o.num;
				 avgStruct.count+=o.count;
			 }
			 return true;
		 }
		 public double terminate() {
			 return avgStruct.count==0?null:(double)(avgStruct.num/avgStruct.count);
		 }
	}
}
打包和加入hive 和 hdf一样

你可能感兴趣的:(null,Class,merge)