其他分享
首页 > 其他分享> > 自定义udtf函数(一进多出)

自定义udtf函数(一进多出)

作者:互联网

案例要求

java编写

package udtf;

import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDTF;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory;
import org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
import java.util.ArrayList;
import java.util.List;

public class MyExplode extends GenericUDTF {

    @Override
    public StructObjectInspector initialize(StructObjectInspector argOIs) throws UDFArgumentException {
        List<String> columnNames = new ArrayList<String>();
        columnNames.add("user");
        List<ObjectInspector> objectInspectors = new ArrayList<ObjectInspector>();
        objectInspectors.add(PrimitiveObjectInspectorFactory.javaStringObjectInspector);
        return ObjectInspectorFactory.getStandardStructObjectInspector(columnNames, objectInspectors);
    }

    public void process(Object[] args) throws HiveException {
        String str = args[0].toString();
        String split = args[1].toString();
        String[] strings = str.split(split);
        for (String s : strings) {
            ArrayList<String> list = new ArrayList<String>();
            list.add(s);
            forward(list);
        }
    }

    public void close() throws HiveException {

    }
}

shell

hive (default)> create temporary function myexplode as "udtf.MyExplode" using jar "hdfs://node1:9000/hive_function-1.0-SNAPSHOT.jar";
Added [/tmp/10de4466-6601-49b1-b749-8b5c8c2809b2_resources/hive_function-1.0-SNAPSHOT.jar] to class path
Added resources: [hdfs://node1:9000/hive_function-1.0-SNAPSHOT.jar]
OK
Time taken: 5.442 seconds


hive (default)> create table a(name string);
OK
Time taken: 1.046 seconds


hive (default)> insert into table a values("zs_ls_ww"),("ww_ml_wb");


hive (default)> select myexplode(name, "_") from a;
OK
user
zs
ls
ww
ww
ml
wb
Time taken: 1.138 seconds, Fetched: 6 row(s)

标签:自定义,org,udtf,hive,hadoop,import,apache,一进,ArrayList
来源: https://www.cnblogs.com/jsqup/p/16550253.html