Home > Database > Mysql Tutorial > body text

Hive的UDF实现详解

WBOY
Release: 2016-06-07 16:10:29
Original
1886 people have browsed it

Hive自身查询语言HQL能完成大部分的功能,但遇到特殊需求时,需要自己写UDF实现。以下是一个完整的案例。 1、eclipse中编写UDF ①项目中加入hive的lib下的所有jar包和Hadoop中share下hadoop-common-2.5.1.jar(Hadoop目前最新版本2.5.1)。 ②UDF类要继承org

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类,类中要实现evaluate。 当我们在hive中使用自定义的UDF的时候,hive会调用类中的evaluate方法来实现特定的功能
③导出项目为jar文件。
注:项目的jdk与集群的jdk要一致。
具体例子:
<span style="font-family: Arial, Helvetica, sans-serif;">package com.zx.hive.udf;
</span>
Copy after login
<span style="font-family: Arial, Helvetica, sans-serif;">import org.apache.hadoop.hive.ql.exec.UDF;</span>
Copy after login
public class UdfTestLength extends UDF{

    public Integer evaluate(String s)
    {
        if(s==null)
        {
            return null;
        }else{
            return s.length();
        }
    }
}
Copy after login
将上面的类打成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;

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!