Hive 创建自定义UDF函数

一、为什么需要UDF函数

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

二、UDF函数的分类

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

三、创建UDF函数

1.导入依赖

<dependencies>
		<!-- https://mvnrepository.com/artifact/org.apache.hive/hive-exec -->
		<dependency>
			<groupId>org.apache.hive</groupId>
			<artifactId>hive-exec</artifactId>
			<version>1.2.1</version>
		</dependency>
</dependencies>

2.创建类并继承org.apache.hadoop.hive.ql.UDF

import org.apache.hadoop.hive.ql.exec.UDF;
/**
 * 转小写
 */
public class Lower extends UDF{
    public String evaluate (final String s) {
        if (s == null) {
            return null;
        }
        return s.toLowerCase();
    }
}

3.将项目打包成jar上传至服务器
在这里插入图片描述
4.在hive中添加jar资源

hive> add jar /getProduct_udf-1.0-SNAPSHOT.jar;

5.创建临时函数与jar中的udf函数类关联

hive> create temporary function mylower as "com.hive.udf.Lower";

6.调用自定义udf函数

hive> select mylower('HELLO WORLD');
OK
hello world
Time taken: 0.048 seconds, Fetched: 1 row(s)

注:
1.创建临时函数仅对当前session(黑窗口)有效

hive> create temporary function sayhello as 'com.ruozedata.bigdata.HelloUDF';

2.创建永久函数全局有效

hive> CREATE FUNCTION sayhello AS 'com.ruozedata.bigdata.HelloUDF' USING JAR 'hdfs://hadoop002:9000/lib/hive-1.0-SNAPSHOT.jar'; 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值