--创建 create table temp_db.json_test (id int comment '源数据库id主键', str string comment '日志字符串');
--插入 insert into temp_db.json_test(id,str) values (1,'{"name":"孙先生","carrer":"大数据开发工程师","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":"退休农民","dream":["儿子听话","带孙子"],"friend":{ "friend_1":"CC" } }');
--json_tuple提取数据 -- 提取一级格式下的数据 select name from temp_db.json_test lateral view json_tuple(str,'name') tb as name;
-- 提取二级格式下的数据(如好友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;
-- 提取标签中所有的内容(没有的标签,返回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元素内容(使用"$"的方式,"."表示对象,"[]"引用数组) -- 获取标签对象 select get_json_object(str,'$.name') as name from temp_db.json_test;
-- 获取标签中的数组元素 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;
|