(一)集合數據類型
數據類型 |
描述 |
語法示例 |
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類似,它封裝了一個命名字段集合,復雜數據類型允許任意層次的嵌套。
(1)數據結構如下
{ "name": "songsong", "friends": ["bingbing" , "lili"] , //列表Array, "children": { //鍵值Map, "xiao song": 18 , "xiaoxiao song": 19 } "address": { //結構Struct, "street": "hui long guan" , "city": "beijing" } }
(2)hive上創建表格
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 STRUCT 和 ARRAY 的分隔符(數據分割符號) map keys terminated by ':' -- MAP中的key與value的分隔符 lines terminated by '\n'; -- 行分隔符
具體文件數據如下
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
(3)將文件的數據同步到hive
load data local inpath '/home/hadoop/file/file1' into table test;
(4)查詢某條數據的制定元素
select friend[1],children['xiao song'],address.city from test where name='songsong';
(5)運行結果
(二)hive刪除數據庫
(1)刪除空的數據庫
drop database db_hive2;//若數據庫不存在會報錯
drop database if exists db_hive2;
(2)刪除不為空的數據庫
drop database db_hive cascade;//cascade為強制刪除