首先pom文件导入依赖,Hadoop和hive的依赖导入自己机器的版本,hive记得导jdbc

<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.6.</version>
</dependency>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-service</artifactId>
<version>1.2.</version>
</dependency>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>1.2.</version>
</dependency>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-exec</artifactId>
<version>1.2.</version>
</dependency>

新建UDF的Java类

package com.hive.udf;

import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text; import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date; /**
* Created by lenovo on 2017/10/26.
*/
public class DateTransforUDF extends UDF{ public static final SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //匹配讲要格式化的日期格式
public static final SimpleDateFormat outputFormat=new SimpleDateFormat("yyyyMMddHHmmss"); //要格式化的最终格式
/*
2013-02-17 10:38:34
20130217103834
*/ public Text evaluate(final Text input){
Text output=new Text(); if (null==input){
return null;
}
String inputDate = input.toString().trim();
if(null==inputDate){
return null;
}
try {
Date parseDate = inputFormat.parse(inputDate);
String outputDate = outputFormat.format(parseDate);
output.set(outputDate);
} catch (ParseException e) {
e.printStackTrace();
return output;
}
return output;
} public static void main(String[] args) {
System.out.println(new DateTransforUDF().evaluate(new Text("2013-02-17 10:38:34")));
}
}

打jar包,上传到Linux,

hive (register_info)> add jar /root/hive_udf/BaseTest-1.0-SNAPSHOT.jar
> ;
Added [/root/hive_udf/BaseTest-1.0-SNAPSHOT.jar] to class path
Added resources: [/root/hive_udf/BaseTest-1.0-SNAPSHOT.jar]
hive (register_info)> create temporary function Tdate as 'com.test.OnlyDateUDF';
OK
Time taken: 0.006 seconds

最新文章

  1. align使图片和文字居中
  2. EasyUI树和Ztree树冲突问题
  3. 【Python】调用WPS V9 API,实现Word转PDF
  4. css_04之显示、定位
  5. SQL SERVER 与ACCESS、EXCEL的数据转换
  6. 第八篇:web之前端踩的一些坑
  7. 仿新浪微博短网址PHP实现方案
  8. c++内存对齐 转载
  9. Nginx-动态路由升级版
  10. JSP标签c:forEach实例
  11. D. Kuro and GCD and XOR and SUM
  12. python将两个数组合并成一个数组的两种方法的代码
  13. 在mongoose中使用正则,参数为变量时的写法
  14. c# 判断时间是否在 某一时间段内
  15. BLDC
  16. RNN-LSTM-GRU-BIRNN
  17. [Direct2D开发] 绘制网格
  18. Eclipse 常用插件安装(最新更新:2016-12-06)
  19. numpy数组的创建
  20. JSON.parse与eval

热门文章

  1. jquery的push()
  2. 动态注册HttpModule管道,实现global.asax功能
  3. 网络启动并安装Debian
  4. codeforces 427 div.2 F. Roads in the Kingdom
  5. Struts 2 框架特性
  6. selenium驱动Firefox跳转页慢慢慢的问题(待验证)
  7. mysql windows安装
  8. 关于PHP反射
  9. (转)linux设备驱动之USB数据传输分析 一
  10. phpcms的基础知识和配置