设为首页 加入收藏

TOP

Hive的UDF实现详解
2014-11-23 17:38:38 来源: 作者: 【 】 浏览:69
Tags:Hive UDF 实现 详解

Hive自身查询语言HQL能完成大部分的功能,但遇到特殊需求时,需要自己写UDF实现。以下是一个完整的案例。

1、eclipse中编写UDF

①项目中加入hive的lib下的所有jar包和Hadoop中share下hadoop-common-2.5.1.jar(Hadoop目前最新版本2.5.1)。
②UDF类要继承org.apache.hadoop.hive.ql.exec.UDF类,类中要实现eva luate。 当我们在hive中使用自定义的UDF的时候,hive会调用类中的eva luate方法来实现特定的功能
③导出项目为jar文件。
注:项目的jdk与集群的jdk要一致。
具体例子:
package com.zx.hive.udf;
import org.apache.hadoop.hive.ql.exec.UDF;
public class UdfTestLength extends UDF{

    public Integer eva luate(String s)
    {
        if(s==null)
        {
            return null;
        }else{
            return s.length();
        }
    }
}
将上面的类打成jar的形式,我使用eclipse直接导出为test-udf.jar包,然后放在/root目录中。

2、自定义函数调用过程

①添加jar包(在hive命令行里面执行)
hive> add jar /root/test-udf.jar;

②创建临时函数 ,hive命令行关闭后,即失效。
hive> create temporary function testlength as ‘com.zx.hive.udf.UdfTestLength';

③调用
hive> select id, name, testlength(name) from student;

④将查询结果保存到HDFS中

hive> create table result row format delimited fields terminated by '\t' as select id, testlength(nation) from student;

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Oracle简单的列转行 下一篇windows下安装MySQL

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: