(八)hive复杂数据类型array map struct

本文详细介绍了Hive中的三种复杂数据类型:Array、Map和Struct的使用方法。包括如何创建包含这些复杂类型的表,加载数据,以及使用特定函数如array_contains、map_keys和map_values进行数据检索和处理。

1.array

create table hive_array(

id string,

work_locations array<string>

)

ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'

COLLECTION ITEMS TERMINATED BY ',';

load data local inpath '/root/hive_array.txt'

overwrite into table hive_array;

1 ["a","b","c","l"]

2 ["e","f","g"]

3 ["u","ip","i"]

4 ["w","g","m"]

5 ["t","g","j"]

6 ["a","u","j"]

 

array_contains函数:判断数组是否包含指定元素

select * from hive_array where array_contains(work_locations,'a');

1 ["a","b","c","l"]

6 ["a","u","j"]

2.map : key-value

father:xiaoming#mother:xiaohuang#brother:xiaoxu

create table hive_map(

id string,

str map<string,string>

)

ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'

COLLECTION ITEMS TERMINATED BY '#'

MAP KEYS TERMINATED BY ':';

load data local inpath '/root/hive_map.txt'

overwrite into table hive_map;

 

map_keys、map_values:返回key和value的数组,可以和array的操作函数配合使用

hive (test_hive)> select * from hive_map;

OK

1 {"a":"aa","b":"bb","c":"cc"}

2 {"d":"aa","e":"ee","f":"ff"}

3 {"a":"hh","s":"ss"}

返回key =a 的值

select id , str['a'] from hive_map;

1 aa

2 NULL

3 hh

返回map搜有的key

select id , map_keys(str) from hive_map;

返回map所有的values

select id , map_values(str) from hive_map;

 

3.struct可以自定义复杂数据类型

create table hive_struct(

id string,

userinfo struct<name:string,age:int>

)

ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'

COLLECTION ITEMS TERMINATED BY ':';

load data local inpath '/root/hive_struct.txt'

overwrite into table hive_struct;

 

hive (test_hive)> select * from hive_struct;

OK

1 {"name":"张三","age":18}

2 {"name":"李四","age":30}

3 {"name":"王五","age":44}

取值

select id,userinfo.name,userinfo.age from hive_struct;

1 张三 18

2 李四 30

3 王五 44

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值