1.udtf介绍及编写
1.1.介绍
HIVE中udtf可以将一行转成一行多列,也可以将一行转成多行多列,使用频率较高。本篇文章通过实际案例剖析udtf的编写及使用方法和原理。
测试数据
drop table if exists test;
create table test
(
ind int,
col string,
col1 string
) ;
insert into test values (1,'a,b,c','1,2');
insert into test values (2,'j,k',null);
insert into test values (3,null,null) ;
对第一行需要输出如下结果:
Ind | Key | Value |
---|---|---|
1 | a | 1 |
1 | b | 2 |
1 | c | Null |
其它行都要输出类似数据,如果输入数据为null,则没输出。
1.2udtf编写
编写UDTF(User-Defined Table-Generating Functions),需要继承GenericUDTF类,类中部分代码如下:
/**
* A Generic User-defined Table Generating Function (UDTF)
*
* Generates a variable number of o