傳送門
hadoop入門系列--hive基礎(chǔ)
hadoop入門系列--hive的三種集合數(shù)據(jù)類型array施绎、map、struct以及自定義分隔符示例
hadoop入門系列--hive中array(或map集合類型)的行轉(zhuǎn)多列LATERAL VIEW explode用法
傳送門
前言
Hive的數(shù)據(jù)類型主要有int贞绳、string谷醉、boolean、array冈闭、map俱尼、struct等,在這只描述array萎攒,map遇八,struct三種。
一耍休、array(等同于數(shù)組刃永,可以使用下標(biāo)來(lái)操作相應(yīng)的元素)
示例數(shù)據(jù):
tom 80,90羊精,100
harry 90斯够,100,89
sula 90,80读规,95
//在hive中創(chuàng)建表
create table score_1 (name string,score array<string>)
row format delimited
fields terminated by '\t' //設(shè)置列分隔符
collection items terminated by ',' //設(shè)置集合元素分割
;
//把數(shù)據(jù)導(dǎo)入hive表
load data local inpath '/home/hadoop/Downloads/data_1.txt' into table score_1
//開(kāi)始查詢
select * from score_1;
select no,score[0] from score2; //map和array不能用點(diǎn)抓督,得用[]
二、map(等同于hashmap的操作)
- map束亏,要把key寫入數(shù)據(jù)源里铃在,會(huì)占空間
- 不過(guò),從數(shù)據(jù)就可以直接看出這個(gè)是代表什么意思
示例數(shù)據(jù)(注意碍遍,這里數(shù)學(xué)這個(gè)數(shù)據(jù)的位置改變的完全不影響的定铜!):
1 數(shù)學(xué):80,語(yǔ)文:89怕敬,英語(yǔ):95
2 語(yǔ)文:70宿稀,數(shù)學(xué):98,英語(yǔ):89
//在hive中創(chuàng)建表
create table score (id int,score map<string,float>)
row format delimited
fields terminated by '\t' //設(shè)置列分隔符
collection items terminated by ',' //設(shè)置集合元素分割
map keys terminated by ':' //設(shè)置map:key-value分割
;
//把數(shù)據(jù)導(dǎo)入hive表
load data local inpath '/home/hadoop/Downloads/data.txt' into table score
//開(kāi)始查詢
select * from score;
select id,score["數(shù)學(xué)"] from score;//使用key赖捌、value的形式
三、struct
- 有點(diǎn)像java中的對(duì)象矮烹,c語(yǔ)言中的struct越庇,可以容納多種數(shù)據(jù)類型的數(shù)據(jù)。
示例數(shù)據(jù):
80奉狈,90卤唉,100
90,100仁期,89
90桑驱,80,95
//在hive中創(chuàng)建表
create table score2 (no string,struct<chinese:float,math:float,english:float>)
row format delimited
fields terminated by '\t' //設(shè)置列分隔符
collection items terminated by ',' //設(shè)置集合元素分割
;
//把數(shù)據(jù)導(dǎo)入hive表
load data local inpath '/home/hadoop/Downloads/data2.txt' into table score2
//開(kāi)始查詢
select * from score2;
select no,score.math from score2; //map和array不能用點(diǎn)跛蛋,得用[]
四熬的、自定義分隔符
row format delimited
fields terminated by ... 列分隔符
collection items terminated by ... 集合元素分割
key-value分割 map keys terminated by ... map
lines terminated by ...(一般不寫) 行分割