Hive的基础函数-日期函数

当前系统时间函数:current_date()、current_timestamp()、unix_timestamp()

复制代码
-- 函数1:current_date();		
	当前系统日期		格式:"yyyy-MM-dd"
-- 函数2:current_timestamp();	
	当前系统时间戳:	格式:"yyyy-MM-dd HH:mm:ss.ms"
-- 函数3:unix_timestamp();	
	当前系统时间戳	格式:距离1970年1月1日0点的秒数。
	select unix_timestamp(); //获取当前的时间戳
  select unix_timestamp('2017/1/21','yyyy/MM/dd') ;


hive (yhdb)> select current_date();
OK
_c0
2023-08-26
Time taken: 1.032 seconds, Fetched: 1 row(s)
hive (yhdb)> select current_timestamp();
OK
_c0
2023-08-26 18:38:02.304
Time taken: 0.404 seconds, Fetched: 1 row(s)

日期转时间戳函数:unix_timestamp()

复制代码
获取当前时间的时间戳
select unix_timestamp();
select unix_timestamp(current_timestamp());

hive (yhdb)> select unix_timestamp();
unix_timestamp(void) is deprecated. Use current_timestamp instead.
unix_timestamp(void) is deprecated. Use current_timestamp instead.
OK
_c0
1693046302
Time taken: 0.448 seconds, Fetched: 1 row(s)
hive (yhdb)> select unix_timestamp('2022-10-01 00:00:00');
OK
_c0
1664582400
Time taken: 0.322 seconds, Fetched: 1 row(s)

hive (yhdb)> select unix_timestamp('2022/10/01');
OK
_c0
NULL
Time taken: 0.473 seconds, Fetched: 1 row(s)
hive (yhdb)> select unix_timestamp('2022/10/01','yyyy/MM/dd');
OK
_c0
1664582400
Time taken: 0.336 seconds, Fetched: 1 row(s)

时间戳转日期函数:from_unixtime

复制代码
根据时间戳,指定输出日期的格式:
hive (yhdb)> select from_unixtime(1693046606);
OK
_c0
2023-08-26 10:43:26
Time taken: 0.39 seconds, Fetched: 1 row(s)
hive (yhdb)> select from_unixtime(1693046606,'yyyy/MM/dd HH~mm~ss');
OK
_c0
2023/08/26 10~43~26
Time taken: 1.367 seconds, Fetched: 1 row(s)

总而言之

select from_unixtime(1724722412+8*3600);

from_unixtime 换算的时间出来之后,和真实的时间相差 8 小时。所以要+8 小时的秒值

计算时间差函数:datediff()、months_between()

复制代码
select datediff('2023-01-01','2023-02-01');

hive (yhdb)> select datediff('2023-01-01','2023-02-01');
OK
_c0
-31
出现负数,说明数据是前面的时间减去后面的时间,相差的天数。

hive (yhdb)> select months_between('2019-12-20','2019-11-01');
OK
_c0
1.61290323
Time taken: 0.3 seconds, Fetched: 1 row(s)
前面的时间减去后面时间的月数 ,可以精确到小数。
日期相加:
select date_add('2023-12-31',3); -- 日期相加  2024-01-03
日期相减
select date_sub('2023-12-31',3);
select add_months('2023-12-31',3); --月份相加 2024-03-31

日期时间分量函数:year()、month()、day()、hour()、minute()、second()

复制代码
select month(current_date());

hive (yhdb)> select month(current_date());
OK
_c0
8
Time taken: 0.395 seconds, Fetched: 1 row(s)
hive (yhdb)> select year(current_date());
OK
_c0
2023
Time taken: 0.374 seconds, Fetched: 1 row(s)
hive (yhdb)> select year('2012-12-12');
OK
_c0
2012
Time taken: 0.395 seconds, Fetched: 1 row(s)

日期定位函数:last_day()、next_day()

复制代码
--月末:
select  last_day(current_date());
--可以求出当前日期的下个星期几
select next_day(current_date(),'thursday');

日期加减函数:date_add()、date_sub()、add_months()

复制代码
select date_add(current_date,1);
select date_sub(current_date,90);
select add_months(current_date,1);

综合练习:

复制代码
--当月第1天
当前月往前推一个月,7月,7月的最后一天,+1
select  date_add(last_day(add_months(current_date,-1)),1);

--下个月第1天:
select date_add(last_day(current_date),1);

dayofmonth(日期): 当前时间是这个月的第几天
select  add_months(date_sub(current_date,dayofmonth(current_date)-1),1);

字符串转日期:to_date()

复制代码
select to_date('2023-07-01');

将日期转为字符串:date_format()

复制代码
select date_format(current_timestamp(),'yyyy-MM-dd HH:mm:ss');
select date_format(current_timestamp(),'yyyy/MM/dd');
select date_format('2017-01-01','yyyy-MM-dd HH:mm:ss');

需求:假如需要将'2024-12-31' --> '2024/12/31'
-- 第一种方案
select date_format('2024-12-31','yyyy~MM~dd');
-- from_unixtime  将一个时间戳转换为某种字符串类型
-- 方案二
select from_unixtime(unix_timestamp('2024-12-31','yyyy-MM-dd'),'yyyy/MM/dd');
相关推荐
武子康16 小时前
大数据-237 离线数仓 - Hive 广告业务实战:ODS→DWD 事件解析、广告明细与转化分析落地
大数据·后端·apache hive
大大大大晴天18 小时前
Flink生产问题排障-Kryo serializer scala extensions are not available
大数据·flink
武子康3 天前
大数据-236 离线数仓 - 会员指标验证、DataX 导出与广告业务 ODS/DWD/ADS 全流程
大数据·后端·apache hive
武子康4 天前
大数据-235 离线数仓 - 实战:Flume+HDFS+Hive 搭建 ODS/DWD/DWS/ADS 会员分析链路
大数据·后端·apache hive
DianSan_ERP4 天前
电商API接口全链路监控:构建坚不可摧的线上运维防线
大数据·运维·网络·人工智能·git·servlet
够快云库4 天前
能源行业非结构化数据治理实战:从数据沼泽到智能资产
大数据·人工智能·机器学习·企业文件安全
AI周红伟4 天前
周红伟:智能体全栈构建实操:OpenClaw部署+Agent Skills+Seedance+RAG从入门到实战
大数据·人工智能·大模型·智能体
B站计算机毕业设计超人4 天前
计算机毕业设计Django+Vue.js高考推荐系统 高考可视化 大数据毕业设计(源码+LW文档+PPT+详细讲解)
大数据·vue.js·hadoop·django·毕业设计·课程设计·推荐算法
计算机程序猿学长4 天前
大数据毕业设计-基于django的音乐网站数据分析管理系统的设计与实现(源码+LW+部署文档+全bao+远程调试+代码讲解等)
大数据·django·课程设计
B站计算机毕业设计超人4 天前
计算机毕业设计Django+Vue.js音乐推荐系统 音乐可视化 大数据毕业设计 (源码+文档+PPT+讲解)
大数据·vue.js·hadoop·python·spark·django·课程设计