hive中UDF,UDAF,UDTF编写和使用学习

UDF,UDAF,UDTF三个函数是Hive中常用的自定义函数,这些函数可以实现复杂的功能,且可以重复使用。
这篇文档,我们了解一下UDF,UDAF,UDTF三个函数编写和使用学习

UDF(User-Defined Functions)

功能特点:

输入一条数据,输出一条数据;是一个一对一的关系,属于单纯处理操作。

用途说明:

UDF函数可以直接应用于hive中的select语句,对查询结果做格式化或者各种处理后,再输出内容。

开发相关:

1.自定义UDF需要继承 org.apache.hadoop.hive.ql.exec.UDF;

2.一个UDF必须实现evaluate()方法(evaluate函数支持重载),但是该方法并不存在于UDF中。evaluate的参数个数以及类型都是用户按需定义。在使用的时候,Hive会调用UDF的evaluate()方法。

使用介绍:

第一步:继承UDF,实现特定的方法(evaluate)。
第二步:将写好的类打包为jar。如hive_process.jar.
第三步:进入到Hive外壳环境中,利用add jar /home/tmp/hadoop/hive_process.jar.注册添加该jar文件
第四步:创建函数的别名,create temporary function tmplength as ‘com.test.StringLength’;注意:这里注意“create temporary function”是函数临时会话创建的,会话关闭则消失。
第五步:在select中使用tmplength();

示例代码:
package test;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;
//必须继承UDF
public class StringLength extends UDF{
    //自定义实现evalution方法
    public Integer evaluate(String str)
    {
      if(str==null)
        return null;
      return str.length();
    }
    //参数由自己设定
    public Integer evaluate(String str,String stripChars)
    {
        if(str==null)
            return null;
        String tmp = StringUtils.strip(str.toString(),stripChars));
        return tmp.length();
    }
}

UDAF(User-Defined Aggregation Functions)

功能特点:输入多条数据,输出一条数据;是一个多对一的关系,属于聚合处理操作。

<待续>

UDTF(User-Defined Table-Generating Functions)

功能特点:

输入一条数据,输出多条数据;是一个一对多的关系,属于拆分处理操作。

开发相关:

1.继承org.apache.hadoop.hive.ql.udf.generic.GenericUDTF,
2.实现initialize, process, close三个方法(支持重载)。
3.UDTF首先会调用initialize方法,此方法返回UDTF的返回行的信息(返回个数,类型)。初始化完成后,会调用process方法,真正的处理过程在process函数中,在process中,每一次forward()调用产生一行;如果产生多列可以将多个列的值放在一个数组中,然后将该数组传入到forward()函数。

使用介绍:

第一步:将写好的类打包为jar。如hive_process.jar.
第二步:进入到Hive外壳环境中,利用add jar /home/tmp/hadoop/hive_process.jar.注册添加该jar文件
第三步:创建函数的别名,create temporary function tmpSplit as ‘com.test.ExplodeMap’;注意:这里注意“create temporary function”是函数临时会话创建的,会话关闭则消失。
第四步:在select中使用tmpSplit();或tmpSplit()在lateral view中使用。

代码示例:

下面是一个网上找的用来切分”key:value;key:value;”这种字符串,返回结果为key, value两个字段。供参考:

 package test;
 import java.util.ArrayList;
 import org.apache.hadoop.hive.ql.udf.generic.GenericUDTF;
 import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
 import org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException;
 import org.apache.hadoop.hive.ql.metadata.HiveException;
 import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
 import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory;
 import org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector;
 import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;

 public class ExplodeMap extends GenericUDTF{

     @Override
     public void close() throws HiveException {
         // TODO Auto-generated method stub    
     }

     @Override
     public StructObjectInspector initialize(ObjectInspector[] args)
             throws UDFArgumentException {
         if (args.length != 1) {
             throw new UDFArgumentLengthException("ExplodeMap takes only one argument");
         }
         if (args[0].getCategory() != ObjectInspector.Category.PRIMITIVE) {
             throw new UDFArgumentException("ExplodeMap takes string as a parameter");
         }

         ArrayList<String> fieldNames = new ArrayList<String>();
         ArrayList<ObjectInspector> fieldOIs = new ArrayList<ObjectInspector>();
         fieldNames.add("col1");
         fieldOIs.add(PrimitiveObjectInspectorFactory.javaStringObjectInspector);
         fieldNames.add("col2");
         fieldOIs.add(PrimitiveObjectInspectorFactory.javaStringObjectInspector);

         return ObjectInspectorFactory.getStandardStructObjectInspector(fieldNames,fieldOIs);
     }

     @Override
     public void process(Object[] args) throws HiveException {
         String input = args[0].toString();
         String[] test = input.split(";");
         for(int i=0; i<test.length; i++) {
             try {
                 String[] result = test[i].split(":");
                 forward(result);
             } catch (Exception e) {
                 continue;
             }
         }
     }
 }
使用示例:

1.select explode_map(properties) as (col1,col2) from src;
2.select src.id, tmpTable.col1, tmpTable.col2 from src lateral view explode_map(properties) tmpTable as col1, col2;

参考资料

1.https://www.cnblogs.com/ggjucheng/archive/2013/02/01/2888819.html
2.https://blog.csdn.net/u012485099/article/details/80790908
3.https://blog.csdn.net/classics_moon/article/details/85056378
4.https://blog.csdn.net/wypersist/article/details/80314352
5.https://www.cnblogs.com/ggjucheng/archive/2013/02/01/2888051.html

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值