hive 自定义UDF
复习hive
1.pom
<dependencies>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-exec</artifactId>
<version>${hive.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>${hive.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-service</artifactId>
<version>${hive.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>${hadoop.version}</version>
</dependency>
</dependencies>
2.集成UDF 实现evaluate方法
demo
import jodd.util.StringUtil;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
public class CountToolsUDF extends UDF {
public IntWritable evaluate(Text votetools){
String v = votetools.toString();
if(StringUtil.isBlank(v)){
return new IntWritable(0);
}
int length = v.trim().length();
return new IntWritable(length);
}
public static void main(String[] args) {
Text s = new Text("abc");
System.out.println(new CountToolsUDF().evaluate(s));
}
}
3.mvn -- clean package hive: add jar jar-localtion;
4.创建函数 == create function xien.count_tools_length as 'CountToolsUDF'; show functions; 发现自定义函数 --- 成功;
5.
posted on 2020-06-09 10:23 nnnnnnnnnnnnnnnn 阅读(185) 评论(0) 收藏 举报