官方地址:https://cwiki.apache.org/confluence/display/Hive/HivePlugins
什么是UDF(user-Define-Function)?用户定义的函数。
查看hive自带的函数。
show funtion;
查询某个函数的详细使用手册
desc function extended split ;
![](https://img.haomeiwen.com/i4176128/d212106b2e0101c4.png)
UDF几种类型
UDF(User-Defined-Function)
一进一出
UDAF(User-Defined Aggregation Funcation)
聚集函数,多进一出;
类似于:count/max/min
UDTF(User-Defined Table-Generating Functions)
一进多出;
如lateral view explore()
UDF自定步骤
继承UDF ->实现evaluste方法
Creating Custom UDFs
First, you need to create a new class that extends UDF, with one or more methods named evaluate.
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;
public final class MyLower extends UDF {
public Text evaluate(final Text s) {
if (s == null) { return null; }
return new Text(s.toString().toLowerCase());
}
}
2:打开eclipse创建maven工程并添加依赖
hive-client依赖jar包。
![](https://img.haomeiwen.com/i4176128/9fb4ea9d886e0d96.png)
3、把MyLower 类打个jar包
4、(方式1)进入hive客户端,添加jar包:hive>add jar /opt/datas/MyLower.jar;
5、创建临时函数:
hive>CREATE TEMPORARY FUNCTION MyLower AS 'com.example.hive.udf.MyLower ';
6、show function;
7、使用 hive>select empno,mylower(ename) from emp ;
(方式2)
CREATE FUNCTION myfunc AS '类路径' USING JAR 'hdfs:///path/to/jar';
![](https://img.haomeiwen.com/i4176128/4b7c7c72ebb0f929.png)
例子:
![](https://img.haomeiwen.com/i4176128/4d3634a75f45dc71.png)
网友评论