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 
相关推荐
自由能燃气设备2 天前
燃气容积式热水器厂家选购指南:2026年商用热水设备采购避坑手册
大数据·数据库·数据仓库·人工智能
哥本哈士奇2 天前
dbt+SQLServer构建数据仓库(8):Vibe Coding用dbt构建data vault数仓
数据仓库·sqlserver
Blossom i2 天前
Python+spark2.0+Hadoop机器学习与大数据实战第十一章:Python Spark的集成开发环境
大数据·hadoop·spark
我要用代码向我喜欢的女孩表白3 天前
hdfs获取所有路径大小的脚本
大数据·hadoop·hdfs
ha_lydms3 天前
使用DataWorks导入 Maxcompute 数据
大数据·数据仓库·dataworks·maxcompute·hologres·odps·数据同步
ZCBUS实时计算3 天前
金融证券实时数仓建设实践:轻量化实时计算平台落地,实现交易数据端到端秒级处理
大数据·数据库·数据仓库·金融·flink·dba·etl
fastjson_4 天前
Hadoop之Yarn
大数据·hadoop·npm
爱看报的猿4 天前
【金仓数据库征文】MySQL至金仓KES异构数据库平滑迁移与性能深度调优实战
数据库·数据仓库·mysql·金仓数据库征文
fastjson_4 天前
Hive的介绍和使用
数据仓库·hive·hadoop
光电的一只菜鸡5 天前
小白从零开始学agent(二)——如何高效编写 Prompt 与 Skill
hadoop·microsoft·prompt