lateral view 側視圖 explode 爆炸
select wdmc,customer_id from(
select 1 as wdmc,'14590170|14587203|21396385|22105752' as qyid
)a lateral view explode(split(qyid,'|')) as customer_id
報錯:
Error while compiling statement: FAILED: ParseException line 5:44 extraneous input 'customer_id' expecting EOF near '<EOF>'
側視圖本質是left join, explode 之後就是 右表了,原表爲座標
而左右兩表 都需要 命名
正確的寫法: 多了一個b(側視圖命名) 而已
select wdmc,customer_id from(
select 1 as wdmc,"14590170|14587203|21396385|22105752" as qyid
)a lateral view explode(split(qyid,"|"))b as customer_id
但結果有問題,我想用‘|’隔開 但結果是:
wdmc | customer_id | |
---|---|---|
1 | 1 | 1 |
2 | 1 | 4 |
3 | 1 | 5 |
4 | 1 | 9 |
5 | 1 | 0 |
6 | 1 | 1 |
7 | 1 | 7 |
8 | 1 | 0 |
9 | 1 | | |
10 | 1 | 1 |
11 | 1 | 4 |
12 | 1 | 5 |
13 | 1 | 8 |
...
嘗試單獨使用explode:
select explode(split("14590170|14587203|21396385|22105752","|")) as id
結果一樣:
id | |
---|---|
1 | 1 |
2 | 4 |
3 | 5 |
4 | 9 |
5 | 0 |
6 | 1 |
7 | 7 |
8 | 0 |
9 | | |
10 | 1 |
11 | 4 |
12 | 5 |
13 | 8 |
14 | 7 |
15 | 2 |
split有問題
describe function split
tab_name | |
---|---|
1 | split(str, regex) - Splits str around occurances that match regex |
後來慢慢分析,原來是轉義有問題,單獨一個 ‘|’ 在 hive中可能是別的含義,我們需要轉義‘|’ 讓hive知道我要的就是 ‘|’用它,
但是 ‘\|’ 其實這樣也是錯的,因爲 ‘\’ 在hive中也有別的意思,所以,我們要將斜槓 ‘\’ 先轉義成 真斜槓
select explode(split("14590170|14587203|21396385|22105752","\\|")) as id
id | |
---|---|
1 | 14590170 |
2 | 14587203 |
3 | 21396385 |
4 | 22105752 |
再搭配 側視圖 lateral view
select wdmc,customer_id from(
select 1 as wdmc,"14590170|14587203|21396385|22105752" as qyid
)a lateral view explode(split(qyid,"\\|"))b as customer_id
結果完美:
wdmc | customer_id | |
---|---|---|
1 | 1 | 14590170 |
2 | 1 | 14587203 |
3 | 1 | 21396385 |
4 | 1 | 22105752 |