[转]HIVE UDF/UDAF/UDTF的Map Reduce代码框架模板

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

自己写代码时候的利用到的模板

 
UDF步骤:
1.必须继承org.apache.hadoop.hive.ql.exec.UDF
2.必须实现evaluate函数,evaluate函数支持重载

  1. <span style="font-size: x-small;">package com.alibaba.hive.udf;
  2. import org.apache.hadoop.hive.ql.exec.UDF
  3. public class helloword extends UDF{
  4. public String evaluate(){
  5. return "hello world!";
  6. }
  7. public String evaluate(String str){
  8. return "hello world: " + str;
  9. }
  10. }</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():返回最终的聚集函数结果

  1. <span style="font-size: x-small;">package com.alibaba.hive;
  2. import org.apache.hadoop.hive.ql.exec.UDAF;
  3. import org.apache.hadoop.hive.ql.exec.UDAFEvaluator;
  4. public class myAVG extends UDAF{
  5. public static class avgScore{
  6. private long pSum;
  7. private double pCount;
  8. }
  9. public static class AvgEvaluator extends UDAFEvaluator{
  10. avgScore score;
  11. public AvgEvaluator(){
  12. score = new avgScore();
  13. init();
  14. }
  15. /*
  16. *init函数类似于构造函数,用于UDAF的初始化
  17. */
  18. public void init(){
  19. score.pSum = 0;
  20. score.pCount = 0;
  21. }
  22. /*
  23. *iterate接收传入的参数,并进行内部的轮转。其返回类型为boolean
  24. *类似Combiner中的mapper
  25. */
  26. public boolean iterate(Double in){
  27. if(in != null){
  28. score.pSum += in;
  29. score.pCount ++;
  30. }
  31. return true;
  32. }
  33. /*
  34. *terminatePartial无参数,其为iterate函数轮转结束后,返回轮转数据
  35. *类似Combiner中的reducer
  36. */
  37. public avgScore terminatePartial(){
  38. return score.pCount == 0 ? null : score;
  39. }
  40. /*
  41. *merge接收terminatePartial的返回结果,进行数据merge操作,其返回类型为boolean
  42. */
  43. public boolean merge(avgScore in){
  44. if(in != null){
  45. score.pSum += in.pSum;
  46. score.pCount += in.pCount;
  47. }
  48. return true;
  49. }
  50. /*
  51. *terminate返回最终的聚集函数结果
  52. */
  53. public Double terminate(){
  54. return score.pCount == 0 ? null : Double.valueof(score.pSum/score.pCount);
  55. }
  56. }
  57. }</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()方法调用,对需要清理的方法进行清理

  1. <span style="font-size: x-small;"><span style="font-size: xx-small;">public class GenericUDTFExplode extends GenericUDTF {
  2. private ListObjectInspector listOI = null;
  3. @Override
  4. public void close() throws HiveException {
  5. }
  6. @Override
  7. public StructObjectInspector initialize(ObjectInspector[] args) throws UDFArgumentException {
  8. if (args.length != 1) {
  9. throw new UDFArgumentException("explode() takes only one argument");
  10. }
  11. if (args[0].getCategory() != ObjectInspector.Category.LIST) {
  12. throw new UDFArgumentException("explode() takes an array as a parameter");
  13. }
  14. listOI = (ListObjectInspector) args[0];
  15. ArrayList<String> fieldNames = new ArrayList<String>();
  16. ArrayList<ObjectInspector> fieldOIs = new ArrayList<ObjectInspector>();
  17. fieldNames.add("col");
  18. fieldOIs.add(listOI.getListElementObjectInspector());
  19. return ObjectInspectorFactory.getStandardStructObjectInspector(fieldNames,
  20. fieldOIs);
  21. }
  22. private final Object[] forwardObj = new Object[1];
  23. @Override
  24. public void process(Object[] o) throws HiveException {
  25. List<?> list = listOI.getList(o[0]);
  26. if(list == null) {
  27. return;
  28. }
  29. for (Object r : list) {
  30. forwardObj[0] = r;
  31. forward(forwardObj);
  32. }
  33. }
  34. @Override
  35. public String toString() {
  36. return "explode";
  37. }
  38. }</span></span>
上一篇:HDU 5734 Acperience(数学推导)


下一篇:深入理解计算机系统第二版习题解答CSAPP 2.7