1、hive的常見的數據結構
Hive數據結構 |
Java數據結構 |
int | int |
bigint | long |
string | string |
hive的數據類型基本沒變,除了long變成bigint之外,其他的都沒有變
2、集合類型
數據類型 |
描述 |
語法示例 |
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() |
集合類型舉例使用
案例實操
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) |