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>

[转]HIVE UDF/UDAF/UDTF的Map Reduce代码框架模板的更多相关文章

  1. 【转】HIVE UDF UDAF UDTF 区别 使用

    原博文出自于:http://blog.csdn.net/longzilong216/article/details/23921235(暂时) 感谢! 自己写代码时候的利用到的模板   UDF步骤: 1 ...

  2. hive中 udf,udaf,udtf

    1.hive中基本操作: DDL,DML 2.hive中函数 User-Defined Functions : UDF(用户自定义函数,简称JDF函数)UDF: 一进一出  upper  lower ...

  3. Hive 自定义函数 UDF UDAF UDTF

    1.UDF:用户定义(普通)函数,只对单行数值产生作用: 继承UDF类,添加方法 evaluate() /** * @function 自定义UDF统计最小值 * @author John * */ ...

  4. 简述UDF/UDAF/UDTF是什么,各自解决问题及应用场景

    UDF User-Defined-Function 自定义函数 .一进一出: 背景 系统内置函数无法解决实际的业务问题,需要开发者自己编写函数实现自身的业务实现诉求. 应用场景非常多,面临的业务不同导 ...

  5. Hive UDF 实验1

    项目中使用的hive版本低于0.11,无法使用hive在0.11中新加的开窗分析函数. 在项目中需要使用到row_number()函数的地方,有人写了udf来实现这个功能. new java proj ...

  6. UDF/UDAF开发总结

    参考文章: https://www.cnblogs.com/itxuexiwang/p/6264547.html https://www.cnblogs.com/eRrsr/p/6096989.htm ...

  7. HIVE UDF

    基本函数 SHOW FUNCTIONS; DESCRIBE FUNCTION <function_name>; 日期函数 返回值类型 名称 描述 string from_unixtime( ...

  8. Hive UDF,就这

    摘要:Hive UDF是什么?有什么用?怎么用?什么原理?本文从UDF使用入手,简要介绍相关源码,UDF从零开始. 本文分享自华为云社区<Hive UDF,就这>,作者:汤忒撒. Hive ...

  9. Hive自定义UDAF详解

    遇到一个Hive需求:有A.B.C三列,按A列进行聚合,求出C列聚合后的最小值和最大值各自对应的B列值.这个需求用hql和内建函数也可完成,但是比较繁琐,会解析成几个MR进行执行,如果自定义UDAF便 ...

随机推荐

  1. springBoot事物

    1.事物 只是需要一个注解即可 2.事物程序 package com.caojun.springboot; import org.springframework.beans.factory.annot ...

  2. Linux下安装matlab2014a

    下载Matlab 我放在百度云盘里了,下载链接: http://pan.baidu.com/s/1pLE1qgr 密码: x4tw 该文件下载解压后如下所示:该文件下载解压后如下所示: 注意linux ...

  3. 子域名枚举工具Sublist3r

    子域名枚举工具Sublist3r   通过搜集子域名信息,可以找到目标的关联网站,找寻相应的漏洞.Kali Linux提供一款基于OSINT的枚举工具Sublist3r.该工具会搜索多个数据来源,如G ...

  4. Redis五大类型操作使用以及订阅发布功能

    redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted set ...

  5. Codeforces Round #373 (Div. 2) C. Efim and Strange Grade 水题

    C. Efim and Strange Grade 题目连接: http://codeforces.com/contest/719/problem/C Description Efim just re ...

  6. 使用gtest对DLL工程进行单元测试的实践

    前言 关于单元测试的重要性.gtest的优缺点等就不说了.之前项目是没有做单元测试的,在VS的解决方案中,只有一个可执行的工程,其他的工程都是以DLL库的形式提供.本文只针对使用VS对DLL库进行单元 ...

  7. delete void pointer

    delete void pointer是否会有内存泄漏? 看下面一个简单例子 class Test{ public: Test(){ printf ("constructor\n" ...

  8. CentOS7忘记mysql的root密码_处理方法.

    1.打开mysql的配置文件: vi /etc/my.cnf 2.在配置文件中添加:skip-grant-tables,然后保存退出, vi常用命令在最后.   如图 3.重启mysql servic ...

  9. mysql_提示 Lock wait timeout exceeded解决办法

    我的mysql报这个错 err=1205 - Lock wait timeout exceeded; try restarting transaction 利用 SHOW PROCESSLIST来查看 ...

  10. ant design Modal关闭时清除数据的解决方案

    背景:modal组件关闭时不清除数据,原来输入的数据还存在 解决方案: 1.modal的api:destroyOnClose 2.手动控制modal的销毁 this.state = { destroy ...