[轉]HIVE UDF/UDAF/UDTF的Map Reduce代碼架構模板

來源:互聯網
上載者:User

標籤:

FROM : http://hugh-wangp.iteye.com/blog/1472371

 

自己寫代碼時候的利用到的模板

 UDF步驟:
1.必須繼承org.apache.hadoop.hive.ql.exec.UDF2.必須實現evaluate函數,evaluate函數支援重載
Java代碼  
  1. <span style="font-size: x-small;">package com.alibaba.hive.udf;  
  2.   
  3. import org.apache.hadoop.hive.ql.exec.UDF  
  4.   
  5. public class helloword extends UDF{  
  6.      public String evaluate(){  
  7.           return "hello world!";  
  8.      }  
  9.   
  10.      public String evaluate(String str){  
  11.           return "hello world: " + str;  
  12.      }  
  13. }</span>  
  UDAF步驟:
1.必須繼承     org.apache.hadoop.hive.ql.exec.UDAF(函數類繼承)     org.apache.hadoop.hive.ql.exec.UDAFEvaluator(內部類Evaluator實現UDAFEvaluator介面)2.Evaluator需要實現 init、iterate、terminatePartial、merge、terminate這幾個函數     init():類似於建構函式,用於UDAF的初始化     iterate():接收傳入的參數,並進行內部的輪轉。其傳回型別為boolean     terminatePartial():無參數,其為iterate函數輪轉結束後,返回亂轉資料,iterate和terminatePartial類似於hadoop的Combiner(iterate--mapper;terminatePartial--reducer)     merge():接收terminatePartial的返回結果,進行資料merge操作,其傳回型別為boolean     terminate():返回最終的聚集合函式結果
Java代碼  
  1. <span style="font-size: x-small;">package com.alibaba.hive;  
  2.   
  3. import org.apache.hadoop.hive.ql.exec.UDAF;  
  4. import org.apache.hadoop.hive.ql.exec.UDAFEvaluator;  
  5.   
  6. public class myAVG extends UDAF{  
  7.   
  8.      public static class avgScore{  
  9.           private long pSum;  
  10.           private double pCount;  
  11.      }  
  12.        
  13.      public static class AvgEvaluator extends UDAFEvaluator{  
  14.           avgScore score;  
  15.             
  16.           public AvgEvaluator(){  
  17.                score = new avgScore();  
  18.                init();  
  19.           }  
  20.             
  21.           /* 
  22.           *init函數類似於建構函式,用於UDAF的初始化 
  23.           */  
  24.           public void init(){  
  25.                score.pSum = 0;  
  26.                score.pCount = 0;  
  27.           }  
  28.             
  29.           /* 
  30.           *iterate接收傳入的參數,並進行內部的輪轉。其傳回型別為boolean 
  31.           *類似Combiner中的mapper 
  32.           */  
  33.           public boolean iterate(Double in){  
  34.                if(in != null){  
  35.                     score.pSum += in;  
  36.                     score.pCount ++;  
  37.                }  
  38.                return true;  
  39.           }  
  40.             
  41.           /* 
  42.           *terminatePartial無參數,其為iterate函數輪轉結束後,返回輪轉資料 
  43.           *類似Combiner中的reducer 
  44.           */  
  45.           public avgScore terminatePartial(){  
  46.                return score.pCount == 0 ? null : score;  
  47.           }  
  48.             
  49.           /* 
  50.           *merge接收terminatePartial的返回結果,進行資料merge操作,其傳回型別為boolean 
  51.           */  
  52.           public boolean merge(avgScore in){  
  53.                if(in != null){  
  54.                     score.pSum += in.pSum;  
  55.                     score.pCount += in.pCount;  
  56.                }  
  57.                return true;  
  58.           }  
  59.             
  60.           /* 
  61.           *terminate返回最終的聚集合函式結果 
  62.           */  
  63.           public Double terminate(){  
  64.                return score.pCount == 0 ? null : Double.valueof(score.pSum/score.pCount);  
  65.           }  
  66.      }  
  67. }</span>  

 

UDTF步驟:1.必須繼承org.apache.hadoop.hive.ql.udf.generic.GenericUDTF
2.實現initialize, process, close三個方法
3.UDTF首先會
     a.調用initialize方法,此方法返回UDTF的返回行的資訊(返回個數,類型)
     b.初始化完成後,會調用process方法,對傳入的參數進行處理,可以通過forword()方法把結果返回
     c.最後close()方法調用,對需要清理的方法進行清理Java代碼  
  1. <span style="font-size: x-small;"><span style="font-size: xx-small;">public class GenericUDTFExplode extends GenericUDTF {  
  2.   
  3.   private ListObjectInspector listOI = null;  
  4.   
  5.   @Override  
  6.   public void close() throws HiveException {  
  7.   }  
  8.   
  9.   @Override  
  10.   public StructObjectInspector initialize(ObjectInspector[] args) throws UDFArgumentException {  
  11.     if (args.length != 1) {  
  12.       throw new UDFArgumentException("explode() takes only one argument");  
  13.     }  
  14.   
  15.     if (args[0].getCategory() != ObjectInspector.Category.LIST) {  
  16.       throw new UDFArgumentException("explode() takes an array as a parameter");  
  17.     }  
  18.     listOI = (ListObjectInspector) args[0];  
  19.   
  20.     ArrayList<String> fieldNames = new ArrayList<String>();  
  21.     ArrayList<ObjectInspector> fieldOIs = new ArrayList<ObjectInspector>();  
  22.     fieldNames.add("col");  
  23.     fieldOIs.add(listOI.getListElementObjectInspector());  
  24.     return ObjectInspectorFactory.getStandardStructObjectInspector(fieldNames,  
  25.         fieldOIs);  
  26.   }  
  27.   
  28.   private final Object[] forwardObj = new Object[1];  
  29.   
  30.   @Override  
  31.   public void process(Object[] o) throws HiveException {  
  32.     List<?> list = listOI.getList(o[0]);  
  33.     if(list == null) {  
  34.       return;  
  35.     }  
  36.     for (Object r : list) {  
  37.       forwardObj[0] = r;  
  38.       forward(forwardObj);  
  39.     }  
  40.   }  
  41.   
  42.   @Override  
  43.   public String toString() {  
  44.     return "explode";  
  45.   }  
  46. }</span></span>  

[轉]HIVE UDF/UDAF/UDTF的Map Reduce代碼架構模板

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.