hive 的udf 函数使用
生活随笔
收集整理的這篇文章主要介紹了
hive 的udf 函数使用
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
- 1)依據(jù)課程講解UDF編程案例,完成練習(xí),總結(jié)開發(fā)UDF步驟,代碼貼圖,給予注釋,重點(diǎn)
- 2)更改emp 表中名字的大寫給為小寫。
一:hive 的udf 函數(shù):
1.1 hive UDF 函數(shù)概述:
1. hive自帶了一些函數(shù)比如:max/min 等,但是由于自帶的函數(shù)數(shù)量有限,自己可以定義udf來方便擴(kuò)展。 2. udf 函數(shù)可以直接應(yīng)用于select 語句,對(duì)查詢結(jié)構(gòu)做格式化處理之后,然后再輸出內(nèi)容。1.2 hive 編寫udf函數(shù)的時(shí)候需要注意的地方:
1. 自定義udf函數(shù)需要繼承org.apache.hadoop.hive.ql.UDF 2. 需要實(shí)現(xiàn)evaluate 函數(shù),evaluate 函數(shù)支持重載。 3. udf 必須要有返回類型,可以返回null,但是返回類型不能為void; 4. udf 常用Text/LongWrite 等類型,不推薦使用java類型。1.3 hive 的udf 函數(shù)編寫:
1. 環(huán)境配置處理:更改repository源cd .m2/ mv repository repository.bak 上傳新的repository.tar.gz 包。 tar -zxvf repository.tar.gz 備份原有setting.xml 文件 cp -p setting.xml setting.xml.bak cd /home/hadoop/yangyang/maven/conf cp -p setting.xml setting.xml1.4 更改maven源的配置:
在setting.xml 中<mirrors> ....</mirrors> 之間增加新的源倉庫:<mirror><id>nexus-osc</id><mirrorOf>central</mirrorOf><name>Nexus osc</name><url>http://maven.oschina.net/content/groups/public/</url></mirror> 拷貝新的setting文件到maven 的配置文件中 cp -p .m2/setting.xml /home/hadoop/yangyang/maven/conf1.5 更改eclipse的pom.xml 文件增加:
在原有的<dependencies>....</dependencies> 之間加上hive 的參數(shù):<dependency><groupId>org.apache.hive</groupId><artifactId>hive-jdbc</artifactId><version>0.13.1</version></dependency><dependency><groupId>org.apache.hive</groupId><artifactId>hive-exec</artifactId><version>0.13.1</version></dependency> </dependencies>二: 更改emp 表中名字的大寫給為小寫。
2.1 新建UDF包 編寫lowerudf.java
package org.apache.hadoop.udf;import org.apache.commons.lang.StringUtils; import org.apache.hadoop.hive.ql.exec.UDF; import org.apache.hadoop.io.Text;/*** New UDF classes need to inherit from this UDF class.*/ public class LowerUDF extends UDF{/*** 1. Implement one or more methods named "evaluate" which will be called by Hive.* * 2. "evaluate" should never be a void method. However it can return "null" if needed.*/public Text evaluate(Text str){// input parameter validateif(null == str){return null ;}// validate if(StringUtils.isBlank(str.toString())){return null ;}// lower return new Text(str.toString().toLowerCase()) ;}public static void main(String[] args) {System.out.println(new LowerUDF().evaluate(new Text()));}}2.2 導(dǎo)出jar包 到/home/hadoop/yangyang/hive/jars 下面:
2.3 執(zhí)行l(wèi)owerudf包 增加jar包與hive的關(guān)聯(lián):
add jar /home/hadoop/yangyang/hive/jars/lowerudf.jar create temporary function my_lower as 'org.apache.hadoop.udf.LowerUDF' ; show functions;2.3.1 銷毀臨時(shí)的udf 函數(shù):
drop temporary function add_example; <!--這里指my_lower 函數(shù)-->2.4 執(zhí)行my_lower 函數(shù):
select my_lower(ename) from emp;轉(zhuǎn)載于:https://blog.51cto.com/flyfish225/2097260
總結(jié)
以上是生活随笔為你收集整理的hive 的udf 函数使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: node+express+mongDB实
- 下一篇: neutron DVR