您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

Hive的UDF实现详解

2024/3/3 9:48:33发布28次查看
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要一致。
具体例子:
package com.zx.hive.udf;
import org.apache.hadoop.hive.ql.exec.udf;
public class udftestlength extends udf{ public integer evaluate(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;
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product