Hive 數據類型



1. hive的數據類型
Hive的內置數據類型可以分為兩大類:(1)、基礎數據類型;(2)、復雜數據類型
2. hive基本數據類型
基礎數據類型包括:

 


TINYINT,SMALLINT,INT,BIGINT,BOOLEAN,FLOAT,DOUBLE,STRING,BINARY,TIMESTAMP,DECIMAL,CHAR,VARCHAR,DATE。




3. hive集合類型
集合類型主要包括:array,map,struct等,hive的特性支持集合類型,這特性是關系型數據庫所不支持的,利用好集合類型可以有效提升SQL的查詢速率。


3.1 集合類型之array
(1) 先創建一張表


create table t_array(id int,name string,hobby array<string>)
row format delimited
fields terminated by ','
collection items terminated by '-';


 

(2) 准備數據文件 array.txt

1,zhangsan,唱歌-跳舞-游泳
2,lisi,打游戲-籃球

(3) 加載數據文件到t_array表中


load data local inpath ‘/root/array.txt’ into table t_array;



(4) 查詢數據


select id ,name,hobby[0],hobby[1] from t_array;



注意:array的訪問元素和java中是一樣的,這里通過索引來訪問。

 


3.2 集合類型之map
(1) 先創建一張表


create table t_map(id int,name string,hobby map<string,string>)
row format delimited
fields terminated by ','
collection items terminated by '-'
map keys terminated by ':' ;


 

(5) 准備數據文件 map.txt

1,zhangsan,唱歌:非常喜歡-跳舞:喜歡-游泳:一般般
2,lisi,打游戲:非常喜歡-籃球:不喜歡

(6) 加載數據文件到t_map表中


load data local inpath ‘/root/map.txt’ into table t_map;



(7) 查詢數據
select id,name,hobby['唱歌'] from t_map;
注意:map的訪問元素中的value和java中是一樣的,這里通過key來訪問。
3.3集合類型之struct
(1) 先創建一張表


create table t_struct(id int,name string,address struct<country:string,city:string>)
row format delimited
fields terminated by ','
collection items terminated by '-';


 

(8) 准備數據文件 struct.txt

1,zhangsan,china-beijing
2,lisi,USA-newyork

(9) 加載數據文件到t_struct表中


load data local inpath ‘/root/struct.txt’ into table t_struct;



(10) 查詢數據


select id,name,address.country,address.city from t_struct;



總結:struct訪問元素的方式是通過.符號

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM