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

来源:互联网 发布:mac怎么局部截图 编辑:程序博客网 时间:2024/06/04 19:32

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 delimitedfields 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 delimitedfields 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 delimitedfields 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>


0 0
原创粉丝点击