[转]HIVE UDF/UDAF/UDTF的Map Reduce代码框架模板
FROM : http://hugh-wangp.iteye.com/blog/1472371
自己写代码时候的利用到的模板
1.必须继承org.apache.hadoop.hive.ql.exec.UDF2.必须实现evaluate函数,evaluate函数支持重载
- <span style="font-size: x-small;">package com.alibaba.hive.udf;
- import org.apache.hadoop.hive.ql.exec.UDF
- public class helloword extends UDF{
- public String evaluate(){
- return "hello world!";
- }
- public String evaluate(String str){
- return "hello world: " + str;
- }
- }</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():接收传入的参数,并进行内部的轮转。其返回类型为booleanterminatePartial():无参数,其为iterate函数轮转结束后,返回乱转数据,iterate和terminatePartial类似于hadoop的Combiner(iterate--mapper;terminatePartial--reducer)merge():接收terminatePartial的返回结果,进行数据merge操作,其返回类型为booleanterminate():返回最终的聚集函数结果
- <span style="font-size: x-small;">package com.alibaba.hive;
- import org.apache.hadoop.hive.ql.exec.UDAF;
- import org.apache.hadoop.hive.ql.exec.UDAFEvaluator;
- public class myAVG extends UDAF{
- public static class avgScore{
- private long pSum;
- private double pCount;
- }
- public static class AvgEvaluator extends UDAFEvaluator{
- avgScore score;
- public AvgEvaluator(){
- score = new avgScore();
- init();
- }
- /*
- *init函数类似于构造函数,用于UDAF的初始化
- */
- public void init(){
- score.pSum = 0;
- score.pCount = 0;
- }
- /*
- *iterate接收传入的参数,并进行内部的轮转。其返回类型为boolean
- *类似Combiner中的mapper
- */
- public boolean iterate(Double in){
- if(in != null){
- score.pSum += in;
- score.pCount ++;
- }
- return true;
- }
- /*
- *terminatePartial无参数,其为iterate函数轮转结束后,返回轮转数据
- *类似Combiner中的reducer
- */
- public avgScore terminatePartial(){
- return score.pCount == 0 ? null : score;
- }
- /*
- *merge接收terminatePartial的返回结果,进行数据merge操作,其返回类型为boolean
- */
- public boolean merge(avgScore in){
- if(in != null){
- score.pSum += in.pSum;
- score.pCount += in.pCount;
- }
- return true;
- }
- /*
- *terminate返回最终的聚集函数结果
- */
- public Double terminate(){
- return score.pCount == 0 ? null : Double.valueof(score.pSum/score.pCount);
- }
- }
- }</span>
UDTF步骤:
2.实现initialize, process, close三个方法
3.UDTF首先会
a.调用initialize方法,此方法返回UDTF的返回行的信息(返回个数,类型)
b.初始化完成后,会调用process方法,对传入的参数进行处理,可以通过forword()方法把结果返回
c.最后close()方法调用,对需要清理的方法进行清理
- <span style="font-size: x-small;"><span style="font-size: xx-small;">public class GenericUDTFExplode extends GenericUDTF {
- private ListObjectInspector listOI = null;
- @Override
- public void close() throws HiveException {
- }
- @Override
- public StructObjectInspector initialize(ObjectInspector[] args) throws UDFArgumentException {
- if (args.length != 1) {
- throw new UDFArgumentException("explode() takes only one argument");
- }
- if (args[0].getCategory() != ObjectInspector.Category.LIST) {
- throw new UDFArgumentException("explode() takes an array as a parameter");
- }
- listOI = (ListObjectInspector) args[0];
- ArrayList<String> fieldNames = new ArrayList<String>();
- ArrayList<ObjectInspector> fieldOIs = new ArrayList<ObjectInspector>();
- fieldNames.add("col");
- fieldOIs.add(listOI.getListElementObjectInspector());
- return ObjectInspectorFactory.getStandardStructObjectInspector(fieldNames,
- fieldOIs);
- }
- private final Object[] forwardObj = new Object[1];
- @Override
- public void process(Object[] o) throws HiveException {
- List<?> list = listOI.getList(o[0]);
- if(list == null) {
- return;
- }
- for (Object r : list) {
- forwardObj[0] = r;
- forward(forwardObj);
- }
- }
- @Override
- public String toString() {
- return "explode";
- }
- }</span></span>
[转]HIVE UDF/UDAF/UDTF的Map Reduce代码框架模板的更多相关文章
- 【转】HIVE UDF UDAF UDTF 区别 使用
原博文出自于:http://blog.csdn.net/longzilong216/article/details/23921235(暂时) 感谢! 自己写代码时候的利用到的模板 UDF步骤: 1 ...
- hive中 udf,udaf,udtf
1.hive中基本操作: DDL,DML 2.hive中函数 User-Defined Functions : UDF(用户自定义函数,简称JDF函数)UDF: 一进一出 upper lower ...
- Hive 自定义函数 UDF UDAF UDTF
1.UDF:用户定义(普通)函数,只对单行数值产生作用: 继承UDF类,添加方法 evaluate() /** * @function 自定义UDF统计最小值 * @author John * */ ...
- 简述UDF/UDAF/UDTF是什么,各自解决问题及应用场景
UDF User-Defined-Function 自定义函数 .一进一出: 背景 系统内置函数无法解决实际的业务问题,需要开发者自己编写函数实现自身的业务实现诉求. 应用场景非常多,面临的业务不同导 ...
- Hive UDF 实验1
项目中使用的hive版本低于0.11,无法使用hive在0.11中新加的开窗分析函数. 在项目中需要使用到row_number()函数的地方,有人写了udf来实现这个功能. new java proj ...
- UDF/UDAF开发总结
参考文章: https://www.cnblogs.com/itxuexiwang/p/6264547.html https://www.cnblogs.com/eRrsr/p/6096989.htm ...
- HIVE UDF
基本函数 SHOW FUNCTIONS; DESCRIBE FUNCTION <function_name>; 日期函数 返回值类型 名称 描述 string from_unixtime( ...
- Hive UDF,就这
摘要:Hive UDF是什么?有什么用?怎么用?什么原理?本文从UDF使用入手,简要介绍相关源码,UDF从零开始. 本文分享自华为云社区<Hive UDF,就这>,作者:汤忒撒. Hive ...
- Hive自定义UDAF详解
遇到一个Hive需求:有A.B.C三列,按A列进行聚合,求出C列聚合后的最小值和最大值各自对应的B列值.这个需求用hql和内建函数也可完成,但是比较繁琐,会解析成几个MR进行执行,如果自定义UDAF便 ...
随机推荐
- 【Java】 大话数据结构(11) 查找算法(2)(二叉排序树/二叉搜索树)
本文根据<大话数据结构>一书,实现了Java版的二叉排序树/二叉搜索树. 二叉排序树介绍 在上篇博客中,顺序表的插入和删除效率还可以,但查找效率很低:而有序线性表中,可以使用折半.插值.斐 ...
- P2858 [USACO06FEB]奶牛零食Treats for the Cows
P2858 [USACO06FEB]奶牛零食Treats for the Cows区间dp,级像矩阵取数, f[i][i+l]=max(f[i+1][i+l]+a[i]*(m-l),f[i][i+l- ...
- Javascript实现一个插件
写一个插件,兼容commonjs,amd,cmd,原生js. ;(function (global, factory) { if(typeof define == 'function' &&a ...
- curl dns缓存设置
CURLOPT_DNS_USE_GLOBAL_CACHE 启用时会启用一个全局的DNS缓存,此项为线程安全的,并且默认启用.CURLOPT_DNS_CACHE_TIMEOUT 设置在内存中保存DNS信 ...
- 吴恩达-coursera-机器学习-week8
十三.聚类(Clustering) 13.1 无监督学习:简介 13.2 K-均值算法 13.3 优化目标 13.4 随机初始化 13.5 选择聚类数 十四.降维(Dimensionality Red ...
- Codeforces Round #374 (Div. 2) D. Maxim and Array 贪心
D. Maxim and Array 题目连接: http://codeforces.com/contest/721/problem/D Description Recently Maxim has ...
- Western Subregional of NEERC, Minsk, Wednesday, November 4, 2015 Problem A. A + B
Problem A. A + B 题目连接: http://opentrains.snarknews.info/~ejudge/team.cgi?SID=c75360ed7f2c7022&al ...
- 【原】Maven解决jar冲突调试步骤:第三方组件引用不符合要求的javassit导致的相关异常
[环境参数]开发框架:Spring + MyBatis + SpringMVC + KettleJDK版本:1.8.0_91javassist依赖版本:javassit-3.12.1.GA [障碍再现 ...
- sqlserver 2012 IDE中 Windows身份验证连接服务器报错 ,Login failed for user 'xxx\Administrator'. 原因: 找不到与提供的名称匹配的登录名。
问题描述: 本地装了两个实例,一个是SQLEXPRESS,可以正常操作.但是另一个开发常用的实例MSSQLSERVER却连Windows身份验证都报错,报的错误也是很奇葩,怎么会找不到Administ ...
- VirtualBox 在WIN7 X64 安装报错 获取VirtualBox COM对象失败,Unable to start the virtual device
Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\CLSID\{---C000-}\InprocServer32] @="C:\ ...