Hive创建一个简单的UDF

创建一个类

package com.dufeng.hive;

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;

/**
 * Hello world!
 *
 */
public class Dufeng extends UDF {
    
    private Text result = new Text();
    
    public Text evaluate(Text str, String stripChars) {
        if (str == null) {
            return null;
        }
        
        result.set(StringUtils.strip(str.toString(), stripChars));
        return result;
    }
    
    
    public Text evaluate(Text str) {
        if (str == null) {
            return null;
        }
        
        result.set("hello " + StringUtils.strip(str.toString()));
        return result;
    }
}

用Maven构建成jar包

maven clean install package

打开hive shell控制台

hive> add jar /home/hive/dufengHive-0.0.1-SNAPSHOT.jar;
Added [/home/hive/dufengHive-0.0.1-SNAPSHOT.jar] to class path
Added resources: [/home/hive/dufengHive-0.0.1-SNAPSHOT.jar]
hive> list jars;
/home/hive/dufengHive-0.0.1-SNAPSHOT.jar
hive> create temporary function dufeng as 'com.dufeng.hive.Dufeng';
OK
Time taken: 0.382 seconds
hive> select dufeng('sss') from employee;
OK
hello sss
hello sss
hello sss
hello sss
hello sss
hello sss
hello sss
hello sss
hello sss
hive> select dufeng('hadoop', 'ha') from employee;
OK
doop
doop
doop
doop
doop
doop
doop
doop
doop
Time taken: 0.178 seconds, Fetched: 9 row(s)


原文地址:https://www.cnblogs.com/yandufeng/p/6437163.html