hive UDF操作

创建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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值