hive UDAF

hive UDAF_第1张图片

java 程序

package com.ibeifeng.udaf;

import org.apache.hadoop.hive.ql.exec.UDAF;
import org.apache.hadoop.hive.ql.exec.UDAFEvaluator;

public class Wm_concat extends UDAF {
    
    public static class MYUDAFVAL implements UDAFEvaluator{

        private PartialResult partial = new PartialResult();
        public static class PartialResult{
            String result = "";
            String delilmiter = null;
        }
        @Override
        public void init() {
            // TODO 自动生成的方法存根
            partial.result = "";
            
        }
        public boolean iterate(String value, String deli){
             if(value == null || "null".equalsIgnoreCase(value)){
                 return true;
             }
             if(partial.delilmiter == null){
                 partial.delilmiter = deli;
                 
             }
             if(partial.result.length() > 0){
                 
                 partial.result = partial.result.concat(partial.delilmiter);
                 
             }
             
             partial.result = partial.result.concat(value);
             
             return true;
             
        }
        
        public PartialResult terminatePartial(){
            
            return partial;
        }
        
        public boolean merge(PartialResult other){
            
             if(other == null)
                 return true;
              if(partial.delilmiter == null){
                  partial.delilmiter = other.delilmiter;
                  partial.result = other.result;
              }else{
                  if(partial.result.length()>0){
                      partial.result = partial.result.concat(partial.delilmiter);
                      
                  }
                  partial.result = partial.result.concat(other.result);
                  
              }
              return true;
             
        }
        public String terminate(){
            
            if(partial == null || partial.result == null){
                
                return null;
            }
            return partial.result;
        }
    }

}

 

你可能感兴趣的:(hive UDAF)