23-大数据-hive复杂数据类型

1、array
一组有序字段,字段的类型必须相同,array(1,2)
数组是一组具有相同类型和名称的变量的集合,这些变量称为数组的元素,每个数组的元素都有一个编号,编号从0开始
create table t1(id int,name string,likes array) row format delimited fields terminated by ‘,’ collection items terminated by ‘-’ --集合项用’-’ 分割 lines terminated by ‘\n’;
数据:
1,张三,足球-读书
2,李四,足球-电影-汽车
查看第一个爱好:select likes[0] from t1;
查看第一个爱好为足球的:select * from t1 where likes[0]=‘足球’;
查看用户最喜欢哪个爱好,直接count是不可用的
hive提供了两个函数:
explode将数组拆分成行,lateral view来实现聚合 select hobby,count(1) c1 from t1 lateral view explode(likes) likes as hobby group by hobby order by c1 desc;

2、map
array
一组无序的键值对,键的类型必须是原子的,值可以是任何类型。同一个映射的键的类型必须相同,值的类型必须相同 map(‘a’,1,‘b’,2)
map是一组键值对元组集合,使用数组表示,可访问元素
create table test_map(id int,name string,deductons map<string,string>) row format delimited fields terminated by ‘,’ collection items terminated by ‘-’ map keys terminated by ‘;’ lines terminated by ‘\n’;
数据:
1,里奥,英语:四级-日语:三级
2,马力斯,英语:六级-日语:一级
查看英语为四级的:select * from test_map where deductons[‘英语’]=‘四级’;
查询日语:select deductons[‘日语’] from test_map;
select explode(deductons) as (note_name,note_level) from test_map;
select id,name,note_name,note_level from test_map lateral view outer explode(deductons) deductons as note_name,note_level;

3、struct
一组命名的字段,字段的类型可以不同。一种记录类型,封装了一个命名的字段集合
通过’点’符号访问元素内容
针对一组数据中有不同的数据类型,升级版map
strunct一旦声明好结果,其位置就不可以改变
create table test_struct(id int ,address structcity:string,province:string,area:string,code:string) row format delimited fields terminated by ‘,’ collection items terminated by ‘-’ map keys terminated by ‘:’ lines terminated by ‘\n’;
数据:
1,大连-辽宁-东北部-116000
2,青岛-山东-东部-211000
select * from test_struct where address.city=‘大连’;

4、union
值的数据类型可以是多个被定义的数据类型中的任意一个,这个值通过一个整数(零索引)来标记其为联合类型中的那个数据类型

复杂数据类型允许任意层次的嵌套。复杂数据类型声明必须用尖括号指明其中数据类型的字段类型

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值