Lateral View和Explode用法簡介

一、Explode用法

  • hive wiki對于expolde的解釋如下:

explode() takes in an array (or a map) as an input and outputs the elements of the array (map) as separate rows. UDTFs can be used in the SELECT expression list and as a part of LATERAL VIEW.

explode()接受一個數(shù)組(或一個map)作為輸入,并將數(shù)組元素(map)作為單獨的行輸出。 UDTF可以在SELECT表達(dá)式列表中使用,也可以作為LATERAL VIEW的一部分使用。

使用如下圖:

  • 將Map作為輸入端
map作為輸入端
  • 將ArrayList作為輸入端:
array作為輸入端

二、Lateral View用法

lateral view的意義是配合explode(或者其他的UDTF),一個語句生成把單行數(shù)據(jù)拆解成多行后的數(shù)據(jù)結(jié)果集。

首先準(zhǔn)備一張表test,test表的數(shù)據(jù)結(jié)構(gòu)如下

test表結(jié)構(gòu)
利用 lateral view explode 結(jié)合map的使用方式和結(jié)果如下:
select
    t.cola
    ,t.indexa
    ,tt.class_id
    ,tt.score
from test t
LATERAL VIEW explode(map('1',100,'2',200)) tt as class_id ,score
;

輸出結(jié)果:

結(jié)果

結(jié)果注解:

test本身只有4條記錄, explode(map('1',100,'2',200)) 本身只有2條記錄,

上述sql的原理實質(zhì)上是對2個結(jié)果集做了笛卡爾積。

實現(xiàn)形式如下:

select 

    t1.column1

    ,t1.column2

    ,…

    ,t2.column1

    ,t2.column2 

from tablea t1 

lateral view explode(…) t2 as column1,column2

;
利用 lateral view explode 結(jié)合array的使用方式和結(jié)果如下:
select

    t.cola

    ,t.indexa

    ,tt.class_id

from test t

lateral view explode(array(1,100)) tt as class_id

;

輸出如下:

ArrayList端結(jié)果
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容