版權聲明:本文爲博主原創文章,轉載請標明出處:
歡迎訪問:https://blog.csdn.net/qq_21439395
交流QQ: 824203453
歡迎訪問博主個人主頁:http://www.oldsheep.cn
Hive數據類型
基本數據類型
Hive數據類型 |
Java數據類型 |
長度 |
例子 |
TINYINT |
byte |
1byte有符號整數 |
20 |
SMALINT |
short |
2byte有符號整數 |
20 |
INT |
int |
4byte有符號整數 |
20 |
BIGINT |
long |
8byte有符號整數 |
20 |
FLOAT |
float |
單精度浮點數 |
3.14159 |
DOUBLE |
double |
雙精度浮點數 |
3.14159 |
|
|
|
|
STRING |
string |
字符系列。可以指定字符集。可以使用單引號或者雙引號。 |
‘now is the time’ “for all good men” |
|
字符串1-65535長度,超長截斷 |
|
|
|
字符串,最大長度255 |
|
|
|
|
|
|
BOOLEAN |
boolean |
布爾類型,true或者false |
TRUE FALSE |
BINARY |
|
字節數組 |
|
TIMESTAMP |
|
時間類型 包含年月日時分秒毫秒 |
|
|
日期,只包含年月日 |
|
對於Hive的String類型相當於數據庫的varchar類型,該類型是一個可變的字符串,不過它不能聲明其中最多能存儲多少個字符,理論上它可以存儲2GB的字符數。
示例:
create table t_test(a string ,b int,c bigint,d float,e double,f tinyint,g smallint)
日期類型示例:
1,zhangsan,1985-06-31
2,lisi,1986-07-10
3,wangwu,1985-08-09
那麼,就可以建一個表來對數據進行映射
create table t_customer(id int,name string,birthday date)
row format delimited fields terminated by ',';
然後導入數據
load data local inpath '/root/customer.dat' into table t_customer;
然後,就可以正確查詢
boolean類型實例:
1,zs,28,true
2,ls,30,false
3,ww,32,false
4,lulu,18,true
create table t_p(id int,name string,age int,is_married boolean)
select * from t_p where is_married;
集合數據類型
數據類型 |
描述 |
語法示例 |
STRUCT |
和c語言中的struct類似,都可以通過“點”符號訪問元素內容。例如,如果某個列的數據類型是STRUCT{first STRING, last STRING},那麼第1個元素可以通過字段.first來引用。 |
struct() |
MAP |
MAP是一組鍵-值對元組集合,使用數組表示法可以訪問數據。例如,如果某個列的數據類型是MAP,其中鍵->值對是’first’->’John’和’last’->’Doe’,那麼可以通過字段名[‘last’]獲取最後一個元素 |
map() |
ARRAY |
數組是一組具有相同類型和名稱的變量的集合。這些變量稱爲數組的元素,每個數組元素都有一個編號,編號從零開始。例如,數組值爲[‘John’, ‘Doe’],那麼第2個元素可以通過數組名[1]進行引用。 |
Array() |
Hive有三種複雜數據類型ARRAY、MAP 和 STRUCT。ARRAY和MAP與Java中的Array和Map類似,而STRUCT與C語言中的Struct類似,它封裝了一個命名字段集合,複雜數據類型允許任意層次的嵌套。
array數組類型
arrays: ARRAY<data_type> )
示例:array類型的應用
假如有如下數據需要用hive的表去映射:
戰狼2,吳京:吳剛:余男,2017-08-16 三生三世十里桃花,劉亦菲:癢癢,2017-08-20 羞羞的鐵拳,沈騰:瑪麗:艾倫,2017-12-20 |
設想:如果主演信息用一個數組來映射比較方便
建表:
create table t_movie(moive_name string,actors array<string>,first_show date)
row format delimited fields terminated by ','
collection items terminated by ':';
導入數據:
load data local inpath '/root/hivedata/movie.dat' into table t_movie;
查詢:
select * from t_movie;
select moive_name,actors[0] from t_movie;
select moive_name,actors from t_movie where array_contains(actors,'吳剛');
select moive_name,size(actors) from t_movie;
map類型
maps: MAP<primitive_type, data_type>
- 假如有以下數據:
1,zhangsan,father:xiaoming#mother:xiaohuang#brother:xiaoxu,28 2,lisi,father:mayun#mother:huangyi#brother:guanyu,22 3,wangwu,father:wangjianlin#mother:ruhua#sister:jingtian,29 4,mayun,father:mayongzhen#mother:angelababy,26 |
可以用一個map類型來對上述數據中的家庭成員進行描述
建表語句:
create table t_person(id int,name string,fm map<string,string>,age int)
row format delimited fields terminated by ','
collection items terminated by '#'
map keys terminated by ':';
加載數據:
load data local inpath '/root/hivedata/map.dat' into table t_person;
查詢
select * from t_person;
## 取map字段的指定key的值
select id,name,fm['father'] as father from t_person;
## 取map字段的所有key
select id,name,map_keys(fm) as relation from t_person;
## 取map字段的所有value
select id,name,map_values(fm) from t_person;
select id,name,map_values(fm)[0] from t_person;
struct類型
struct: STRUCT<col_name : data_type, ...>
- 假如有如下數據:
1,zhangsan,18:male:beijing 2,lisi,28:female:shanghai |
其中的用戶信息包含:年齡:整數,性別:字符串,地址:字符串
設想用一個字段來描述整個用戶信息,可以採用struct
建表:
create table t_ps(id int,name string,info struct<age:int,sex:string,addr:string>)
row format delimited fields terminated by ','
collection items terminated by ':';
加載數據:
load data local inpath '/root/hivedata/struct.dat' into table t_ps;
查詢
select * from t_ps;
select id,name,info.age from t_ps;
案例實操
1)假設某表有如下一行,我們用JSON格式來表示其數據結構。在Hive下訪問的格式爲
{ "name": "songsong", "friends": ["bingbing" , "lili"] , //列表Array, "children": { //鍵值Map, "xiao song": 18 , "xiaoxiao song": 19 } "address": { //結構Struct, "street": "hui long guan" , "city": "beijing" } } |
2)基於上述數據結構,我們在Hive裏創建對應的表,並導入數據。
創建本地測試文件test.txt
songsong,bingbing_lili,xiao song:18_xiaoxiao song:19,hui long guan_beijing yangyang,caicai_susu,xiao yang:18_xiaoxiao yang:19,chao yang_beijing |
注意,MAP,STRUCT和ARRAY裏的元素間關係都可以用同一個字符表示,這裏用“_”。
3)Hive上創建測試表test
create table test( name string, friends array<string>, children map<string, int>, address struct<street:string, city:string> ) row format delimited fields terminated by ',' collection items terminated by '_' map keys terminated by ':' lines terminated by '\n'; |
字段解釋:
row format delimited fields terminated by ',' -- 列分隔符
collection items terminated by '_' --MAP STRUCT 和 ARRAY 的分隔符(數據分割符號)
map keys terminated by ':' -- MAP中的key與value的分隔符
lines terminated by '\n'; -- 行分隔符
4)導入文本數據到測試表
hive (default)> load data local inpath '/opt/module/datas/test.txt' into table test; |
5)訪問三種集合列裏的數據,以下分別是ARRAY,MAP,STRUCT的訪問方式
hive (default)> select friends[1],children['xiao song'],address.city from test where name="songsong"; OK _c0 _c1 city lili 18 beijing Time taken: 0.076 seconds, Fetched: 1 row(s) |
類型轉化
Hive的原子數據類型是可以進行隱式轉換的,類似於Java的類型轉換,例如某表達式使用INT類型,TINYINT會自動轉換爲INT類型,但是Hive不會進行反向轉化,例如,某表達式使用TINYINT類型,INT不會自動轉換爲TINYINT類型,它會返回錯誤,除非使用CAST操作。
1)隱式類型轉換規則如下。
(1)任何整數類型都可以隱式地轉換爲一個範圍更廣的類型,如TINYINT可以轉換成INT,INT可以轉換成BIGINT。
(2)所有整數類型、FLOAT和STRING類型都可以隱式地轉換成DOUBLE。
(3)TINYINT、SMALLINT、INT都可以轉換爲FLOAT。
(4)BOOLEAN類型不可以轉換爲任何其它的類型。
2)可以使用CAST操作顯示進行數據類型轉換,例如CAST('1' AS INT)將把字符串'1' 轉換成整數1;如果強制類型轉換失敗,如執行CAST('X' AS INT),表達式返回空值 NULL。
版權聲明:本文爲博主原創文章,轉載請標明出處
交流QQ: 824203453
歡迎訪問博主個人主頁:http://www.oldsheep.cn