您好,登錄后才能下訂單哦!
這篇文章主要介紹了Hive復合數(shù)據(jù)類型怎么用,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
1.概述
Hive作為大數(shù)據(jù)中離線數(shù)據(jù)的存儲,并把Hive作為構建數(shù)據(jù)倉庫的環(huán)境,一般情況下數(shù)據(jù)倉庫的數(shù)據(jù)類型都是基本數(shù)據(jù)類型如int、string、double等,但是有時候也會需要一些復合數(shù)據(jù)結構來存儲數(shù)據(jù),如array、map、struct;下面我們就分別介紹下這三種符合數(shù)據(jù)結構:
類型 | 定義 | 說明 |
array | Array<data_type> | array中的數(shù)據(jù)為相同類型,例如,假如array A中元素['a','b','c'],則A[1]的值為'b' |
map | Map<key,value> | Map數(shù)據(jù)類型,主要是以K:V形式進行存儲可以通過字段名[‘key’]進行訪問,將返回這個key對應的Value |
struct | STRUCT < col_name : data_type [COMMENT col_comment], …> | structs內(nèi)部的數(shù)據(jù)可以通過DOT(.)來存取,例如,表中一列a的類型為STRUCT{b INT; c INT},我們可以通過a.ba來訪問域b |
2.Array使用
1).新建一張學生成績表student1,里面有id,name,score字段,score是個array數(shù)據(jù)類型,里面是學生的成績,新建表語句:
hive>create table student1(id int,name string, score array<double>) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' COLLECTION ITEMS TERMINATED BY '|';
2)數(shù)據(jù)準備student1.txt
[root@salver158 ~]# cat student1.txt 100,"student1",80|82|84101,"student2",70|72|74102,"student3",60|62|64
3)加載數(shù)據(jù)
hive>load data local inpath "/root/student1.txt" into table student1;
4)加載成功,查詢下看看:
hive> select * from student1;OK100 "student1" [80.0,82.0,84.0]101 "student2" [70.0,72.0,74.0]102 "student3" [60.0,62.0,64.0]Time taken: 0.612 seconds, Fetched: 3 row(s)
3.Map使用
1).新建表sudent2,字段id,name,score,其中score數(shù)據(jù)類型為Map<科目,分數(shù)>,建表語句:
hive> create table student2(id int,name string,score map<string,double>)ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' COLLECTION ITEMS TERMINATED BY '|' MAP KEYS TERMINATED BY ':';
2).數(shù)據(jù)準備student2.txt
[root@salver158 ~]# cat student2.txt 100,"student1","yuwen":80|"shuxue":82|"yingyu":84101,"student2","yuwen":70|"shuxue":72|"yingyu":74102,"student3","yuwen":60|"shuxue":62|"yingyu":64
3).數(shù)據(jù)加載
hive> load data local inpath "/root/student2.txt" into table student2;
4).加載成功,查詢下看看:
hive> select * from student2;OK100 "student1" {"\"yuwen\"":80.0,"\"shuxue\"":82.0,"\"yingyu\"":84.0}101 "student2" {"\"yuwen\"":70.0,"\"shuxue\"":72.0,"\"yingyu\"":74.0}102 "student3" {"\"yuwen\"":60.0,"\"shuxue\"":62.0,"\"yingyu\"":64.0}Time taken: 0.124 seconds, Fetched: 3 row(s)
4.struct使用
1).新建表sudent3,字段id,name,score,其中score數(shù)據(jù)類型為struct<kecheng:string,score:double>,建表語句:
hive> create table student3(id int,name string,score struct<kecheng:string,score:double>)ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' COLLECTION ITEMS TERMINATED BY '|' ;
2).數(shù)據(jù)準備student3.txt
[root@salver158 ~]# cat student3.txt 100,"student1","yuwen"|80101,"student2","yuwen"|70102,"student3","yuwen"|60
3).數(shù)據(jù)加載
hive> load data local inpath "/root/student3.txt" into table student3;
4).加載成功,查詢下看看:
hive> select * from student3;OK100 "student1" {"kecheng":"\"yuwen\"","score":80.0}101 "student2" {"kecheng":"\"yuwen\"","score":70.0}102 "student3" {"kecheng":"\"yuwen\"","score":60.0}Time taken: 0.091 seconds, Fetched: 3 row(s)
感謝你能夠認真閱讀完這篇文章,希望小編分享的“Hive復合數(shù)據(jù)類型怎么用”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業(yè)資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。