hive 自定义UDF实现时间字段格式转换

1.编写类继承UDF

package beifengly.Hive20;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;


import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;






/**
 * 转换日期格式
 * 转入前:"31/Aug/2015:00:04:37 +0800"
 * 转入后:2015-08-31 00:04:37
 * 
 */
public class testdate extends UDF{
//定义输入,传入的日期格式
public SimpleDateFormat inputDate =new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss", Locale.ENGLISH);
//定义输出,输出日期格式
public SimpleDateFormat outputDate =new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
public  Text evaluate(Text time){

String output=null;

//判断time是不是null
if (time==null){
return null;
}
//判断字符串中的值是否符合
if(StringUtils.isBlank(time.toString())){
return null;
}

//去除引号
String prase=time.toString().replaceAll("\"", "");
try {
Date praseDate=inputDate.parse(prase);
//将读入的数据进行格式化转换
 output =outputDate.format(praseDate);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}



return new Text(output);

}
public static void main(String[] args) {
System.out.println(new testdate().evaluate(new Text("31/Aug/2015:00:04:37 +0800")) );
}

}

2.打jar包,并上传到linux指定目录(/opt/datas/)目录根据自己的确定不是固定

3.添加jar包:add jar /opt/datas/testdate.jar;

4.创建临时函数:

create temporary function pdate as 'beifengly.Hive20.testdate';(as ‘包名 . 类名’)

5.查看函数是否创建成功:show functions;

6.使用函数:select pdate(time_local)from bf_log limit 10;(time_local 是日志的时间字段,并保存在bf_log表中)

7.结果:


扫描二维码关注公众号,回复: 1447193 查看本文章

猜你喜欢

转载自blog.csdn.net/qq_36567024/article/details/79413606