hive自定义函数

一、概述

当Hive提供的内置函数无法满足你的业务处理需要时,此时就可以考虑使用用户自定义函数(UDF:user-defined function)

二、自定义函数

  • UDF(User-Defined-Function):一进一出
  • UDAF(User-Defined Aggregation Function):聚集函数,多进一出,类似于:count/max/min
  • UDTF(User-Defined Table-Generating Functions):一进多出,如lateral view explode()

demo:

  • 创建一个Maven工程Hive
  • 导入依赖
<dependencies>
		<dependency>
			<groupId>org.apache.hive</groupId>
			<artifactId>hive-exec</artifactId>
			<version>3.1.2</version>
		</dependency>
</dependencies>
  • 创建一个类
package com.hive.udf;

import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
import org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException;
import org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDF;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;

/**
 * 自定义UDF函数,需要继承GenericUDF类
 *  需求: 计算指定字符串的长度
 * */
public class MyStringLength extends GenericUDF {

    /**
     * 初始化方法
     * 参数:传入到函数参数对应类型的鉴别器对象
     * 返回值:指定函数的返回值 函数参数对应类型的鉴别器对象
     *
     */
    public ObjectInspector initialize(ObjectInspector[] objectInspectors) throws UDFArgumentException {
        // 1.判断参数传入的个数(空值 null判断 长度判断)
        if (objectInspectors.length==0 || objectInspectors.length !=1){
            throw new UDFArgumentLengthException("函数参数格式不正确");
        }

        //2. 校验函数的类型
        if(!objectInspectors[0].getCategory().equals(ObjectInspector.Category.PRIMITIVE)){
            throw new UDFArgumentTypeException(0,"函数参数类型不正确");
        }

        // 3.返回参数类型

        return PrimitiveObjectInspectorFactory.javaIntObjectInspector;
    }

    /**
     * 参数:传入函数的参数
     *
     *
     * */
    public Object evaluate(DeferredObject[] deferredObjects) throws HiveException {
        //1.获取参数
        Object arg = deferredObjects[0].get();
        if (arg == null || arg == ""){
            return 0;
        }else {
            //2.返回参数长度
            return deferredObjects.toString().length();
        }
    }


    public String getDisplayString(String[] strings) {
        return "";
    }
}

  • 打成jar包上传到服务器/opt/module/hive/datas/myudf.jar
[atdyh@hadoop102 datas]$ pwd
/opt/module/hive-3.1.2/datas
[atdyh@hadoop102 datas]$ ll
总用量 84
-rw-r--r--. 1 atdyh atdyh 2922 7月  16 17:46 myudf.jar

  • 将jar包添加到hive的classpath
hive (dyhtest)> add jar /opt/module/hive-3.1.2/datas/myudf.jar;
Added [/opt/module/hive-3.1.2/datas/myudf.jar] to class path
Added resources: [/opt/module/hive-3.1.2/datas/myudf.jar]

  • 创建临时函数与开发好的java class关联
    语法:create temporary function udf名字 as “udf类的全限定名”;
hive (dyhtest)>  create temporary function my_len as "com.hive.udf.MyStringLength";
OK
Time taken: 0.046 seconds

  • 在hql中使用自定义的函数
hive (dyhtest)>  select ename,my_len(ename) ename_len from emp;
OK
ename	ename_len
CLERK	75
ALLEN	75
WARD	75
JONES	75
MARTIN	75
BLAKE	75
CLARK	75
SCOTT	75
KING	75
TURNER	75
ADAMS	75
JAMES	75
FORD	75
MILLER	75
Time taken: 2.688 seconds, Fetched: 14 row(s)

其他两个基本没用到,后续如果常用的话,博主在更新本文!!!有需要的铁铁,可私信我更新

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值