Hive 行列转换
在京東眾多業(yè)務(wù)中,促銷業(yè)務(wù)充滿了復(fù)雜性和挑戰(zhàn)性,因?yàn)闃I(yè)務(wù)的靈活性,很多數(shù)據(jù)都存儲(chǔ)成xml和json格式數(shù)據(jù),這就要求下游數(shù)據(jù)分析師們需要對(duì)其做解析后方可使用 。
在眾多操作中 ,有一種是需要對(duì)數(shù)據(jù)做行列轉(zhuǎn)換操作。
數(shù)據(jù)結(jié)構(gòu):
create external table jd_row_to_column( jd_id string , jd_session string );數(shù)據(jù)描述:
insert into jd_row_to_column values('0001','01,02,03,04');1.行轉(zhuǎn)列(對(duì)一列數(shù)據(jù)拆分成多行)
使用函數(shù) lateral view explode(split(column_name, ',')) col_nm
我們對(duì)上面表結(jié)構(gòu)的jd_session列按照逗號(hào)進(jìn)行數(shù)據(jù)拆分
拆分SQL:
selectjd_id,jd_session,session fromjd_row_to_column lateral view explode(split(jd_session, ',')) ses as session wherejd_id = '0001';The result:
?
2.列轉(zhuǎn)行(根據(jù)主鍵,數(shù)據(jù)合并)
使用函數(shù):concat_ws(',',collect_set(column))? 其中 函數(shù)collect_set 會(huì)對(duì)原始數(shù)據(jù)做去重操作,collect_list 則不會(huì)
我們將對(duì)上面的結(jié)果進(jìn)行數(shù)據(jù)合并操作,看看結(jié)果是不是滿足要求
The SQL:
selectjd_id,jd_session,concat_ws(',', collect_set(session)) as jd_session_v2 from(selectjd_id,jd_session,sessionfromjd_row_to_column lateral view explode(split(jd_session, ',')) ses as sessionwherejd_id = '0001')t group byjd_id,jd_session ;The Result :
?
轉(zhuǎn)載于:https://www.cnblogs.com/TendToBigData/p/10501170.html
總結(jié)
- 上一篇: git: command not fou
- 下一篇: JAVA实现Excel的读写--poi