重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇文章主要介绍Hive如何自定义函数实现将自定义函数注册到hive源码中并重新编译hive,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
创新互联公司主要从事网站制作、网站设计、网页设计、企业做网站、公司建网站等业务。立足成都服务江口,10余年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:028-86922220
hive版本:
hive-1.1.0-cdh6.7.0
1 编写UDF函数
1.1 用户idea+maven创建项目,pom.xml文件的配置如下:
1.2 下载相关包:右键maven项目,maven->Reimport,就可自动下载jar包了,网络不好的话有点慢,
1.3 创建类,右键包名:New->java Class 新建HelloUDF.java类
package com.ruozedata.bigdata;
import org.apache.hadoop.hive.ql.exec.Description;
import org.apache.hadoop.hive.ql.exec.UDF;
@Description(
name="sayhelloudf",
value="_FUNC_(input_str) - returns Hello : input_str or _FUNC_(input_str,input_str2) - returns Hello : input_str:input_str2 ",
extended = "Example:\n"+
" > SELECT _FUNC_('xx') FROM dual LIMIT 1;\n" +
" Hello : xx "
)
public class HelloUDF extends UDF {
public String evaluate(String input){
return "Hello:" + input;
}
public String evaluate(String input, String input2){
return "Hello:" + input + " : " + input2;
}
}
2 下载hive-1.1.0-cdh6.7.0-src.tar.gz源码文件
http://archive.cloudera.com/cdh6/cdh/5/hive-1.1.0-cdh6.7.0-src.tar.gz
[hadoop@hadoop002 software]$ pwd
/home/hadoop/software
[hadoop@hadoop002 software]$ ll
total 499272
drwxrwxr-x. 17 hadoop hadoop 4096 Mar 24 2016 hadoop-2.6.0-cdh6.7.0
-rw-rw-r--. 1 hadoop hadoop 42610549 Jul 5 15:05 hadoop-2.6.0-cdh6.7.0-src.tar.gz
-rw-rw-r--. 1 hadoop hadoop 311585484 Feb 20 07:16 hadoop-2.6.0-cdh6.7.0.tar.gz
-rw-rw-r--. 1 hadoop hadoop 14652104 Feb 21 05:28 hive-1.1.0-cdh6.7.0-src.tar.gz
-rw-rw-r--. 1 hadoop hadoop 116082695 Feb 21 05:28 hive-1.1.0-cdh6.7.0.tar.gz
-rw-rw-r--. 1 hadoop hadoop 29966286 Feb 22 02:28 sqoop-1.4.6-cdh6.7.0.tar.gz
解压到当前目录:
[hadoop@hadoop002 software]$ tar -zxvf hive-1.1.0-cdh6.7.0-src.tar.gz
[hadoop@hadoop002 software]$ ll
total 503368
drwxrwxr-x. 17 hadoop hadoop 4096 Mar 24 2016 hadoop-2.6.0-cdh6.7.0
-rw-rw-r--. 1 hadoop hadoop 42610549 Jul 5 15:05 hadoop-2.6.0-cdh6.7.0-src.tar.gz
-rw-rw-r--. 1 hadoop hadoop 311585484 Feb 20 07:16 hadoop-2.6.0-cdh6.7.0.tar.gz
drwxrwxr-x. 32 hadoop hadoop 4096 Jul 5 15:15 hive-1.1.0-cdh6.7.0
-rw-rw-r--. 1 hadoop hadoop 14652104 Feb 21 05:28 hive-1.1.0-cdh6.7.0-src.tar.gz
-rw-rw-r--. 1 hadoop hadoop 116082695 Feb 21 05:28 hive-1.1.0-cdh6.7.0.tar.gz
-rw-rw-r--. 1 hadoop hadoop 29966286 Feb 22 02:28 sqoop-1.4.6-cdh6.7.0.tar.gz
3 将HelloUDF.java类添加到源码
3.1 将HelloUDF.java类上传到/home/hadoop/software/hive-1.1.0-cdh6.7.0/ql/src/java/org/apache/hadoop/hive/ql/udf 目录。
3.2 修改HelloUDF.java类的包:将 package com.ruozedata.bigdata; 修改为package org.apache.hadoop.hive.ql.udf;
3.3 修改FunctionRegistry.java类文件,对HelloUDF.java类进行注册:
3.3.1 引入HelloUDF.java类文件的路径:
import org.apache.hadoop.hive.ql.udf.HelloUDF;
3.3.2 在 static 块中添加:system.registerUDF("helloudf", HelloUDF.class, false);
如下:
static {
system.registerGenericUDF("concat", GenericUDFConcat.class);
system.registerUDF("helloudf", HelloUDF.class, false);
.
.
.
}
4 编译 hive-1.1.0-cdh6.7.0
4.1 进入hive-1.1.0-cdh6.7.0目录进行源码编译:
[hadoop@hadoop002 software]$ cd hive-1.1.0-cdh6.7.0
[hadoop@hadoop002 hive-1.1.0-cdh6.7.0]$ mvn clean package -DskipTests -Phadoop-2 -Pdist
出现如下日志,表示编译成功
[INFO] --- build-helper-maven-plugin:1.8:attach-artifact (attach-jdbc-driver) @ hive-packaging ---
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] Hive ............................................... SUCCESS [ 9.558 s]
[INFO] Hive Shims Common .................................. SUCCESS [ 14.763 s]
[INFO] Hive Shims 0.23 .................................... SUCCESS [ 7.488 s]
[INFO] Hive Shims Scheduler ............................... SUCCESS [ 4.449 s]
[INFO] Hive Shims ......................................... SUCCESS [ 2.784 s]
[INFO] Hive Common ........................................ SUCCESS [ 14.109 s]
[INFO] Hive Serde ......................................... SUCCESS [ 12.877 s]
[INFO] Hive Metastore ..................................... SUCCESS [ 39.906 s]
[INFO] Hive Ant Utilities ................................. SUCCESS [ 3.377 s]
[INFO] Spark Remote Client ................................ SUCCESS [ 13.578 s]
[INFO] Hive Query Language ................................ SUCCESS [02:22 min]
[INFO] Hive Service ....................................... SUCCESS [01:14 min]
[INFO] Hive Accumulo Handler .............................. SUCCESS [ 36.593 s]
[INFO] Hive JDBC .......................................... SUCCESS [01:31 min]
[INFO] Hive Beeline ....................................... SUCCESS [ 8.103 s]
[INFO] Hive CLI ........................................... SUCCESS [ 8.419 s]
[INFO] Hive Contrib ....................................... SUCCESS [ 7.041 s]
[INFO] Hive HBase Handler ................................. SUCCESS [01:29 min]
[INFO] Hive HCatalog ...................................... SUCCESS [ 31.376 s]
[INFO] Hive HCatalog Core ................................. SUCCESS [ 13.366 s]
[INFO] Hive HCatalog Pig Adapter .......................... SUCCESS [ 10.937 s]
[INFO] Hive HCatalog Server Extensions .................... SUCCESS [ 32.887 s]
[INFO] Hive HCatalog Webhcat Java Client .................. SUCCESS [ 6.679 s]
[INFO] Hive HCatalog Webhcat .............................. SUCCESS [ 41.266 s]
[INFO] Hive HCatalog Streaming ............................ SUCCESS [ 6.445 s]
[INFO] Hive HWI ........................................... SUCCESS [ 5.169 s]
[INFO] Hive ODBC .......................................... SUCCESS [ 3.305 s]
[INFO] Hive Shims Aggregator .............................. SUCCESS [ 1.139 s]
[INFO] Hive TestUtils ..................................... SUCCESS [ 1.869 s]
[INFO] Hive Packaging ..................................... SUCCESS [01:19 min]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 13:37 min
[INFO] Finished at: 2018-07-05T16:02:01+08:00
[INFO] Final Memory: 158M/494M
[INFO] ------------------------------------------------------------------------
4.2 编译成功后会生成一个tar包:/home/hadoop/software/hive-1.1.0-cdh6.7.0/packaging/target/apache-hive-1.1.0-cdh6.7.0-bin.tar.gz
5 部署hive-1.1.0-cdh6.7.0
5.1 直接将编译好的apache-hive-1.1.0-cdh6.7.0-bin.tar.gz进行安装,见我前面写的一篇博客:hive-1.1.0-cdh6.7.0 安装 http://blog.itpub.net/29609890/viewspace-2155488/
5.2 替换hive-exec-1.1.0-cdh6.7.0.jar包。 在编译好的文件中找到hive-exec-1.1.0-cdh6.7.0.jar包,去替换现在使用的hadoop-2.6.0-cdh6.7.0的hive-exec-1.1.0-cdh6.7.0.jar包
编译好的hive-exec-1.1.0-cdh6.7.0.jar包路径:/home/hadoop/software/hive-1.1.0-cdh6.7.0/packaging/target/apache-hive-1.1.0-cdh6.7.0-bin/apache-hive-1.1.0-cdh6.7.0-bin/lib/
现在使用的hadoop-2.6.0-cdh6.7.0的hive-exec-1.1.0-cdh6.7.0.jar包路径:/home/hadoop/app/hive-1.1.0-cdh6.7.0/lib/hive-exec-1.1.0-cdh6.7.0.jar
切换到现在使用的hive目录:
[hadoop@hadoop002 ~]$ cd ~/app/hive-1.1.0-cdh6.7.0/
[hadoop@hadoop002 hive-1.1.0-cdh6.7.0]$ pwd
/home/hadoop/app/hive-1.1.0-cdh6.7.0
[hadoop@hadoop002 hive-1.1.0-cdh6.7.0]$
备份现在使用的jar包:
/home/hadoop/app/hive-1.1.0-cdh6.7.0/lib/
[hadoop@hadoop002 hive-1.1.0-cdh6.7.0]# mv hive-exec-1.1.0-cdh6.7.0.jar hive-exec-1.1.0-cdh6.7.0.jar.bak
拷贝到原hive 部署位置:
[hadoop@hadoop002 hive-1.1.0-cdh6.7.0]# cp /home/hadoop/software/hive-1.1.0-cdh6.7.0/packaging/target/apache-hive-1.1.0-cdh6.7.0-bin/apache-hive-1.1.0-cdh6.7.0-bin/lib/hive-exec-1.1.0-cdh6.7.0.jar ./
查看hive-exec-1.1.0-cdh6.7.0.jar包
[hadoop@hadoop002 lib]$ ll hive-exec-1.1.0-cdh6.7.0.*
-rw-rw-r--. 1 hadoop hadoop 19274963 Jul 5 17:44 hive-exec-1.1.0-cdh6.7.0.jar
-rw-r--r--. 1 hadoop hadoop 19272159 Mar 24 2016 hive-exec-1.1.0-cdh6.7.0.jar.bak
6 重启hive进行验证:
hive> desc function sayhelloudf;
OK
sayhelloudf(input_str) - returns Hello : input_str or sayhelloudf(input_str,input_str2) - returns Hello : input_str:input_str2
Time taken: 0.031 seconds, Fetched: 1 row(s)
hive> select sayhelloudf('zhangsan') from ruozedata.dual;
OK
Hello:zhangsan
Time taken: 2.531 seconds, Fetched: 1 row(s)
hive> select sayhelloudf('zhangsan','lisi') from ruozedata.dual;
OK
Hello:zhangsan : lisi
Time taken: 0.397 seconds, Fetched: 1 row(s)
hive>
以上是“Hive如何自定义函数实现将自定义函数注册到hive源码中并重新编译hive”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!