HIVE - UDTF开发(指定分割符分割字符串,返回对应的大小写字符串)

一、实现展示
hive> desc test_split_string;
name                	string

hive> select * from test_split_string;
John,Smith
John,and,Ann White
Ted,Green
Dorothy,

-- UDTF: split_string
hive> select
    >   upper_str, lower_str
    > from test_split_string
    > lateral view split_string(name, ",") tmp as upper_str, lower_str ;
JOHN	john
SMITH	smith
JOHN	john
AND	and
ANN WHITE	ann white
TED	ted
GREEN	green
DOROTHY	dorothy
二、关键函数
//指定输入输出参数
abstract StructObjectInspector initialize(ObjectInspector[] args) throws UDFArgumentException; 
//处理一条输入记录,输出(forward)多条记录
abstract void process(Object[] record) throws HiveException;
三、代码CODE
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.PrimitiveObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class SplitStringUDTF extends GenericUDTF {
    private PrimitiveObjectInspector string1OI = null;
    private PrimitiveObjectInspector string2OI = null;

    @Override
    public StructObjectInspector initialize(ObjectInspector[] args) throws UDFArgumentException {
        if (args.length != 2) {
            throw new UDFArgumentException("NameParserGenericUDTF() takes exactly tow argument");
        }
        // 输入格式(inspectors)
        string1OI = (PrimitiveObjectInspector) args[0];
        string2OI = (PrimitiveObjectInspector) args[1];
        // 输出格式(inspectors) -- 有两个属性的对象
        List<String> fieldNames = new ArrayList<String>(2);
        List<ObjectInspector> fieldOIs = new ArrayList<ObjectInspector>(2);
        fieldNames.add("upper");
        fieldNames.add("lower");
        fieldOIs.add(PrimitiveObjectInspectorFactory.javaStringObjectInspector);
        fieldOIs.add(PrimitiveObjectInspectorFactory.javaStringObjectInspector);
        return ObjectInspectorFactory.getStandardStructObjectInspector(fieldNames, fieldOIs);
    }

    public ArrayList<Object[]> processInputRecord(String str, String splitStr){
        ArrayList<Object[]> result = new ArrayList<Object[]>();
        // 忽略null值与空值
        if (str == null || str.isEmpty()) {
            return result;
        }
        String[] tokens = str.split(splitStr);
        for (String elem: tokens) {
            result.add(new Object[] { elem.toUpperCase(), elem.toLowerCase()});
        }
        return result;
    }

    @Override
    public void process(Object[] record) throws HiveException {
        final String str = string1OI.getPrimitiveJavaObject(record[0]).toString();
        final String splitStr = string2OI.getPrimitiveJavaObject(record[1]).toString();
        ArrayList<Object[]> results = processInputRecord(str, splitStr);
        Iterator<Object[]> it = results.iterator();
        while (it.hasNext()){
            Object[] r = it.next();
            forward(r);
        }
    }

    @Override
    public void close() throws HiveException {
        // do nothing
    }
}
参考文章

https://blog.csdn.net/kent7306/article/details/50200061

你可能感兴趣的:(Hive)