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,大家都懂什么意思了吧
原文地址:https://www.cnblogs.com/tnsay/p/5752633.html