传送门
hadoop入门系列--hive基础
hadoop入门系列--hive的三种集合数据类型array、map、struct以及自定义分隔符示例
hadoop入门系列--hive中array(或map集合类型)的行转多列LATERAL VIEW explode用法
传送门
前言
Hive的数据类型主要有int、string、boolean、array、map、struct等,在这只描述array,map,struct三种。
一、array(等同于数组,可以使用下标来操作相应的元素)
示例数据:
tom 80,90,100
harry 90,100,89
sula 90,80,95
//在hive中创建表
create table score_1 (name string,score array<string>)
row format delimited
fields terminated by '\t' //设置列分隔符
collection items terminated by ',' //设置集合元素分割
;
//把数据导入hive表
load data local inpath '/home/hadoop/Downloads/data_1.txt' into table score_1
//开始查询
select * from score_1;
select no,score[0] from score2; //map和array不能用点,得用[]
二、map(等同于hashmap的操作)
- map,要把key写入数据源里,会占空间
- 不过,从数据就可以直接看出这个是代表什么意思
示例数据(注意,这里数学这个数据的位置改变的完全不影响的!):
1 数学:80,语文:89,英语:95
2 语文:70,数学:98,英语:89
//在hive中创建表
create table score (id int,score map<string,float>)
row format delimited
fields terminated by '\t' //设置列分隔符
collection items terminated by ',' //设置集合元素分割
map keys terminated by ':' //设置map:key-value分割
;
//把数据导入hive表
load data local inpath '/home/hadoop/Downloads/data.txt' into table score
//开始查询
select * from score;
select id,score["数学"] from score;//使用key、value的形式
三、struct
- 有点像java中的对象,c语言中的struct,可以容纳多种数据类型的数据。
示例数据:
80,90,100
90,100,89
90,80,95
//在hive中创建表
create table score2 (no string,struct<chinese:float,math:float,english:float>)
row format delimited
fields terminated by '\t' //设置列分隔符
collection items terminated by ',' //设置集合元素分割
;
//把数据导入hive表
load data local inpath '/home/hadoop/Downloads/data2.txt' into table score2
//开始查询
select * from score2;
select no,score.math from score2; //map和array不能用点,得用[]
四、自定义分隔符
row format delimited
fields terminated by ... 列分隔符
collection items terminated by ... 集合元素分割
key-value分割 map keys terminated by ... map
lines terminated by ...(一般不写) 行分割
网友评论