Hive UDF開發 第一個例子


package udf;

import org.apache.hadoop.hive.ql.exec.UDF;

public class helloudf extends UDF{
public String evaluate(String str){
try {
return "HelloWorld " + str;
} catch (Exception e) {
// TODO: handle exception
return null;
}
}
}


上面是一個java project 寫的 udf  程序的意思大家應該都明白

打包成jar: 用fat jar  博客之前發過jar包了,然后導出成jar  傳到Linux本地目錄

進入hive
add jar /home/hadoop/udf_1_fat.jar;    //注意,這個地址為Linux本地地址,不是hdfs地址  傳到hdfs是接收不到的  除非你加上hdfs鏈接(沒有測試)
CREATE TEMPORARY FUNCTION helloworld AS 'udf.helloudf'
show functions  可以看到所有支持的方法,里面會有helloworld
找一個表,里面有點數據的
我這邊找的一個表,我查出來的數據為:
hive> select * from hive1 limit 10;
OK
1 百花家園 張雲龍 18001295207 155 2室1廳1衛 94 2/5層 南北 sina 河北鏈家地
2 中國鐵建原香小鎮 高偉 13501061874 210 3室2廳1衛 115 6/6層 南北 sina 凱利門
3 金泰城麗灣 馬豐收 18210989857 1200 5室3廳3衛 258 18/19層 南北 sina 21世紀
4 戀日綠島 於洪梁 13366126543 430 3室2廳2衛 149 2/4層 南北 sina 如一房產
5 怡海花園恆泰園 史潤生 18401296028 520 2室2廳2衛 136 15/28層 西 sina 我愛我家
6 高教新城觀宇園 張超 18910750910 180 2室1廳1衛 89 14/18層 南北 sina 晨建順房屋
7 東潤楓景 孫心源 13501365333 380 2室1廳1衛 91 1/20層 東南 sina 我愛我家
8 北苑家園望春園 劉韜 18311385981 245 1室1廳1衛 69 17/25層 東南 sina 城承物業
9 中央公園廣場 呂德輝 15010507970 5800 1室0廳0衛 672 2/26層 南 sina 華清永泰
10 上上城第二季 楊瑞平 15001210716 200 4室1廳2衛 155 6/15層 南北 sina 河北鏈家地

字段對應就不說了

下面就是結果了
hive> select helloworld(hive1.large) from hive1 limit 10;
OK
HelloWorld 94
HelloWorld 115
HelloWorld 258
HelloWorld 149
HelloWorld 136
HelloWorld 89
HelloWorld 91
HelloWorld 69
HelloWorld 672
HelloWorld 155
Time taken: 0.131 seconds, Fetched: 10 row(s)
hive> select helloworld(hive1.price) from hive1 limit 10;
OK
HelloWorld 155
HelloWorld 210
HelloWorld 1200
HelloWorld 430
HelloWorld 520
HelloWorld 180
HelloWorld 380
HelloWorld 245
HelloWorld 5800
HelloWorld 200
Time taken: 0.111 seconds, Fetched: 10 row(s)
hive> select helloworld(hive1.id) from hive1 limit 10;
OK
HelloWorld 1
HelloWorld 2
HelloWorld 3
HelloWorld 4
HelloWorld 5
HelloWorld 6
HelloWorld 7
HelloWorld 8
HelloWorld 9
HelloWorld 10
Time taken: 0.115 seconds, Fetched: 10 row(s)


ok,大家都懂什么意思了吧

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM