Hive时间函数

无论在哪门语言当中,时间函数显得都是特别重要的.

参考博客:

https://www.cnblogs.com/0xcafedaddy/p/9360492.html

https://www.cnblogs.com/shujuxiong/p/9929297.html

https://www.cnblogs.com/zhange000/p/10002055.html

 

在HIVE当中时间函数不是很复杂,我们总结一下:

在HIVE当中只有两种时间格式:时间戳(timestamp)与字符串时间(unixtime),没有格式化时间,我们只需要记住2个主要

的函数:unix_timestamp()和from_unixtime().

整体转化方式:

 

时间戳转日期函数

string from_unixtime(unix_time, format):通过该函数可以将时间戳按照指定的格式转化成字符串时间.

hive> desc function from_unixtime;   --传入进来的需要是10位的s

from_unixtime(unix_time, format) - returns unix_time in the specified format

示例程序:

hive> select from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:mm:ss');
unix_timestamp(void) is deprecated. Use current_timestamp instead.
OK
2019-07-14 16:40:10
Time taken: 0.089 seconds, Fetched: 1 row(s)
hive> select from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:mm');
unix_timestamp(void) is deprecated. Use current_timestamp instead.
OK
2019-07-14 16:40
Time taken: 0.102 seconds, Fetched: 1 row(s)
hive> select from_unixtime(unix_timestamp(),'yyyy-MM-dd HH');
unix_timestamp(void) is deprecated. Use current_timestamp instead.
OK
2019-07-14 16
Time taken: 0.099 seconds, Fetched: 1 row(s)
hive> select from_unixtime(unix_timestamp(),'yyyy-MM-dd');
unix_timestamp(void) is deprecated. Use current_timestamp instead.
OK
2019-07-14
Time taken: 0.106 seconds, Fetched: 1 row(s)

 

日期转时间戳函数

bigint unix_timestamp()  or unix_timestamp(date[, pattern]):返回时间戳,指格林威治时间1970年1月1日0时0分0秒(北京时

间1970年01月01日08时00分00秒)起到现在的(或指定字符串时间)的总秒数.

hive> desc function extended unix_timestamp;
OK
unix_timestamp(date[, pattern]) - Converts the time to a number

Converts the specified time to number of seconds since 1970-01-01. The unix_timestamp(void) overload is deprecated,

use current_timestamp.

实例程序:

hive> select unix_timestamp(); --1970-01-01到现在的总秒数.
1563082508

hive> select unix_timestamp('1970-01-02 08:0:00'); --1970-01-01到指定字符串时间的总秒数.
86400

hive> select unix_timestamp('1970-01-02 08:0:00','yyyy-MM-dd HH:mm:ss'); --有的时候需要指定指定格式
86400

  

其余常用函数

current_date():返回当前日期 yyyy-MM-dd时间

实例程序:

hive> select current_date();
OK
2020-02-28
Time taken: 0.066 seconds, Fetched: 1 row(s)
hive> select from_unixtime(unix_timestamp(),'yyyy-MM-dd');
unix_timestamp(void) is deprecated. Use current_timestamp instead.
OK
2020-02-28
Time taken: 0.511 seconds, Fetched: 1 row(s)

 

to_date('2018-08-08 17:12:00'):返回指定时间字符串的日期部分.

hive> select to_date('2018-08-08 17:12:00');
OK
2018-08-08
Time taken: 0.061 seconds, Fetched: 1 row(s)
hive> select from_unixtime(unix_timestamp('2018-08-08 17:12:00','yyyy-MM-dd HH:mm:ss'),'yyyy-MM-dd');
OK
2018-08-08
Time taken: 0.055 seconds, Fetched: 1 row(s)

  

int datediff(string enddate,string startdate):计算开始时间startdate到结束时间enddate相差的天数.

hive> select datediff('2018-08-08','2018-08-01') ;
OK
7
Time taken: 0.173 seconds, Fetched: 1 row(s)

 

string date_sub:返回日期前n天的日期.

hive> select date_sub('2015-04-09',4);
OK
2015-04-05
Time taken: 0.055 seconds, Fetched: 1 row(s)

 

string date_add:返回日期后n天的日期

hive> select date_add('2015-04-09',4);
OK
2015-04-13
Time taken: 1.239 seconds, Fetched: 1 row(s)

  

其余常用函数

year:返回日期中的年

select year('2015-04-02 11:32:12');

输出:2015
month:返回日期中的月份

select month('2015-12-02 11:32:12');

输出:12

day:返回日期中的天

select day('2015-04-13 11:32:12');

输出:13

hour:返回日期中的小时

select hour('2015-04-13 11:32:12');

输出:11

minute:返回日期中的分钟

select minute('2015-04-13 11:32:12');

输出:32

second:返回日期中的秒

select second('2015-04-13 11:32:56');

输出:56

 

练习题目 

https://blog.csdn.net/weixin_37536446/article/details/82419601

实例程序:查看一个表上次的DDL时间.

TBLPROPERTIES (
  'mart_name'='dd_edw', 
  'transient_lastDdlTime'='1575619982')
Time taken: 0.15 seconds, Fetched: 26 row(s)
hive> select from_unixtime(1575619982,'yyyy-MM-dd HH:mm:ss');
OK
2019-12-06 16:13:02
Time taken: 0.08 seconds, Fetched: 1 row(s)

  

 

 

 

  

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一只懒得睁眼的猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值