【博学谷学习记录】超强总结,用心分享|Hive表生成函数

explode函数

explode(列): 将Hive表的某一列复杂类型的array或者map结构拆分成多行
explode(array): 数组中的每个元素生成一行
explode(map): map中每个键值对生成一行,key一列,value一列
注意: struct类不支持

Array类型数据演示

演示数据

zhangsan beijing,shanghai,tianjin,hangzhou
lisi xiamen,fuzhou,jiangxi,nanchang
wangwu changchun,chengdu,wuhan,beijing

创建一张包含array类型的表

create table work_locations(
  name string,
  citys array<string>
)
	row format delimited
	fields terminated by ' '
	collection items terminated by ',';

插入表数据

load data local inpath '/export/testdatas/emp.txt' into table emp;

查询验证

select * from emp;

image.png

使用explode查询

select explode (names) as name from emp;

image.png

Map 类型数据演示

演示数据

1,zhangsan,father:xiaoming#mother:xiaohuang#brother:xiaoxu,28
2,lisi,father:mayun#mother:huangyi#brother:guanyu,22
3,wangwu,father:wangjianlin#mother:ruhua#sister:jingtian,29
4,mayun,father:mayongzhen#mother:angelababy,26

创建一张包含map类型的表

create table family (
    id      int,
    name    string,
    members map<string,string>,
    age     int
)
	row format delimited
	fields terminated by ','
	collection items terminated by '#'
	map keys terminated by ':';
	

加载表数据

load data local inpath '/export/testdatas/map.txt' into table family;

查询验证

select * from family;

image.png

使用explode查询

select explode(members) from family;

image.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值