hive 用户自定义函数udf,udaf,udtf

udf:一对一的关系

udtf:一对多的关系

udaf:多对一的关系

使用Java实现步骤

自定义编写UDF函数注意:

1.需要继承org.apache.hadoop.hive.ql.exec.UDF

2.需要实现evaluete函数

编写UDTF函数注意:

1.需要继承org.apache.hadoop.hive.ql.udf.generic.GenericUDTF

2.实现 initialize, process, close三个方法

1.自定义实现一个大小写转换的函数(UDF)

执行mvn命令

将jar包上传到服务器上

上传之后,进入hive,添加jar包

add jar // /xxx.jar(jar包全路径)

创建临时函数

create temporary function upper_func as 'org.example.Uppercase';

之后,可以直接在查询中使用

transform方式

hive中除了使用Java编写udf,还可以使用transform,支持多种语言

例如: 将表第一列与第二列用 _ (下划线) 连接

1.Linux中的 awk

创建一个transform.awk

内容

bash 复制代码
{
	print $1"_"$2
}

在hive中使用add file 添加 transform.awk

然后就可以调用函数了

select transform(col1,col2) using "awk -f transform.awk" as (uu) from test_table limit 10;

2.使用python

在hive中使用 add file 添加 transform.py

使用命令调用函数

select transform(col1,col2) using "python transform.py" as (uu) from test_table limit 10;

3.基于python实现wordcount

整个过程模拟map 和 reduce

add file 上传 mapper.pyreduce.py

创建一张表,保存结果

bash 复制代码
create table word_cnt(
word string , 
cnt int)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '\t';
bash 复制代码
WITH map_cnt as  (
select transform(line) using "python mapper.py" as  word , cnt 
from docs  
cluster by word ),

insert  overwrite table word_cnt 
select transform(word,cnt) using "python reduce.py" as w, c  
from map_cnt 
相关推荐
牛奶咖啡132 天前
大数据Hadoop运维应用实践——HIVE与Hadoop实现整合_Hive的配置安装与使用
大数据·hive·hive的配置与安装·metastore服务的配置·hiveserver2服务配置·hive的常用sql操作·beeline的使用
Zhu7583 天前
在k8s集群环境部署高可用的Apache Hadoop3.1.1定制版集群
hadoop·kubernetes
Y3815326623 天前
3 种 SERP 数据 ETL 方案对比:实时 / 定时 / 流式
数据仓库·etl
还有你Y4 天前
软件工程架构
hadoop·架构·软件工程
向夏威夷 梦断明暄4 天前
基于Tez引擎的 Hive SQL 性能优化
hive·sql·性能优化
这个DBA有点耶4 天前
交易型数据库是什么?OLTP核心能力与2026选型指南
数据库·数据仓库·sql·database·数据库架构·olap·dba
RestCloud5 天前
Informatica迁移国产ETL完整实施指南:ETLCloud自动化平滑替换方案
数据仓库·etl·etlcloud·数据传输·数据集成工具·informatica·国产化替代
德昂信息dataondemand5 天前
全量还是增量?聊聊数仓ETL中的数据同步策略
数据仓库·etl
观远数据7 天前
数据集成平台选型战卡:DataFlow对比传统ETL的5个维度与红线排除项
数据仓库·etl