學習筆記 hive 之行拆列explode

2021-09-20 01:35:30 字數 2216 閱讀 2810

1、explode

explode(array) 列表中的每個元素生成一行

explode(map) map中每個key-value對,生成一行,key為一列,value為一列

限制:1、no other expressions are allowed in select

select pageid, explode(adid_list) as mycol... is not supported
2、udtf's can't be nested

select explode(explode(adid_list)) as mycol... is not supported
3、group by / cluster by / distribute by / sort by is not supported

select explode(adid_list) as mycol ... group by mycol is not supported
2、lateral view

可使用lateral view解除以上限制,語法:

lateralview: lateral view explode(expression) tablealias as columnalias (',' columnalias)*

fromclause: from basetable (lateralview)*

案例:table名稱為pageads

輸出結果:

3、多個lateral view

from語句後面可以帶多個lateral view語句

案例:表名:basetable

from後只有乙個lateral view:

select mycol1, col2 from basetable

lateral view explode(col1) mytable1 as mycol1;

結果:

多個lateral view:

select mycol1, mycol2 from basetable

lateral view explode(col1) mytable1 as mycol1

lateral view explode(col2) mytable2 as mycol2;

結果:

4、outer lateral views

比如:select * from src lateral view explode(array()) c as a limit 10;

這條語句中的array欄位是個空列表,這條語句不管src表中是否有記錄,結果都是空的。

而:select * from src lateral view outer explode(array()) c as a limit 10;

結果中的記錄數為src表的記錄數,只是a欄位為null。

比如:238 val_238 null

86 val_86 null

311 val_311 null

27 val_27 null

165 val_165 null

409 val_409 null

255 val_255 null

278 val_278 null

98 val_98 null

官方文件:

hive 之行拆列explode

1 explode explode array 列表中的每個元素生成一行 explode map map中每個key value對,生成一行,key為一列,value為一列 限制 1 no other expressions are allowed in select select pageid,e...

HIve之行轉列,列轉行操作

行轉列 將類似如下資料轉為,特徵值,標記資料 表名為test 3列c1,c2,c3資料如下 a,b,1 a,b,2 a,b,3 c,d,4 c,d,5 d,f 6 多行轉換為一列 select c1,c2,concat ws collect set c3 from test group by c1,...

Hive經典SQL之行轉列,列轉行

前言 目前做離線資料計算,由於之前沒有完整的數倉功能,前兩天把cdh的配置和自己的 又除錯了一遍,目前資料已經進入到hive原始資料ods層。使用到了這個經典函式,在這裡記錄一下。版本 hive 1.1.0 建立表挺簡單的,這裡就不演示了。stu name course score 張三語文 98張...