
鏈表.jpg
Hive中Map類型的操作
map的結(jié)構(gòu)
-
創(chuàng)建map的表
create table temp_db.map_test( id int comment "源數(shù)據(jù)主鍵id" ,smap map<string,string> comment "string型map" ,imap map<string,int> comment "int型map" );map的存儲形式:key-value,如:{“張三”,23,“性別“,"male"}
-
插入map數(shù)據(jù)
insert into temp_db.map_test(id,smap,imap) select 12,map('姓名','張三') as pp,map('年齡',23,'性別',1) as dd; insert into temp_db.map_test(id,smap,imap) select 14,map('地址','安徽') as dd,map('年級',3); -- 注意,這里的map引用使用"()",有時候會錯誤寫成"{}";此外,對于key-value值來說,是沒有特定的限制的。key可以有多個。如上"姓名","地址" -
查詢map中的數(shù)據(jù)
-- ***[key] select smap['姓名'] as arg1,imap['年齡'] as age from temp_db.map_test; -
刪除map數(shù)據(jù)
別搞笑了,hive不支持刪除操作??梢允褂胕nsert overwrite。同理,也不支持修改
map的一些操作函數(shù)
-
key鍵查詢
-- map_keys(colName) 結(jié)果是一個Array,如果希望提取,則使用[index],如map_keys(smap)[0] -- hive和prest的index起點存在差異,hive從0開始,presto從1開始【我測試的環(huán)境是這樣的】 select map_keys(smap) as smap_keys,map_keys(imap) as imap_keys from temp_db.map_test; -
value值查詢
-- map_values(colname) select map_values(smap) as s_values,map_values(imap) as i_values from temp_db.map_test; -
鍵值對查詢
-- size(colName),返回對應(yīng)列有多少個key-value select size(imap) as pair_cnt from temp_db.map_test;
map類型數(shù)據(jù)的加工
-
將map列拆分為key、value列
-- smap中只存在單個key-value的情況,所有l(wèi)ateral之后,數(shù)據(jù)有單列變成雙列。但是行數(shù)沒有變化 select id,skey,svalue from temp_db.map_test lateral view explode(smap) tb as skey,svalue; -- imap中 存在多個鍵值對。這頓操作之后,行數(shù)會增加 select id,ikey,ivalue from temp_db.map_test lateral view explode(imap) tb as ikey,ivalue;
Array操作
Array的結(jié)構(gòu)
-
創(chuàng)建Array表
create table temp_db.array_test ( id int comment '源數(shù)據(jù)主鍵id' ,year_arr array<string> comment '數(shù)組記錄,年份' ,score_arr array<string> comment '數(shù)組記錄,分數(shù)' ); -
插入數(shù)據(jù)
insert into temp_db.array_test (id,year_arr,score_arr) select 12,array('1991','1990','1989'),array('56','20','23') ; -
查詢
-- 注意事項,如果數(shù)組越界了,則報錯。 select id,year_arr[1],year_arr[2] from temp_db.array_test
Array的一些操作
-
是否包含某個值(array_contains()),Boolean型(true/false,where條件中比較合適)
select * from temp_db.array_test where array_contains(year_arr,'1990'); -
拆成單條多行記錄
select col1 from temp_db.array_test lateral view explode(year_arr) tb as col1
Json的操作
在處理日志數(shù)據(jù)時,會遇到j(luò)son格式的數(shù)據(jù)。那么,在hive中如何處理它呢?
一般情況下,json數(shù)據(jù)會以string類型,字符串格式進行存儲。
-
創(chuàng)建案例
create table temp_db.json_test (id int comment '源數(shù)據(jù)庫id主鍵', str string comment '日志字符串'); insert into temp_db.json_test(id,str) values (1,'{"name":"孫先生","carrer":"大數(shù)據(jù)開發(fā)工程師","dream":["開個便利店","去外面逛一逛","看本好書"],"friend":{ "friend_1":"MM", "friend_2":"NN", "friend_3":"BB", "friend_4":"VV" } }'); insert into temp_db.json_test(id,str) values (2,'{"name":"唐女士","carrer":"退休農(nóng)民","dream":["兒子聽話","帶孫子"],"friend":{ "friend_1":"CC" } }'); -
json_tuple提取數(shù)據(jù)-- 提取一級格式下的數(shù)據(jù) select name from temp_db.json_test lateral view json_tuple(str,'name') tb as name; -- 提取二級格式下的數(shù)據(jù)(如好友1) select good_friend_1 from temp_db.json_test lateral view json_tuple(str,'friend') dd as good_friend lateral view json_tuple(good_friend,'好友1') tb as good_friend_1; -- 提取標(biāo)簽中所有的內(nèi)容(沒有的標(biāo)簽,返回null) select good_friend_1,good_friend_2,good_friend_3 from temp_db.json_test lateral view json_tuple(str,'friend') dd as good_friend lateral view json_tuple(good_friend,'好友1','好友2','好友3') tb as good_friend_1,good_friend_2,good_friend_3; -- 提取Array select dream_col from temp_db.json_test lateral view json_tuple(str,'dream') dd as dreaming lateral view explode(dreaming) tb as dream_col -
get_json_object提取指定的json元素內(nèi)容(使用"$"的方式,"."表示對象,"[]"引用數(shù)組)-- 獲取標(biāo)簽對象 select get_json_object(str,'$.name') as name from temp_db.json_test; -- 獲取標(biāo)簽中的數(shù)組元素 select get_json_object(str,'$.dream[0]') as good_friend from temp_db.json_test; -- 獲取多層中的對象 select get_json_object(str,'$.friend.friend_1') as good_friend from temp_db.json_test;
json_tuple與get_json_object都是hive自帶的UDF。json_tuple 相對于 get_json_object 的優(yōu)勢就是一次可以解析多個 Json 字段。有興趣可以參考如何在 Apache Hive 中解析 Json 數(shù)組這篇文章,其中也說了通過自行開發(fā)UDF來實現(xiàn)相關(guān)的功能。