Hive 时间转换函数使用心得

时间:2022-04-23
本文章向大家介绍Hive 时间转换函数使用心得,主要内容包括【客户案例背景】、【下面详细介绍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