Hive时间日期函数一文详解+代码实例


目录

前言

一、HiveSQL运行过程

二、Hive时间函数

1.获取当前时间

1.current_date()

2. current_timestamp()

3. unix_timestamp()

2.获取指定时间维度

1. year()

2.quarter()

3.month()

4.day()

5.hour()

6.minute()

7.second

8.weekofyear()

9. dayofweek()

 10.last_day()

 11.next_day()

12.trunc()

 3.时间格式转换

 1.to_date()

2. from_unixtime()

3.date_format

 4.unix_timestamp

5. from_utc_timestamp/to_utc_timestamp

6. to_unix_timestamp

 4.时间运算

 1.datediff()

 2.date_sub()

 3.date_add()

 4.months_between()

点关注,防走丢,如有纰漏之处,请留言指教,非常感谢


前言

Hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张表,并提供类SQL查询功能。Hive中的表示纯逻辑表,只有表的定义等,即表的元数据(存储于MySQL中)。本质就是Hadoop的目录/文件,这种设计方式实现了元数据与数据存储分离。Hive本身不存储数据,它完全依赖HDFS和MapReduce。

时间在数据库中经常作为时间索引,在数据入库和出库以及更新的时候都需要变化。在一些指标计算或者是提取某段时间的数据时,都会根据数据库中的时间索引数据进行操作。因此很大一部分我们操作数据都得先从时间数据下手,但是想要真正提取到我们想要的时间作为索引,还需要我们掌握许多功能函数方便我们操作,这是一个比较复杂的运用过程。因此特地写下这篇文章,记录一些十分好用常用的处理Hive数据库SQL时间数据的函数,以及实例运用这些函数完成一些复杂查询任务。希望能够帮助到正在看此博文的各位,如果还有什么问题解决不了尽请在评论区提出,博主会一一作答。


一、HiveSQL运行过程

本质上HiveSQL是将sql语句转换为MapReduce程序:

这里不展开细化,直接仅需要知道Hive的SQL是和传统MYSQL和SQL server的SQL语法是不同的就够了,他们之间的语法也是存在很多差异。

二、Hive时间函数

1.获取当前时间

共有以下函数可以获取当前时间:

函数 说明 返回
current_date() 获取当前格式化日期 2022-08-08
current_timestamp() 获取当前格式化日期 2022-08-08 11:03:34.946
unix_timestamp() 获取当前unix时间戳 1659927898

1.current_date()

select current_date();

 

2. current_timestamp()

select current_timestamp();

 

3. unix_timestamp()

select unix_timestamp();

 

2.获取指定时间维度

共有以下函数可以获取指定时间维度:

函数 说明 返回结果
year() 获取日期中的年 2022
quarter() 获取日期中的季度 3
month() 获取日期中的月 8
day() 获取日期中的日 8
hour() 获取日期中的小时 11
minute() 获取日期中的分 32
second() 获取日期中的秒 52
weekofyear() 获取日期在当前年份的第几周 32
dayofweek() 获取日期在当前周的第几天(周日为第一天) 2
last_day() 获取日期当月最后一天 2022-08-31
next_day() 获取当前日期之后的下个星期几的日期 2022-08-15
trunc() 获取日期月初(参数MM),年初日期(参数YY) 2022-08-01,2022-01-01

1. year()

select year(current_date());

2.quarter()

select year(current_date());

3.month()

select month(current_date());

 

4.day()

select day(current_date());

5.hour()

select hour(current_timestamp());

 

6.minute()

select minute(current_timestamp());

 

7.second

select second(current_timestamp());

8.weekofyear()

select weekofyear(current_timestamp());

 

9. dayofweek()

select dayofweek(current_timestamp());

 

 10.last_day()

select last_day(current_timestamp());

 

 11.next_day()

select next_day(current_date(),'MO');

 

12.trunc()

SELECT TRUNC(CURRENT_DATE(),'MM') 

 

SELECT TRUNC(CURRENT_DATE(),'YY') 

 

 3.时间格式转换

函数 说明 返回
to_date() 获取日期时间中日期部分数据 2022-08-08
from_unixtime() unix时间戳到转时间格式 yyyy-MM-dd
date_format() 日期、时间戳、字符串类型格式化输出标准时间格式 yyyy-MM-dd
unix_timestamp() 获取当前时间的unix时间戳和日期转UNIX时间戳函数 1659938033
from_utc_timestamp/to_utc_timestamp() utc时间转换 yyyy-MM-dd
to_unix_timestamp() 日期转unix时间戳 1659938033

 1.to_date()

select to_date(current_timestamp())

 

2. from_unixtime()

select from_unixtime(1659938033,'yyyy-MM-dd')

 

select from_unixtime(1659938033,'yyyyMMdd')

 

select from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:dd:ss');

3.date_format

select date_format(current_timestamp(),'yyyy-MM-dd HH:mm:ss'); 

 

select date_format(current_date(),'yyyy-MM-dd');

 

select date_format('2022-08-09 12:12:12','yyyy-MM-dd HH:mm:ss'); 

 

 4.unix_timestamp

select unix_timestamp();

SELECT unix_timestamp(current_timestamp())

 

5. from_utc_timestamp/to_utc_timestamp

select from_utc_timestamp(current_timestamp(),8);

 

select to_utc_timestamp(current_timestamp(),8);

 

6. to_unix_timestamp

select to_unix_timestamp('2022-08-09 11:10:27','yyyy-MM-dd HH:dd:ss');

 

 4.时间运算

关于时间运算有很多个不同的方法来实现,自定义函数或者是使用函数拼凑获取自己想要的结果就行了。

这里列举几个常用的时间运算函数:

函数 说明 返回
datediff() 日期比较函数,返回开始日期减去结束日期的天数 前者大于后者,返回值为正,否则,返回值为负。
date_sub() 日期减少函数,返回日期前n天的日期 返回日期前n天的日期
date_add() 日期增加函数,返回日期后n天的日期 返回日期后n天的日期
months_between() 返回两个日期之间包含的月数(结果为double类型) double类型月份数值

 1.datediff()

select datediff('2022-08-14','2022-08-04');

 

select datediff('2022-08-04','2022-08-14');

 

 2.date_sub()

SELECT date_sub('2022-08-04',10)

SELECT date_sub('2022-08-04',-10)

 

 3.date_add()

其实和date_sub可以正负号替换,记住一个就行了:

select DATE_ADD('2022-08-04',10) 

 

 4.months_between()

select months_between('2022-08-04','2022-08-14')

 

点关注,防走丢,如有纰漏之处,请留言指教,非常感谢

以上就是本期全部内容。我是fanstuck ,有问题大家随时留言讨论 ,我们下期见。

猜你喜欢

转载自blog.csdn.net/master_hunter/article/details/126222842
今日推荐