Hive中自定义函数的实现

1、Hive自定义函数的实现细节

1).自定义UDF需要继承:org.apache.hadoop.hive.ql.UDF
2).需要evaluate函数,evaluate函数支持重载。

2、Hive自定义函数的部署运行

1).把程序打包放到目标机器上去
2).进入hive客户端,添加jar包:
hive> add jar /home/sfd/udf_test.jar
3).创建临时函数:
hive> create temporary function <函数名>
    > as 'java全类名';
4).销毁临时函数:
hive> drop temporary function <函数名>;

3、Hive自定义函数的使用

hive> select <函数名> from table;

实例:实现函数content(’hello‘,’world‘)结果为:hellow*****world。

1.函数代码很简单:

package com.sfd.UDF;

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

public class ConcatString extends UDF{

    public Text evaluate(Text a,Text b){
        return new Text(a.toString()+"*****"+b.toString());
    }   
}

2.打成jar包(我起的名字为UTFC.jar);并添加到hive中去;

hive> add jar /home/sfd/UDFC.jar; 

3.定义临时函数content:

hive> create temporary function content as 'com.sfd.UDF.ConcatString';

4.使用:

hive> select content('hello','world');

结果为:
hello*****world

发布了56 篇原创文章 · 获赞 31 · 访问量 24万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章