1. UDTF介紹
UDTF(User-Defined Table-Generating Functions) 用來解決 輸入一行輸出多行(On-to-many maping) 的需求。
2. 編寫自己需要的UDTF
- 繼承org.apache.hadoop.hive.ql.udf.generic.GenericUDTF。
- 實現initialize, process, close三個方法
- UDTF首先會調用initialize方法,此方法返回UDTF的返回行的信息(返回個數,類型)。初始化完成後,會調用process方法,對傳入的參數進行處理,可以通過forword()方法把結果返回。最後close()方法調用,對需要清理的方法進行清理。
下面是我寫的一個用來切分”key:value;key:value;”這種字符串,返回結果爲key, value兩個字段。供參考:
1: import java.util.ArrayList;
2:
3: import org.apache.hadoop.hive.ql.udf.generic.GenericUDTF;
4: import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
5: import org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException;
6: import org.apache.hadoop.hive.ql.metadata.HiveException;
7: import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
8: import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory;
9: import org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector;
10: import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
11:
12: public class ExplodeMap extends GenericUDTF{
13:
14: @Override
15: public void close() throws HiveException {
16: // TODO Auto-generated method stub
17: }
18:
19: @Override
20: public StructObjectInspector initialize(ObjectInspector[] args)
21: throws UDFArgumentException {
22: if (args.length != 1) {
23: throw new UDFArgumentLengthException("ExplodeMap takes only one argument");
24: }
25: if (args[0].getCategory() != ObjectInspector.Category.PRIMITIVE) {
26: throw new UDFArgumentException("ExplodeMap takes string as a parameter");
27: }
28:
29: ArrayList<String> fieldNames = new ArrayList<String>();
30: ArrayList<ObjectInspector> fieldOIs = new ArrayList<ObjectInspector>();
31: fieldNames.add("col1");
32: fieldOIs.add(PrimitiveObjectInspectorFactory.javaStringObjectInspector);
33: fieldNames.add("col2");
34: fieldOIs.add(PrimitiveObjectInspectorFactory.javaStringObjectInspector);
35:
36: return ObjectInspectorFactory.getStandardStructObjectInspector(fieldNames,fieldOIs);
37: }
38:
39: @Override
40: public void process(Object[] args) throws HiveException {
41: String input = args[0].toString();
42: String[] test = input.split(";");
43: for(int i=0; i<test.length; i++) {
44: try {
45: String[] result = test[i].split(":");
46: forward(result);
47: } catch (Exception e) {
48: continue;
49: }
50: }
51: }
52: }
3. 使用方法
UDTF有兩種使用方法,一種直接放到select後面,一種和lateral view一起使用。
1:直接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
2:和lateral view一起使用:select src.id, mytable.col1, mytable.col2 from src lateral view explode_map(properties) mytable as col1, col2;
- 此方法更爲方便日常使用。執行過程相當於單獨執行了兩次抽取,然後union到一個表裏。
4. 參考文檔
http://wiki.apache.org/hadoop/Hive/LanguageManual/UDF
http://wiki.apache.org/hadoop/Hive/DeveloperGuide/UDTF
http://www.slideshare.net/pauly1/userdefined-table-generating-functions