hive日期函数总结

Hive 日期函数

Hive Date Functions

官网地址:https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF#LanguageManualUDF-DateFunctions

函数名返回类型简介
from_unixtime(bigint unixtime[, string format])string将UNIX时间戳转化为日期,可以设定日期格式
unix_timestamp(string date, string pattern)bigint将字符串日期转化为UNIX时间戳(以秒为单位),默认字符串日期格式为yyyy-MM-dd HH:mm:ss,使用其他格式日期需要指明pattern
to_date(string timestamp)prev2.1.0:string
2.1.0 on:date
返回时间戳字符串的日期部分,在2.1.0版本开始返回date类型
year(string date)int获取日期中年份
quarter(date/timestamp/string)int返回1到4范围内的日期(或时间戳或字符串)的所属季度
month(string date)int获取日期中的月份
day(string date)
dayofmonth(date)
int获取日期中的天数
hour(string date)int获取日期中的小时
minute(string date)int获取日期中的分钟
second(string date)int获取日期中的秒
weekofyear(string date)int获取日期在当前年份的周数
extract(field FROM source)int提取source中的field部分 , source必须是日期、时间戳、间隔、字符串时间戳,field可以是天、星期几、小时、分钟、月、季度、秒、周和年
datediff(string enddate, string startdate)int获取两个日期的相差天数,返回是从enddate到startdate的天数
date_add(date/timestamp/string startdate, tinyint/smallint/int days)pre 2.1.0:string
2.1.0 on: date
获取当前传入日期增加days后的日期,days可以是负数
date_sub(date/timestamp/string startdate, tinyint/smallint/int days)prev2.1.0:string
2.1.0 on:date
获取当前传入日期减少days后的日期,days可以是负数
from_utc_timestamp({any primitive type} ts, string timezone)timestamp将UTC中的时间戳转换为给定的时区
to_utc_timestamp({any primitive type} ts, string timezone)timestamp将给定时区中的时间戳
current_datedate返回当前日期
current_timestamptimestamp返回当前时间戳
add_months(string start_date, int num_months, output_date_format)string返回start_date后的num_months的日期,num_months可以是负数,也可以指定output_date_format输出格式
last_day(string date)string返回当前日期所属月份的最后一天
next_day(string start_date, string day_of_week)string返回日期的下个星期几的日期,day_of_week的参数为MO,TU,WE,TH,FR,SA,SU
trunc(string date, string format)string返回当前日期月初(format=MONTH/MON/MM)、年初(format=YEAR/YYYY/YY)
months_between(date1, date2)double返回日期date1和date2之间的月数
date_format(date/timestamp/string ts, string fmt)string日期格式化成fmt格式的字符串

1.日期 与 时间戳 相互转换函数

-- 时间 戳转 日期
select from_unixtime(1639464603); -- 2021-12-14 06:50:03
select from_unixtime(1639464603, 'yyyyMMdd');  --20211214
select from_unixtime(1639464603, 'yyyy-MM-dd');  --2021-12-14

-- 日期 转 时间戳
-- unix_timestamp函数
select unix_timestamp('2021-12-14 13:14:52');   --结果为 1639487692
select unix_timestamp('2021-12-14');  --默认格式为 'yyyy-MM-dd HH:mm:ss', 所以 不符合 结果为 null
select unix_timestamp('2021-12-14', 'yyyy-MM-dd');  --结果为 1639440000
-- to_date函数: 获取日期部分
select to_date('2021-12-14 13:14:52');

-- 自定义格式化日期,可以对接java的simpledateformat等
select date_format('2021-12-14', 'yyyy-MM-dd');


-- 特别地
-- 将不同时区的时间进行转换
/*
	时间戳:表示以(1970-01-01 00:00:00)为起点,到现在的秒数。
	GMT:即格林尼治标准时间,也就是世界时。
	UTC:即协调世界时。UTC现在作为世界标准时间使用。
	时区:全球划分为24个时区(东、西各12个时区),英国(格林尼治天文台旧址)为零时区(GMT+00),中国北京处于东8区,若是英国时间为6点整,则GMT时间为6点整,则北京时间为14点整。
	世界时区英文缩写: 百度
*/
select from_utc_timestamp('1970-01-01 00:00:00', 'PRC'); --结果为 1970-01-01 08:00:00.000000000

select to_utc_timestamp(timestamp '1970-01-30 16:00:00','PST'); --结果为1970-01-31 00:00:00.000000000

2.获取年、季节、月、日、周数、星期几、小时、分钟、秒

-- 获取当前时间
select `current_date`();   
select current_date;    -- 以下的 current_date 都是 '2021-12-14'
select `current_timestamp`();
select current_timestamp;

-- 年
-- 1.获取年份
select year(current_date);  --结果: 2021
select year(`current_timestamp`());
select extract(YEAR from current_date);
-- 2.获取年初
select trunc(current_date, 'YY');  --结果: 2021-01-01
-- 3.获取年末
select date_sub(add_months(trunc(current_date, 'YY'), 12), 1);  --结果: 2021-12-31

-- 季节
-- 1.获取当前日期所属季节
select quarter(current_date); --结果: 4
-- 2.获取季节的开始日期
select concat(year(current_date), '-', substr(concat('0', (quarter(current_date) - 1) * 3 + 1), -2), '-01');  --结果: 2021-10-01
-- 3.获取季节的结束日期
select date_sub(concat(year(current_date), '-', substr(concat('0', quarter(current_date) * 3 + 1), -2), '-01'), 1);  --结果: 2021-12-31

