hive中复杂的数据类型array与map

1.array

//创建表

create table if not exists arr2(
name string,
score array<String>
)
row format delimited fields terminated by '\t'     //字段之间的分隔符
collection items terminated by ','          //array之间的分隔符
;

//数据
zhangsan	78,89,92,96
lisi 	67,75,83,94

//加载数据
load data local inpath '/root/test/arraydata' into arr2;

//查询数据
select name,score[1] from arr2 where size(score) > 3;

arr

数组的某个值查询直接字段名[index]

//将数组的数据拆分开并插入到arr_temp表
create table arr_temp
as
select name,cj from arr2 lateral view explode(score) score as cj;
拆分后的结果为:

arr2

//统计某个学生的总成绩
select name,sum(cj) as totalscore 
from arr2 lateral view explode(score) score as cj group by name;

arr3

//将拆分的数据合并并写到arr_temp2表
create table arr_temp2
as
select name,collect_set(cj) from arr_temp group by name;
合并后的结果为:

arr4

2.map

//创建表
create table if not exists map2(
name string,
score map<string,int>
)
row format delimited fields terminated by ' '
collection items terminated by ','
map keys terminated by ':'
;

//数据
zhangsan chinese:90,math:87,english:63,nature:76
lisi chinese:60,math:30,english:78,nature:0
wangwu chinese:89,math:25,english:81,nature:9

load data local inpath '/root/test/mapdata' into map2;

//查询
查询数学大于35分的学生的英语和自然成绩:
select 
m.name,
m.score['english'] ,
m.score['nature']
from map2 m
where m.score['math'] > 35
;

map2

map的某个值查询使用字段名[key]

//拆分数据
select explode(score) as (m_class,m_score) from map2;

map3

//拆分数据并插入到map2_temp表中
create table map2_temp
as
select name,m_class,m_score from map2 
lateral view explode(score) score as m_class,m_score;

结果为:

map4

//合并拆分的数据
select name,collect_set(concat_ws(":",m_class,cast(m_score as string)))
 from map2_temp group by name;

map5

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值