Hive支持的數據類型分爲基礎數據類型和複雜類型,這是關係性數據庫不具備的特性。
- 基礎類型主要包括:tinyint,smalint,int,bigint,boolean,float,double,string,timestamp 等
- 複雜類型主要包括:array,map,struct等
參考:https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Types
1、map
1.1 可以存儲如下數據
"geo": {
"country": "中國",
"subdivision": "四川",
"city": "成都",
"latitude": "31.261",
"longitude": "127.22562",
"isp": "電信"
}
說明:geo 代表hive 表字段名稱,冒號後的是對應的值。
1.2 創建 hive 表
create table fdp.map_test(
id string,
geo map<string,string>
);
1.3 準備數據文件 map_test
1 country:中國,subdivision:四川,city:德陽,latitude:31.86141,longitude:117.27562,isp:移動
2 country:中國,subdivision:四川,city:綿陽,latitude:31.86141,longitude:117.27562,isp:電信
3 country:中國,subdivision:安徽,city:淮南,latitude:31.86141,longitude:117.27562,isp:電信
1.4 load 到hive 表中,然後查詢
load data local inpath "/data/map_test" into table fdp.map_test;
select * from fdp.map_test where geo["country"] ='中國' ;
2、array
2.1 可以存儲如下數據
"business": ["it","os","abc"]
說明:business 代表hive 表裏的字段名稱,冒號後的是對應的值。
2.2 創建hive 表
create table fdp.array_test(
id string,
business array<string>
);
2.3 準備數據文件 array_test
1 iqwt,st,ows,dds
2 idw,iss,dcf
3 idw,swd,dv
2.4 load 到 hive 表中,然後查詢
注意: sql 中需要使用視圖,spark sql 和 hive sql 都支持 視圖。
#load data local inpath "/data/array_test" into table fdp.array_test;
#select * from fdp.array_test LATERAL VIEW explode(business) rTable AS realid where realid='it' limit 2;