HIVE自定义函数
Posted 李杰然
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HIVE自定义函数相关的知识,希望对你有一定的参考价值。
UDF 操作单个数据行,产生单个数据行;
1.
[[email protected] hhh]$ vi TimeFormat.java
import java.sql.Date;
import java.text.SimpleDateFormat;
import org.apache.hadoop.hive.ql.exec.UDF;
public class TimeFormat extends UDF {
public String evaluate(String num){
Date d=new Date(Long.decode(num));
SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:MM:SS");
return sdf.format(d) ;
}
}
2.打包
[[email protected] hhh]$ /usr/jdk1.7.0_25/bin/javac -classpath /home/hadoop/hadoop-0.20.2-cdh3u5/hadoop-core-0.20.2-cdh3u5.jar:/home/hadoop/hive-0.9.0-bin/lib/hive-exec-0.9.0.jar TimeFormat.java
[[email protected] hhh]$ vi main.mf
Manifest-Version: 1.0
[[email protected] hhh]$ /usr/jdk1.7.0_25/bin/jar cvfm TF.jar main.mf TimeFormat.class
3.
hive> add jar /home/hadoop/hhh/TF.jar;
hive> CREATE TEMPORARY FUNCTION TFF AS ‘TimeFormat‘;
create table ss(id bigint)
row format delimited
fields terminated by ‘\t‘
stored as textfile;
hive> load data local inpath ‘/home/hadoop/c.txt‘ into table ss;
[[email protected] ~]$ date +%s (显示当前时间 转换成秒)
(时间戳为 把时间转换成秒 *1000 变为 毫秒点位)
[[email protected] ~]$ vi c.txt
1417792627000
TFF把时间戳 转换为 ("yyyy-MM-dd HH:MM:SS")
hive> select TFF(time) from ha;
-----------------------------------------------
[[email protected] hhh]$ vi hello.java
import org.apache.hadoop.hive.ql.exec.UDF;
public class hello extends UDF {
public String evaluate(String str) {
try {
return "HelloWorld " + str;
} catch (Exception e) {
return null;
}
}
}
2.打包
[[email protected] hhh]$ /usr/jdk1.7.0_25/bin/javac -classpath /home/hadoop/hadoop-0.20.2-cdh3u5/hadoop-core-0.20.2-cdh3u5.jar:/home/hadoop/hive-0.9.0-bin/lib/hive-exec-0.9.0.jar hello.java
[[email protected] hhh]$ vi main.mf
Manifest-Version: 1.0
[[email protected]h91 hhh]$ /usr/jdk1.7.0_25/bin/jar cvfm H.jar main.mf hello.class
3.
hive> add jar /home/hadoop/hhh/H.jar;
hive> CREATE TEMPORARY FUNCTION HH AS ‘hello‘;
hive> select HH(name) from ha;
以上是关于HIVE自定义函数的主要内容,如果未能解决你的问题,请参考以下文章