-- 月份
-- 1.获取当前日期所属月份
select month(current_date);  --结果:  12 
select extract(MONTH from current_date);   --结果:  12
-- 2.获取当前月份的开始日期
select trunc(current_date, 'MM');   --结果: 2021-12-01
-- 3.获取当前月份的结束日期
select last_day(current_date);   --结果: 2021-12-31
-- 4.获取两个日期相差月数
select months_between(current_date, '1970-01-01');  -- 有31   --结果: 623.41935484

-- 天数(日)
-- 1.获取日期中的天数
select day(current_date);   --结果: 14
select dayofmonth(current_date);   --结果: 14
select extract(DAY from current_date);   --结果: 14
-- 2.获取两个日期相差天数
select datediff(current_date, '2020-01-01');   --结果: 713
select datediff('2020-01-01', current_date);   --结果: -713

-- 周数
-- 1.获取当前日期所属年份的周数
select weekofyear(current_date);  --结果为50
select weekofyear('2020-01-08');  --结果为2
select extract(WEEK from current_date);
select extract(WEEK from '2020-01-08'); --结果为2
-- 2.获取当前日期所属月份的周数 
select mod(weekofyear(current_date), month(current_date));  --结果为2

-- 星期数
-- 1.获取当前日期所属星期数
select `dayofweek`('2021-12-17');   --周五 结果为6
select `dayofweek`('2021-12-18');   --周六 结果为7
select `dayofweek`('2021-12-19');   --周日 结果为1
select extract(DAYOFWEEK  from '2021-12-17');  --周五 结果为6
select extract(DAYOFWEEK  from '2021-12-18');  --周六 结果为7
select extract(DAYOFWEEK  from '2021-12-19');  --周日 结果为1
select
       case `dayofweek`('2021-12-14')
            when 2 then '周一'
            when 3 then '周二'
            when 4 then '周三'
            when 5 then '周四'
            when 6 then '周五'
            when 7 then '周六'
            when 1 then '周日'
       end;
select
       case pmod(datediff('2021-12-14', '2021-12-13'), 7) --'2021-12-13'是周一
            when 0 then '周一'
            when 1 then '周二'
            when 2 then '周三'
            when 3 then '周四'
            when 4 then '周五'
            when 5 then '周六'
            when 6 then '周日'
       end;
-- 2.获取下一个周几
-- day_of_week的参数为MO,TU,WE,TH,FR,SA,SU
select next_day('2021-12-14', 'SU');  -- 都是以周日为开始,所以下一个周日是2021-12-19
-- 2.获取上一个周几,先获取下一个周几再减7
select date_sub(next_day('2021-12-14', 'SU'), 7);  -- 都是以周日为开始,所以上一个周日是2021-12-12


-- 小时
select hour(current_timestamp);
select hour('1970-01-01 13:14:52');   --结果:  13

-- 分钟
select minute(current_timestamp);
select minute('1970-01-01 13:14:52');  --结果: 14

-- 秒
select second(current_timestamp);
select second('1970-01-01 13:14:52');  --结果: 52

3.日期增减

-- 日期相关
-- 求明天
select date_add(current_date, 1);  --current_date是 2021-12-14 结果是 2021-12-15
select date_sub(current_date, -1);
-- 求昨天
select date_add(current_date, -1);  --current_date是 2021-12-14 结果是 2021-12-13
select date_sub(current_date, 1);
-- 求两天相差多少天
select datediff('2021-12-15', '2021-12-01');  --结果是 14

-- 月份相关
-- 下一个月的day
select add_months('2021-12-14', 1);  --结果是 2022-01-14
select add_months('2021-01-30', 1);  --2月份没有30号,结果是 2021-02-28
-- 上一个月的day
select add_months('2021-12-14', -1);  --结果是 2021-11-14
select add_months('2021-12-31', -1);  --11月份没有31号,结果是 2021-11-30
-- 计算两个日期相隔的月数  -- 不建议该操作
select months_between('2021-01-15', '2021-12-14');  --会将31号等加天数加入,所以结果会有小数,结果是 -10.96774194
select datediff('2021-01-15', '2021-12-14') / 30; --结果是 -11.1 不准确,原因是有的月份是31天
  • 0
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Hive开窗函数是一种用于在查询结果中执行聚合计算的函数。它们可以在分组的数据上执行计算,并返回结果集中的每一行的聚合值。开窗函数可以使用PARTITION BY子句对数据进行分组,并使用ORDER BY子句对数据进行排序。然后,可以在开窗函数内部使用聚合函数(如SUM、AVG、MAX、MIN、COUNT)对每个分组的数据进行计算。\[2\] 下面是一个使用Hive开窗函数的示例: ``` SELECT user_id, create_date, LAG(create_date, 1, '0000-00-00') OVER (PARTITION BY user_id) AS lag_date, LEAD(create_date, 1, '9999-99-99') OVER (PARTITION BY user_id) AS lead_date, FIRST_VALUE(create_date) OVER (PARTITION BY user_id) AS first_date, LAST_VALUE(create_date) OVER (PARTITION BY user_id) AS last_date FROM order_info; ``` 在这个示例中,我们使用了LAG、LEAD、FIRST_VALUE和LAST_VALUE函数来计算每个用户的前一天日期、后一天日期、第一个日期和最后一个日期。这些函数在每个用户分组内进行计算,并返回结果集中的每一行的相应值。\[2\] 除了开窗函数Hive还提供了其他函数,如NVL函数和CASE WHEN THEN ELSE END函数。NVL函数用于处理空值,可以将空值替换为指定的默认值。CASE WHEN THEN ELSE END函数类似于C语言中的case语句,用于根据条件对字段值进行判断和处理。\[1\]\[3\] #### 引用[.reference_title] - *1* *3* [Hive——Hive常用内置函数总结](https://blog.csdn.net/weixin_44606952/article/details/127929532)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [HIVE函数汇总--开窗函数篇](https://blog.csdn.net/weixin_43935266/article/details/122307605)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值