您好,登錄后才能下訂單哦!
這篇文章給大家介紹Hive中有哪些集合數(shù)據(jù)類型,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
除了使用礎(chǔ)的數(shù)據(jù)類型string等,Hive中的列支持使用struct, map, array集合數(shù)據(jù)類型。
數(shù)據(jù)類型 | 描述 | 語(yǔ)法示例 |
---|---|---|
STRUCT | 和C語(yǔ)言中的struct或者"對(duì)象"類似,都可以通過(guò)"點(diǎn)"符號(hào)訪問(wèn)元素內(nèi)容。 | struct{'John', 'Doe'} |
MAP | MAP是一組鍵-值對(duì)元素集合,使用key可以訪問(wèn)元素。 | map('fisrt', 'John', 'last', 'Doe') |
ARRAY | 數(shù)組是一組具有相同數(shù)據(jù)類型和名稱的變量的集合。 | Array('John', 'Doe') |
1. Array的使用
創(chuàng)建數(shù)據(jù)庫(kù)表,以array作為數(shù)據(jù)類型
create table person(name string,work_locations array<string>) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' COLLECTION ITEMS TERMINATED BY ',';
數(shù)據(jù)
biansutao beijing,shanghai,tianjin,hangzhou linan changchu,chengdu,wuhan
入庫(kù)數(shù)據(jù)
LOAD DATA LOCAL INPATH '/home/hadoop/person.txt' OVERWRITE INTO TABLE person;
查詢
hive> select * from person; biansutao ["beijing","shanghai","tianjin","hangzhou"] linan ["changchu","chengdu","wuhan"] Time taken: 0.355 seconds hive> select name from person; linan biansutao Time taken: 12.397 seconds hive> select work_locations[0] from person; changchu beijing Time taken: 13.214 seconds hive> select work_locations from person; ["changchu","chengdu","wuhan"] ["beijing","shanghai","tianjin","hangzhou"] Time taken: 13.755 seconds hive> select work_locations[3] from person; NULL hangzhou Time taken: 12.722 seconds hive> select work_locations[4] from person; NULL NULL Time taken: 15.958 seconds
2. Map 的使用
創(chuàng)建數(shù)據(jù)庫(kù)表
create table score(name string, score map<string,int>) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' COLLECTION ITEMS TERMINATED BY ',' MAP KEYS TERMINATED BY ':';
要入庫(kù)的數(shù)據(jù)
biansutao '數(shù)學(xué)':80,'語(yǔ)文':89,'英語(yǔ)':95 jobs '語(yǔ)文':60,'數(shù)學(xué)':80,'英語(yǔ)':99
入庫(kù)數(shù)據(jù)
LOAD DATA LOCAL INPATH '/home/hadoop/score.txt' OVERWRITE INTO TABLE score;
查詢
hive> select * from score; biansutao {"數(shù)學(xué)":80,"語(yǔ)文":89,"英語(yǔ)":95} jobs {"語(yǔ)文":60,"數(shù)學(xué)":80,"英語(yǔ)":99} Time taken: 0.665 seconds hive> select name from score; jobs biansutao Time taken: 19.778 seconds hive> select t.score from score t; {"語(yǔ)文":60,"數(shù)學(xué)":80,"英語(yǔ)":99} {"數(shù)學(xué)":80,"語(yǔ)文":89,"英語(yǔ)":95} Time taken: 19.353 seconds hive> select t.score['語(yǔ)文'] from score t; 60 89 Time taken: 13.054 seconds hive> select t.score['英語(yǔ)'] from score t; 99 95 Time taken: 13.769 seconds
修改map字段的分隔符
Storage Desc Params: colelction.delim ## field.delim \t mapkey.delim = serialization.format \t
可以通過(guò)desc formatted tableName查看表的屬性。
hive-2.1.1中,可以看出colelction.delim,這里是colelction而不是collection,hive里面這個(gè)單詞寫錯(cuò)了,所以還是要按照錯(cuò)誤的來(lái)。
alter table t8 set serdepropertyes('colelction.delim'=',');
3. Struct 的使用
創(chuàng)建數(shù)據(jù)表
CREATE TABLE test(id int,course struct<course:string,score:int>) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' COLLECTION ITEMS TERMINATED BY ',';
數(shù)據(jù)
1 english,80 2 math,89 3 chinese,95
入庫(kù)
LOAD DATA LOCAL INPATH '/home/hadoop/test.txt' OVERWRITE INTO TABLE test;
查詢
hive> select * from test; OK 1 {"course":"english","score":80} 2 {"course":"math","score":89} 3 {"course":"chinese","score":95} Time taken: 0.275 seconds hive> select course from test; {"course":"english","score":80} {"course":"math","score":89} {"course":"chinese","score":95} Time taken: 44.968 seconds select t.course.course from test t; english math chinese Time taken: 15.827 seconds hive> select t.course.score from test t; 80 89 95 Time taken: 13.235 seconds
4. 不支持組合的復(fù)雜數(shù)據(jù)類型
我們有時(shí)候可能想建一個(gè)復(fù)雜的數(shù)據(jù)集合類型,比如下面的a字段,本身是一個(gè)Map,它的key是string類型的,value是Array集合類型的。
建表
create table test1(id int,a MAP<STRING,ARRAY<STRING>>) row format delimited fields terminated by '\t' collection items terminated by ',' MAP KEYS TERMINATED BY ':';
導(dǎo)入數(shù)據(jù)
1 english:80,90,70 2 math:89,78,86 3 chinese:99,100,82 LOAD DATA LOCAL INPATH '/home/hadoop/test1.txt' OVERWRITE INTO TABLE test1;
這里查詢出數(shù)據(jù):
hive> select * from test1; OK 1 {"english":["80"],"90":null,"70":null} 2 {"math":["89"],"78":null,"86":null} 3 {"chinese":["99"],"100":null,"82":null}
可以看到,已經(jīng)出問(wèn)題了,我們意圖是想"english":["80", "90", "70"],實(shí)際上把90和70也當(dāng)作Map的key了,value值都是空的。分析一下我們的建表語(yǔ)句,collection items terminated by ','制定了集合類型(map, struct, array)數(shù)據(jù)元素之間分隔符是", ",實(shí)際上map也是屬于集合的,那么也會(huì)按照逗號(hào)分出3個(gè)key-value對(duì);由于MAP KEYS TERMINATED BY ':'定義了map中key-value的分隔符是":",第一個(gè)“english”可以準(zhǔn)確識(shí)別,后面的直接把value置為"null"了。
關(guān)于Hive中有哪些集合數(shù)據(jù)類型就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。