一、引用
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-exec</artifactId>
<version>1.2.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.7.3</version>
</dependency>
二、代码
package UDF;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;
public class LowerUDF extends UDF{
/**
* 1. Implement one or more methods named "evaluate" which will be called by Hive.
*
* 2. "evaluate" should never be a void method. However it can return "null" if needed.
*/
public Text evaluate(Text str){
// input parameter validate
if(null == str){
return null ;
}
// validate
if(StringUtils.isBlank(str.toString())){
return null ;
}
// lower
return new Text(str.toString().toLowerCase()) ;
}
public static void main(String[] args) {
System.out.println(new LowerUDF().evaluate(new Text("BBB")));
}
}
引用完写好后,点击maven的package,打包成jar包
三、上传到hdfs
1、创建目录并上传 hadoop fs -mkdir /hive hadoop fs -put myMavenTest-1.0-SNAPSHOT.jar /hive hadoop fs -ls /hive2、hive客户端添加jar包
hive
add jar hdfs://wangshida:8020/hive/myMavenTest-1.0-SNAPSHOT.jar; 3、设置hiveUDF的main主类 create temporary function lower_udf as 'hive.LowerUDF'; 4、测试 create table testhive (id int ,name string); insert into testhive values(1,'WWWWAA'); select lower_udf(name) from testhive;标签:Text,jar,hadoop,hive,添加,udf,Hive,apache,org From: https://www.cnblogs.com/robots2/p/18643882