hive非基本类型(array、map、struct)

hive非基本类型

array

数据:
gaoyuanyuan0 87,98,97,60
gaoyuanyuan1 88,98,94,60
gaoyuanyuan2 89,98,97,60,100

建表语句

create table if not exists arr2(
name string,
score array<string>
)row format delimited
fields terminated by "\t"
collection items terminated by ","
;

load数据

load data inpath "/user/hdfs/arr.txt" i
nto table arr2;

普通查询

select name,
score[0] ,
score[1],
score[2]
 from arr2;

在这里插入图片描述
不定长全部查询

1.explode展开

--explode展开:
select explode(score)
 from arr2;

在这里插入图片描述
2.lateral view

--lateral view:
select name,mscore
from arr2 
lateral view explode(score) sc
 as mscore;

在这里插入图片描述
3.把查询结果放入表中

create table if not exists arr3
as
select name,mscore
from arr2
 lateral view explode(score) sc 
 as mscore;

在这里插入图片描述
collect_list:

create table if not exists aa as
select name,
concat_ws(",",collect_list(mscore))
from arr3
group by name;

在这里插入图片描述

map

数据
marong lazhu:2,pibian:1,huanghong:2
songjingjing lazhu:3,pibian:3,huanghong:4
songwei lazhu:3,pibian:3,shoukao:3,huanghong:4
创表语句

create table if not exists map1(
name string,
item map<string,int>)row format delimited
fields terminated by "\t"
collection items terminated by ","
map keys terminated by ":";

load数据

load data local inpath "/home/hdfs/map1.txt" 
into table map1;

在这里插入图片描述

查找买了shoukao的用户的pibian的购买量

select name,
item["pibian"]
from map1
where item["shoukao"] is not null;

在这里插入图片描述
求每一个商品的销售的数量

select explode(item) from map1;

在这里插入图片描述
leteral view :

select name,itemname,num
from map1 lateral view explode(item) it 
as itemname,num;

在这里插入图片描述

select itemname,sum(num)
from (
select name,itemname,num
from map1 lateral view explode(item) it as itemname,num
) a
group by itemname;

在这里插入图片描述
数据写入map:

  • str_to_map:(str,deliminated1,deliminated2)
  • map:(k1,m1,k2,m2…)
create table if not exists map_tmp
as
select name,itemname,num
from map1 lateral view explode(item) it as itemname,num;

select name,concat(itemname,":",num)
from  map_tmp; 


insert into map2
select name,str_to_map(
concat_ws(",",
    collect_list(
      concat(itemname,":",num)
      )
    )
)
from  map_tmp
group by name; 

在这里插入图片描述
在这里插入图片描述

struct

数据
cls japan,dongjing,xichuanxi18
wangxuejia japan,dongjing,sss123
建表语句

create table if not exists struct1(
name string,
addr struct<province:string,city:string,street:string>)
row format delimited
fields terminated by "\t"
collection items terminated by ","
;

load data

load data local inpath "/home/hdfs/struct.txt" 
into table struct1;

查询城市在dongjing的用户的省份

select 
name,addr.province,addr.street
from struct1
where addr.city="dongjing";
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值