UDAF

import org.apache.hadoop.hive.ql.exec.UDAF;  
import org.apache.hadoop.hive.ql.exec.UDAFEvaluator;  
import org.apache.hadoop.io.Text;  
import java.util.HashMap;  
import java.util.Map;  
public class helloUDAF extends UDAF {  
    public static class Evaluator implements UDAFEvaluator  
    {  
       //存放不同学生的总分  
       private static Map<String,Integer> ret;  
  
       public Evaluator()  
       {  
       super();  
           init();  
       }  
  
       //初始化  
       public void init()  
       {  
      ret = new HashMap<String,Integer>();  
       }  
  
       //map阶段,遍历所有记录  
       public boolean iterate(String strStudent,int nScore)  
       {   
         if(ret.containsKey(strStudent))  
         {  
            int nValue = ret.get(strStudent);  
            nValue +=nScore;  
            ret.put(strStudent,nValue);  
         }  
         else  
         {  
           ret.put(strStudent,nScore);  
         }  
         return true;  
       }  
      
       //返回最终结果   
       public Map<String,Integer> terminate()  
       {  
         return ret;  
       }  
  
       //combiner阶段,本例不需要  
       public Map<String,Integer> terminatePartial()   
       {  
          return ret;  
       }  
  
       //reduce阶段  
       public boolean merge(Map<String,Integer> other)  
       {  
            for (Map.Entry<String, Integer> e : other.entrySet()) {  
                ret.put(e.getKey(),e.getValue());  
            }  
            return true;  
       }  
    }     
}