创建java类(导入对应的依赖包):
public classHiveHello extendsUDF {
public Text evaluate(Text in){
Stringresult="helloWorld"+in;
return new Text(result);
}
}
打成jar:
hive> add jar /script/hive/HelloHive.jar;
创建表:
hive> create table udf_test(name string)row format delimited fields terminated by ' ' stored as textfile;导入数据:
hive> load data local inpath'/script/hive/udf.txt' overwrite into table udf_test;
创建临时函数:
hive> create temporary function syhello as'com.test.udf.HiveHello';
使用函数:
hive> select syhello(name) from udf_test;
使用命令方式来实现上面操作:
[root@hadoop02 hive]# vi execCommend.hql
add jar /script/hive/HelloHive.jar;
drop table IF EXISTS udf_test2;
create table IF NOT EXISTS udf_test2(name string,age int) row formatdelimited fields terminated by ' ' stored as textfile;
load data local inpath'/script/hive/udf_data.txt' overwrite into table udf_test2;
create temporary function sayHello as'com.test.udf.HiveHello';
select sayHello(name),age from udf_test2;
drop temporary function sayHello;
执行上面sql:
[root@hadoop02 bin]# hive -f/script/hive/execCommend.hql ;
Logging initialized using configuration injar:file:/usr/local/hive/lib/hive-common-1.2.1.jar!/hive-log4j.properties
Added [/script/hive/HelloHive.jar] to classpath
Added resources: [/script/hive/HelloHive.jar]
OK
Time taken: 4.123 seconds
OK
Time taken: 1.415 seconds
Loading data to table default.udf_test2
Table default.udf_test2 stats: [numFiles=1,numRows=0, totalSize=29, rawDataSize=0]
OK
Time taken: 1.459 seconds
OK
Time taken: 1.187 seconds
OK
helloWorldzhangsan 45
helloWorldlisi 56
helloWorldlidan 18
Time taken: 1.024 seconds, Fetched: 3 row(s)
OK
Time taken: 0.033 seconds