您的位置:首页 > 其它

Hive数据类型-集合类型(Array-Map-Struct)的尝试

2016-04-01 13:41 453 查看
Hive支持的数据类型分为基础数据类型和集合类型。

基础类型主要包括:tinyint,smalint,int,bigint,boolean,float,double,string,timestamp,ninary等。这些基础类型和其他关系型数据库中的基础数据类型差不多。

集合类型主要包括:array,map,struct等,hive的特性支持集合类型,这特性是关系型数据库所不支持的,利用好集合类型可以有效提升SQL的查询速率。

由于官方文件的实例中没有集合类型的例子,所以就自己尝试了。下面是3种集合类型的简单实现。

ARRAY类型的实现

先建一张表,建表脚本如下:
create table t_person(
id int,
name string,
likes array<string>
)
row format delimited
fields terminated by ','
collection items terminated by '_';
新建一个文本文件,格式如下:【这是根据建表时的规格,准备导入表的数据格式】
1,王力宏,唱歌_钢琴_二胡_作曲_演戏_导演_书法

执行导入数据的命令,然后再查询该表就可以看到数据了。
load data local inpath 'Documents/hive/t_person.txt' into table t_person;
查询一下试试看:【array的访问元素和java中是一样的,这里通过索引来访问】
select name,likes[1] as likes from t_person;

MAP类型的实现

先建一张表,建表脚本如下:

create table t_person(
id int,
name string,
tedia map<string,string>
)
row format delimited
fields terminated by ','
collection items terminated by '_'
map keys terminated by ':';
新建一个文本文件,格式如下:【这是根据建表时的规格,准备导入表的数据格式】
1,王力宏,性别:男_形象:非常健康
执行导入数据的命令,然后再查询该表就可以看到数据了。
load data local inpath 'Documents/hive/t_person.txt' into table t_person;
查询一下试试看:【map访问元素的方式是通过key】
select name,tedia['<span style="font-family: Arial, Helvetica, sans-serif;">性别</span><span style="font-family: Arial, Helvetica, sans-serif;">'] as xb from t_person;</span>

STRUCT类型的实现

先建一张表,建表脚本如下:
create table t_person(
id int,
name string,
address struct<city:string,street:string>
)
row format delimited
fields terminated by ','
collection items terminated by '_';

新建一个文本文件,格式如下:【这是根据建表时的规格,准备导入表的数据格式】
1,王力宏,台湾省_台北市

执行导入数据的命令,然后再查询该表就可以看到数据了。
load data local inpath 'Documents/hive/t_person.txt' into table t_person;
查询一下试试看:【struct访问元素的方式是通过.符号】

select name,address.city as city<span style="font-family: Arial, Helvetica, sans-serif;"> from t_person;</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: