1.概述數據結構
Hive做爲大數據中離線數據的存儲,並把Hive做爲構建數據倉庫的環境,通常狀況下數據倉庫的數據類型都是基本數據類型如int、string、double等,可是有時候也會須要一些複合數據結構來存儲數據,如array、map、struct;下面咱們就分別介紹下這三種符合數據結構:app
類型 |
定義 |
說明 |
array |
Array<data_type> | array中的數據爲相同類型,例如,假如array A中元素['a','b','c'],則A[1]的值爲'b' |
map |
Map<key,value> | Map數據類型,主要是以K:V形式進行存儲能夠經過字段名[‘key’]進行訪問,將返回這個key對應的Value |
struct |
STRUCT < col_name : data_type [COMMENT col_comment], …> | structs內部的數據能夠經過DOT(.)來存取,例如,表中一列a的類型爲STRUCT{b INT; c INT},咱們能夠經過a.ba來訪問域b |
2.Array使用ide
1).新建一張學生成績表student1,裏面有id,name,score字段,score是個array數據類型,裏面是學生的成績,新建表語句:大數據
hive>create table student1(id int,name string, score array<double>) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' COLLECTION ITEMS TERMINATED BY '|';
2)數據準備student1.txtspa
[root@salver158 ~]# cat student1.txt 100,"student1",80|82|84101,"student2",70|72|74102,"student3",60|62|64
3)加載數據
orm
hive>load data local inpath "/root/student1.txt" into table student1;
4)加載成功,查詢下看看:string
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使用
it
1).新建表sudent2,字段id,name,score,其中score數據類型爲Map<科目,分數>,建表語句:table
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).數據準備student2.txt
class
[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).數據加載
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數據類型爲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).數據準備student3.txt
[root@salver158 ~]# cat student3.txt 100,"student1","yuwen"|80101,"student2","yuwen"|70102,"student3","yuwen"|60
3).數據加載
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)
至此三種複合數據類型的使用介紹完成。