深入淺出學Hive——Hive高階程式設計
阿新 • • 發佈:2018-12-22
•UDTF有兩種使用方法,一種直接放到select後面,一種和lateral view一起使用
•直接select中使用:select explode_map(properties) as (col1,col2) from src;
•不可以新增其他欄位使用:select a, explode_map(properties) as (col1,col2) from src
•不可以巢狀呼叫:select explode_map(explode_map(properties)) from src
•不可以和group by/cluster by/distribute by/sort by一起使用:select explode_map(properties) as (col1,col2) from src group by col1, col2
•和lateral view一起使用:select src.id, mytable.col1, mytable.col2 from src lateral view explode_map(properties) mytable as col1, col2;
此方法更為方便日常使用。執行過程相當於單獨執行了兩次抽取,然後union到一個表裡。
lateral view
• Lateral View語法
•lateralView: LATERAL VIEW udtf(expression) tableAlias AS columnAlias (',' columnAlias)* fromClause: FROM baseTable (lateralView)*
•Lateral View用於UDTF(user-defined table generating functions)中將行轉成列,例如explode().
•目前Lateral View不支援有上而下的優化。如果使用Where子句,查詢可能將不被編譯。解決方法見:
此時,在查詢之前執行set hive.optimize.ppd=false;
• 例子
•pageAds。它有兩個列
•SELECT pageid, adid FROM pageAds LATERAL VIEW explode(adid_list) adTable AS adid;
•將輸出如下結果
string pageid int adid
"front_page" 1
…….
“contact_page" 3
程式碼示例
public class MyUDTF extends GenericUDTF{
public StructObjectInspector initialize(ObjectInspector[] args) {}
public void process(Object[] args) throws HiveException { }
}
實現:切分 ” key:value;key:value;” 這種字串, 返回結果為 key,
value 兩個欄位
string pageid |
Array<int> adid_list |
" front_page" | [1, 2, 3] |
"contact_page " | [ 3, 4, 5] |