hive的三种复合数据类型array、map、struct以及自定义分割符示例

本文介绍了Hive中的三种复合数据类型——array、map、struct,详细说明了如何使用它们,并提供了自定义分隔符的示例。在array中,讨论了如何通过下标访问元素;在map中,展示了类似HashMap的操作方式;对于struct,提到了它类似Java对象或C语言struct的特点,用于容纳多种数据类型。文章还详细解释了如何设置和使用不同的分隔符来处理这些复合数据类型。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Hive的数据类型主要有int、boolean、date、array、map、struct等,在这只描述array,map,struct三种。
1.array(等同于数组,可以使用下标来操作相应的元素)

默认分割符下的array,示例如下:
有一群学生,id,name,hobby(多个)
create table t3_arr(
id int,
name string,
hobyy array
) row format delimited
 fields terminated by '\t';
hive> load data local inpath 'data/t1' into table t3_arr;
查看数组中的某一个元素
hive> select id, hobyy[1] from t3_arr;
设置数组中的分隔符
它的默认分隔符为\002,使用ctrl+v ctrl+b可以输入。

自定义分割符,示例如下:
create table t3_arr_1(
id int,
name string,
hobyy array
) row format delimited
 fields terminated by '\t'
 collection items terminted by ',';
使用','作为array中的分割符,在导入数据的文件中,array的元素以','分开的。
例如:
1  张三   唱歌,游泳,看书
加载数据:
hive> load data local inpath 'data/t2' into table t3_arr_1;
查询数据:
hive> select id, name, hobby[0],hobby[1] from t3_arr_1;

2.map(等同于hashmap的操作)
map也是有默认的分隔符的,它的分隔符是\003,对应的输入为ctrl+v ctrl+c,等同array
自定义分割符:
有一群学生,id,name,scores(chinese,math,english)
create table t4_map (
id int,
name string,
scores map
) row format delimited
fields terminated by '\t'
collection items terminated by ','
map keys terminated by ':';
使用','作为map元素的分割符,对于key和value间的分割符使用:,在导入数据的文件中数据示例如下:
1 张三 Chinese:90,english:80,math:70
加载数据:
load data local inpath 'data/t4_map' into table t4_map;
查询map中的特定的值:
hive> select id, name, scores["chinese"] from t4_map;
3.struct
有点想咱们的java中的对象,c语言中的struct,可以容纳多种数据类的数据
create table t5_struct(
id int,
name string,
info struct
) row format delimited
fields terminated by '\t'
collection items terminated by ',';

自定义使用','作为struct的分隔符,在导入数据的文件中,struct元素用','分割,数据示例如下:
1 张三 address,39

查看其中的struct中某一列数据
hive> select id, name, info.subway from t5_struct;

自定义分隔符
row format delimited
列分隔符:fields terminated by ...
集合元素分割 collection items terminated by ...
map:key-value分割 map keys terminated by ...
行分割 lines terminated by ...(一般不写)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值