Hive&Impala的UDF
目标:自定义实现长度函数
重构length(string) 函数,impala写法要求与hive的不一样,通俗的讲,hive中能执行的udf,impala中不一定能执行
意义
impala中1个汉字长度是3,与hive不统一,不方便使用
操作步骤
1、引包[maven]
对应的hive hadoop版本。
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-exec</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>3.0.0</version>
</dependency>
2、编码[code]
package com.xh.hive.udf;
import org.apache.hadoop.hive.ql.exec.UDF;
public class My_length extends UDF {
public static int evaluate(String data) {
if(data == null){
return 0;
}else{
return data.length();
}
}
}
3、打包[package]
4、jar包放入hdfs
5、hive创建函数
CREATE FUNCTION default.my_length AS 'com.xh.hive.udf.My_length' USING JAR '/user/hive/udf/learn-one-learn.jar';
6、impala创建函数
create function default.my_length(string) returns int location '/user/hive/udf/learn-one-learn.jar' symbol='com.xh.hive.udf.My_length';
7、测试
select default.my_length('dd'),default.my_length('许欢') ,length('许欢')
create table test.xt4 (s string);
insert into test.xt4 values ('123'),('UPPER'),('汉字');
select s,default.my_length(s),length(s) from test.xt4;