一、原始數(shù)據(jù)
acct | content |
1232313 | [{"name":"張三","code":"上海浦東新區(qū)89492jfkdaj\r\n福建的卡"...},{"name":"狂徒","code":"select * from table where aa=1\r\n and a=12"...},{...}] |
... | ... |
二、需求
?上述數(shù)據(jù)表名code_content,把json中code內(nèi)容全都取出來拼接成一行數(shù)據(jù),最終效果:
acct | new_content |
1232313 | 上海浦東新區(qū)89492jfkdaj\r\n福建的卡\u0001select * from table where aa=1\r\n and a=12 |
... | ... |
三、解析思路
四、實(shí)現(xiàn)方法
1.sql
select acct,concat_ws('\u0001',collect_list(t.code)) code
from
(
select acct,get_json_object(a_json,'$.code') code
from
(
select acct,
split(regexp_replace(regexp_extract(code,'(^\\[)(.*?)(\\]$)',2),'\\},\\{','\\}|\\{'),'\\|') as t_code
from code_content
where dt=20230823
) a
lateral view explode(t_code) code_content_tab as a_json
) t
group by acct
2.sql解釋:
- regexp_extract(code,'(^\\[)(.*?)(\\]$)',2):用正則取出數(shù)組里的json
- regexp_replace:替換},{為}|{,方便切割因?yàn)?號(hào)一般語句里會(huì)比較多
- split:切割成數(shù)組
- explode:函數(shù)中的參數(shù)傳入的是arrary數(shù)據(jù)類型的列名,通常,explode函數(shù)會(huì)與lateral view一起結(jié)合使用
- lateral view:Lateral View配合 split, explode 等UDTF函數(shù)一起使用,它能夠?qū)⒁涣袛?shù)據(jù)拆成多行數(shù)據(jù),并且對(duì)拆分后結(jié)果進(jìn)行聚合,即將多行結(jié)果組合成一個(gè)支持別名的虛擬表。
- get_json_object(a_json,'$.code'):獲取json字段value
- concat_ws('\u0001',collect_list(t.code)):“列轉(zhuǎn)行”
參考:
Hive SQL中的 lateral view 與 explode(列轉(zhuǎn)行)以及行轉(zhuǎn)列_sql explode_賣山楂啦prss的博客-CSDN博客文章來源:http://www.zghlxwxcb.cn/news/detail-668102.html
hivesql解析json數(shù)組并拆分成多行_hive sql怎么對(duì)一個(gè)數(shù)組進(jìn)行分行_Time Woods的博客-CSDN博客文章來源地址http://www.zghlxwxcb.cn/news/detail-668102.html
到了這里,關(guān)于Hive字符串?dāng)?shù)組json類型取某字段再列轉(zhuǎn)行的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!