Hive 时间转换函数使用心得
导语:Hive sql 与传统的 oracle 或者mysql 的时间转换函数有一些不同,对于想将传统数据库迁移到hdfs 用 hive sql 进行处理的任务,如何用 hive sql 实现传统数据库sql 时间转换函数,是一个必须要解决的问题。
【客户案例背景】
腾讯云大数据的一个客户,将oracle数据迁移到 hdfs ,做离线大数据处理。 数据处理过程中,会采用 hive sql 去实现 oracle sql 的一些相同功能。
本次案例,客户想要取得时间:月份减一个月。被修改的时间字段是 “年-月”格式的,效果就是2015-09,减一个月得到2015-08。如果用oracle去做,就很简单,直接调用三个函数:
SQL> select to_char(add_months(to_date('2016-09','yyyy-mm'),-1),'yyyy-mm') from dual;
------------------------------
2016-08
客户想要在 hive 里面实现上述 oracle sql 的同样效果,必须要满足以下两个条件:
(1) 首先,hive 里面得有函数识别 “年-月”这种形式的时间格式;
(2) 然后,还得能够有函数能够实现 “年-月”时间格式的 “+1”或者“-1”功能;
而最难之处在于: hive 的时间函数 “无法返回到月份级别”。
(1) 首先,hive里面的to_date函数:日期时间转日期函数: to_date语法: to_date(string timestamp)
返回值: string
说明: 返回日期时间字段中的日期部分。只能识别到 “年-月-日” 级别的时间,无法识别 “年-月” 级别的时间。
举例:
hive> select to_date('2016-09-10');
OK
2016-09-10
Time taken: 0.048 seconds, Fetched: 1 row(s)
hive> select to_date('2016-09');
OK
NULL
Time taken: 0.045 seconds, Fetched: 1 row(s)
(2) 其次,add_months 函数也无法识别 月份 级别的时间:add_months(string start_date, int num_months)
hive> select add_months('2016-09-10',-1);
OK
2016-08-10
Time taken: 0.039 seconds, Fetched: 1 row(s)
hive> select add_months('2016-09',-1);
OK
NULL
Time taken: 0.042 seconds, Fetched: 1 row(s)
add_months只能识别到 “年-月-日” 级别的时间,无法识别 “年-月” 级别的时间。
(3)最后,hive 没有 to_char函数。
使用 date_sub 函数也不行: 无论是 date_sub 函数还是 cast函数都无法识别是 “年-月” 级别的时间格式。
hive> select date_sub('2016-09',30);
OK
NULL
Time taken: 0.055 seconds, Fetched: 1 row(s)
hive> select date_sub('2016-09-10',30);
OK
2016-08-11
Time taken: 0.042 seconds, Fetched: 1 row(s)
hive> select date_sub(cast('2016-09' as date),30);
OK
NULL
Time taken: 0.046 seconds, Fetched: 1 row(s)
hive> select date_sub(cast('2016-09-10' as date),30);
OK
2016-08-11
Time taken: 0.04 seconds, Fetched: 1 row(s)
我采用的方法是:
select from_unixtime((unix_timestamp('2015-09','yyyy-MM')-1296000),'yyyy-MM');
OK
2015-08
Time taken: 0.082 seconds, Fetched: 1 row(s)
解释一下原理:
unix_timestamp(string date, string pattern) 函数表示把 对应格式的时间 转换为 一个整数(这个整数表示 1970-01-01 00:00:00 到 指定时间的经历的秒数),然后减去1296000(表示半个月15天的总秒数)。
然后调用 from_unixtime 函数 ,将上面计算得到的整数转换为 ‘yyyy-MM’月份形式
【下面详细介绍hive 常见的时间转换函数】
注: 以下的 sql 语句,没有以 “from dual”结尾。某些 hive 版本可能需要在 sql 语句结尾加上from dual。
1、from_unixtime
日期函数UNIX时间戳转日期函数: from_unixtime语法: from_unixtime(bigint unixtime[, string format])
返回值: string
说明: 转化UNIX时间戳(从1970-01-01 00:00:00 UTC到指定时间的秒数)到当前时区的时间格式
举例:
hive> select from_unixtime(1323308943,'yyyy-MM-dd HH:mm:ss');
2011-12-08 09:49:03
hive> select from_unixtime(1323308943,'yyyyMMdd');
20111208
hive> select from_unixtime(1323308943,'yyyy-MM-dd');
2011-12-08
hive> select from_unixtime(1323308943,'yyyy-MM');
2011-12
可以识别到 月 一级的时间
2、unix_timestamp: 三种使用方法:unix_timestamp(), unix_timestamp(string date), unix_timestamp(string date, string pattern)
获取当前UNIX时间戳函数: unix_timestamp语法: unix_timestamp()
返回值: bigint
说明: 获得当前时区的UNIX时间戳
举例:
hive> select unix_timestamp();
1323309615
日期转UNIX时间戳函数: unix_timestamp语法: unix_timestamp(string date)
返回值: bigint
说明: 转换格式为“yyyy-MM-dd HH:mm:ss“的日期到UNIX时间戳。如果转化失败,则返回0。
举例:
hive> select unix_timestamp('2011-12-07 13:01:03');
1323234063
hive> select unix_timestamp('2011-12-07'); 注:这个时候,只能识别 2011-12-07 13:01:03 这种完全格式的时间
NULL
指定格式日期转UNIX时间戳函数: unix_timestamp语法: unix_timestamp(string date, string pattern)
返回值: bigint
说明: 转换pattern格式的日期到UNIX时间戳。如果转化失败,则返回0。
举例:
hive> select unix_timestamp('20111207 13:01:03','yyyyMMdd HH:mm:ss');
1323234063
hive> select unix_timestamp('2011-12-07 13:05','yyyy-MM-dd HH:mm');
1323234300
hive> select unix_timestamp('2011-12','yyyy-MM');
1322668800
注: 这个是可以识别到 月 的时间格式一级的。可以识别到 月 一级的时间
3、date_format
date_format 的语法: date_format(date/timestamp/string ts, string fmt)
hive> select date_format('2015-04-08', 'y');
2015
hive> select date_format('2015-04-08', 'yyyy');
2015
hive> select date_format('2015-04-08', 'yyyy-MM');
2015-04
hive> select date_format('2015-04-08 10:10:01', 'yyyy-MM');
2015-04
hive> select date_format('2015-04-08', 'yyyy-MM-dd');
2015-04-08
可以识别到 月 和 年 一级的时间
4、to_date
日期时间转日期函数: to_date语法: to_date(string timestamp)
返回值: string
说明: 返回日期时间字段中的日期部分。
举例:
hive> select to_date('2011-12-08 10:03:01');
2011-12-08
hive> select to_date('2011-12-08');
2011-12-08
hive> select to_date('2011-12');
NULL
所以 to_date 只能识别到 天 的时间一级
5、year
日期转年函数: year语法: year(string date)
返回值: int
说明: 返回日期中的年。
举例:
hive> select year('2011-12-08 10:03:01');
2011
hive> select year('2012-12-08');
2012
hive> select year('2012-12');
NULL
所以 year 只能识别到 天 的时间一级
6、month
日期转月函数: month语法: month (string date)
返回值: int
说明: 返回日期中的月份。
举例:
hive> select month('2011-12-08 10:03:01');
12
hive> select month('2011-08-08');
8
hive> select month('2011-08');
NULL
所以 month 只能识别到 天 的时间一级
7、day
一样的,day 只能识别到 天 的时间一级
日期转天函数: day语法: day (string date)
返回值: int
说明: 返回日期中的天。
举例:
hive> select day('2011-12-08 10:03:01');
8
hive> select day('2011-12-24');
24
hive> select day('2011-12');
NULL
注: 以下所有函数,都只能识别到 天 的时间一级,不能识别到 月 这么大的时间一级
日期转小时函数: hour语法: hour (string date)
返回值: int
说明: 返回日期中的小时。
举例:
hive> select hour('2011-12-08 10:03:01');
10
日期转分钟函数: minute语法: minute (string date)
返回值: int
说明: 返回日期中的分钟。
举例:
hive> select minute('2011-12-08 10:03:01');
3
日期转秒函数: second语法: second (string date)
返回值: int
说明: 返回日期中的秒。
举例:
hive> select second('2011-12-08 10:03:01');
1
8、weekofyear
日期转周函数: weekofyear语法: weekofyear (string date)
返回值: int
说明: 返回日期在当前的周数。
举例:
hive> select weekofyear('2011-12-08 10:03:01');
49
9、datediff
日期比较函数: datediff语法: datediff(string enddate, string startdate)
返回值: int
说明: 返回结束日期减去开始日期的天数。
举例:
hive> select datediff('2012-12-08','2012-05-09');
213
10、date_add
日期增加函数: date_add语法: date_add(string startdate, int days)
返回值: string
说明: 返回开始日期startdate增加days天后的日期。
举例:
hive> select date_add('2012-12-08',10);
2012-12-18
11、date_sub
日期减少函数: date_sub语法: date_sub (string startdate, int days)
返回值: string
说明: 返回开始日期startdate减少days天后的日期。
举例:
hive> select date_sub('2012-12-08',10) from dual;
2012-11-28
【结论】
hive 所有函数都只能识别到 天 的时间一级,不能识别到 月 这么大的时间一级。但是,from_unixtime 和 unix_timestamp 可以识别到 月 一级的时间。
【附录】
更详细的 Hive 时间函数使用方法,请参考官方使用文档:
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF
- 运行jar包找不到主类
- spring cloud 学习(7) - 生产环境如何不停机热发布?
- jupyter notebook 在mac OS上的安装
- Hadoop(七)HDFS容错机制详解
- java一些常用并发工具示例
- 机器人来“抢工作”了,瑞典人为何能淡定处之
- 线性代数01 线性的大脑
- spring cloud 学习(10) - 利用springfox集成swagger
- Hadoop(六)之HDFS的存储原理(运行原理)
- Blockchain Global CEO Sam Lee:以大数据为基础,区块链技术加快决策生成速度
- Hadoop(五)搭建Hadoop客户端与Java访问HDFS集群
- 被解放的姜戈08 远走高飞
- mxnet安装及NDArray初体验
- MySQL(十五)之数据备份中mysqldump详解
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法