Hive中日期处理

作者: 一刀YiDao | 来源:发表于2016-08-25 15:31 被阅读19153次

1、日期函数UNIX时间戳转日期函数:from_unixtime()

函数 格式 返回值 说明
from_unixtime from_unixtime(bigint unixtime[, string format]) string 转化UNIX时间戳(从1970-01-01 00:00:00 UTC到指定时间的秒数)到当前时区的时间格式
hive (temp)> select from_unixtime(1323308943,'yyyyMMdd') from dual;
20111208
hive (temp)> select from_unixtime(1323308943,'yyyy-MM-dd') from dual;
2011-12-08

2、当前UNIX时间戳函数: unix_timestamp()

2.1 获取当前UNIX时间戳函数

函数 格式 返回值 说明
unix_timestamp unix_timestamp() bigint 获得当前时区的UNIX时间戳
hive (temp)> select unix_timestamp() from dual;
1472105939

2.2 日期转UNIX时间戳函数

函数 格式 返回值 说明
unix_timestamp unix_timestamp(string date) bigint 转换格式为"yyyy-MM-dd HH:mm:ss"的日期到UNIX时间戳。转化失败,则返回0
hive (temp)> select unix_timestamp('2016-08-25 13:02:03') from dual;
1472101323

2.3 指定格式日期转UNIX时间戳函数

函数 格式 返回值 说明
unix_timestamp unix_timestamp(string date, string pattern) bigint 转换格式为"yyyyMMdd HH:mm:ss"的日期到UNIX时间戳。转化失败,则返回0
hive (temp)> select unix_timestamp('20160825 13:02:03','yyyyMMdd HH:mm:ss') from dual;
1472101323

3、日期时间转日期函数: to_date()

函数 格式 返回值 说明
to_date to_date(string timestamp) string 返回日期时间字段中的日期部分
hive (temp)> select to_date('2016-12-08 10:03:01') from dual;
2016-12-08

4、日期转年函数: year()

函数 格式 返回值 说明
year year(string date) int 返回日期中的年
hive (temp)> select year('2016-12-08 10:03:01') from dual;
2016

hive (temp)> select year('2016-12-08') from dual;
2016

5、日期转月函数: month()

函数 格式 返回值 说明
month month(string date) int 返回日期中的月份
hive (temp)> select month('2016-12-08 10:03:01') from dual;
12

hive (temp)> select month('2016-11-08') from dual;
11

6、日期转天函数: day()

函数 格式 返回值 说明
day day(string date) int 返回日期中的天
hive (temp)> select day('2016-12-08 10:03:01') from dual;
8

hive (temp)> select day('2016-11-18') from dual;
18

7、日期转小时函数: hour()

函数 格式 返回值 说明
hour hour(string date) int 返回日期中的小时
hive (temp)> select hour('2016-12-08 10:03:01') from dual;
10

8、日期转分钟函数: minute()

函数 格式 返回值 说明
minute minute(string date) int 返回日期中的分钟
hive (temp)> select minute('2016-12-08 10:03:01') from dual;
3

9、日期转秒函数: second()

函数 格式 返回值 说明
second second(string date) int 返回日期中的秒
hive (temp)> select second('2016-12-08 10:03:01') from dual;
1

10、日期转周函数: weekofyear()

函数 格式 返回值 说明
weekofyear weekofyear(string date) int 返回日期在当前的周数
hive (temp)> select weekofyear('2016-12-08 10:03:01') from dual;
49

11、日期比较函数: datediff(string enddate, string startdate)

函数 格式 返回值 说明
datediff datediff(string enddate, string startdate) int 返回结束日期减去开始日期的天数
hive (temp)> select datediff('2016-12-08','2016-12-02') from dual;
6

12、日期增加函数: date_add(string startdate, int days)

函数 格式 返回值 说明
date_add date_add(string startdate, int days) string 返回开始日期startdate增加days天后的日期
hive (temp)> select date_add('2016-12-08',10) from dual;
2016-12-18

#当前日期为2016-08-25,在此基础上加7天
hive (temp)> select date_add(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),7) from dual;
2016-09-01


13、日期减少函数:date_sub (string startdate, int days)

函数 格式 返回值 说明
date_sub date_sub(string startdate, int days) string 返回开始日期startdate减少days天后的日期
hive (temp)> select date_sub('2016-12-08',10) from dual;
2016-11-28

#当前日期为2016-08-25,在此基础上减7天
hive (temp)> select date_sub(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),7) from dual;
2016-08-18

相关文章

  • hive中的日期处理

    hive自带的时间/日期处理函数 将某种格式的时间转换为Unix时间戳select unix_timestamp(...

  • Hive日期处理

    日期的各种转换和处理, 其中不包含DATE_FORMAT.

  • Hive—date函数详解

    引言   在hive中我们经常需要处理日期数据,hive内置了3个日期函数,其格式只能为yyyy-MM-dd格式或...

  • Hive中日期处理

    1、日期函数UNIX时间戳转日期函数:from_unixtime() 2、当前UNIX时间戳函数: unix_ti...

  • hive yyyyMMdd转yyyy-MM-dd

    在hive中,我们经常需要进行日期的计算,可是,经常会出现这样一种情况,hive仓库中日期的存储格式是yyy...

  • 高频的HIVE查询相关SQL

    高频用到的HIVE查询相关SQL 1 HIVE中,字段是String的时间戳转换为日期格式语句公式:select ...

  • Hive和Spark当中对小文件的处理

    Hive当中对小文件的处理 数仓面试高频考点:【在Hive中如何解析小文件过多问题,指定的是:处理表中数据时,有很...

  • Spark 数据倾斜调优

    1.使用Hive ETL预处理数据 方案适用场景:如果导致数据倾斜的是Hive表。如果该Hive表中的数据本身很不...

  • Hive中yyyymmdd和yyyy-mm-dd日期之间的切换

    原文链接 Hive中yyyymmdd和yyyy-mm-dd日期之间的切换 参考链接

  • hive中 日期格式转化办法:

    hive中 日期格式转化办法: 2014-11-10 和 20141110相互转化的办法: 1.from_unix...

网友评论

    本文标题:Hive中日期处理

    本文链接:https://www.haomeiwen.com/subject/nvkasttx.html