首页 > 数据库 > > presto 与 hive的时间转换对比
工作中经常遇到一些时间转换问题:
1) log_date:20200110 需要转换为标准日期,或者与时间戳数据进行比较
2) 工作环境涉及到presto与hive, 利用presto检查查询时速度更快,因此一般需要同时用presto和hive的语法对日期进行转换
因此本篇博文主要对最近用到的时间转换进行梳理
例子: 当前时间20200110 转化为2020-01-10
--输出 2020-01-10
--hive
select to_date(from_unixtime(UNIX_TIMESTAMP('20200110','yyyyMMdd')));
--presto
select (format_datetime(date_parse('20200110','%Y%m%d'),'yyyy-MM-dd') ;
hive string与各种格式时间的转换: [转] String to Date conversion in hive - 在 Hive 中各种字符串转换成日期格式
例子: 原时间为20200110 需先转化为标准日期形式再加减
--hive
select date_add('2020-01-12',10);
select date_add(to_date(from_unixtime(UNIX_TIMESTAMP('20200110','yyyyMMdd'))),10);
--presto
select date_add('day',-6,cast('2020-07-07' as date)); --第三个参数不转换为date格式, 会报错 第三个参数必须为date格式
select
date_add('day', -6, cast(format_datetime(date_parse('20200110','%Y%m%d'),'yyyy-MM-dd') as date));hive中date_add, date_sub, date_diff的用法: HIve中 datediff,date_add和date_sub的用法
date_sub与date_add用法几乎一样
--hive
select from_unixtime(1578585600);
--加格式
select from_unixtime(1578585600,'yyyyMMdd');
--presto
select from_unixtime(1578585600);
--加格式
select format_datetime(from_unixtime(1578585600),'yyyy-MM-dd');--hive
select unix_timestamp('20200110' ,'yyyyMMdd'); --10位时间戳
-- presto
select to_unixtime(cast('2020-01-10' as date));
select to_unixtime(cast(format_datetime(date_parse('20200110','%Y%m%d'),'yyyy-MM-dd') as date));--hive
select datediff('2017-09-15','2017-09-01');-- 结果14
--presto
select date_diff('day',cast('2018-09-05' as date),cast('2018-09-07' as date));
-- 1)需要提供参数'day',表示要查询的是天数间隔;要查询小时,则提供参数'hour'
-- 2)并且后面传参限制为date类型;
-- 3)最后要注意是后面减去前面 --与hive不同--hive
select current_date;
select unix_timestamp(); --获取当前时间戳
select from_unixtime(unix_timestamp());
-- presto
select now(); --精确到今天的时分秒
select current_date; --精确到今天的年月日
select current_date - interval '1' day; 精确到昨天的年月日
--hive其它日期查询
select current_timestamp; --查询当前系统时间(包括毫秒数);
select dayofmonth(current_date); -- 查询当月第几天
select last_day(current_date); --月末
select date_sub(current_date,dayofmonth(current_date)-1); --当月第1天:
select add_months(date_sub(current_date,dayofmonth(current_date)-1),1); --下个月第1天
presto time官网介绍:https://prestodb.io/docs/current/functions/datetime.html
其它参考: Hive中日期函数总结_qq646748739的专栏-CSDN博客_hive datetime
来源 https://zhuanlan.zhihu.com/p/164645100
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69999831/viewspace-3130933/ ,如需转载,请注明出处,否则将追究法律责任。