背景:
提取json中某個字段的故事,get_json_object,但是發現好像不能直接提取數組
嘗試使用
split(regexp_replace(substr(contacts, 2, length(contacts)-2), '"}","', '"}",,,,"'), ',,,,'))
之後提取失敗~ 提取內容基本如下
[{"userId":89224,"contactName":"","contactPhone":"+","originPhone":"+"}, {"userId":89223,"contactName":"","contactPhone":"+","originPhone":""}, {"userId":89221,"contactName":"","contactPhone":"+","originPhone":"+"}] |
還是直接放方案吧 '$[*]' 推薦
select user_id, get_json_object(contacts,'$[*].userId') as cons
from XXXX
你就會得到 類似如下的內容
user_id | cons | |
---|---|---|
1 | 107521212 | [8397213,85,7418687,304388953,198708241,148,111920,5285539,97773639] |
但是還不夠 我想要生成映射map
此時推薦 :
LATERAL VIEW explode (split(substr(t.cons, 2, length(t.cons)-2) , ',')) temp as contact_id
關於 explode 和 LATERAL VIEW 的用法 可以自行百度 不容踩坑
最終寫法:完成列轉行和映射關係
SELECT t.user_id as user_id , contact_id from (
select user_id, get_json_object(contacts,'$[*].userId') as cons
from phone_contacts
WHERE dt = '2020-06-27') as t
LATERAL VIEW explode (split(substr(t.cons, 2, length(t.cons)-2) , ',')) temp as contact_id