[英]Need to subtract some hours from given timestamp in hive
Input: unix_timestamp('01/15/2018 15:26:37', 'mm/dd/YYYY hh:mm:ss')输入:unix_timestamp('01/15/2018 15:26:37', 'mm/dd/YYYY hh:mm:ss')
Expected output is 4 hours delay from above utc input time ie 01/15/2018 11:26:37预计 output 比 utc 输入时间延迟 4 小时,即 01/15/2018 11:26:37
I know that there is date_sub function in hive but it is only used to subtract days from the given timestamp.我知道 hive 中有 date_sub function 但它仅用于从给定时间戳中减去天数。 But I need to know if there is a way by which I can subtract hours or minutes or seconds.
但我需要知道是否有一种方法可以减去小时、分钟或秒。
I have also tried something like below as EDT timezone is 4 hours behind UTC (but getting wrong output):我也尝试过类似下面的内容,因为 EDT 时区比 UTC 晚 4 小时(但输出错误):
SELECT to_date(from_UTC_timestamp(unix_timestamp('01/15/2018 15:26:37', 'mm/dd/YYYY hh:mm:ss')*1000, 'EST6EDT')) as earliest_date; -- OUTPUT: 2017-12-31 (wrong)
So can anyone help me out with this?那么有人可以帮我解决这个问题吗?
它工作正常。
select from_unixtime(unix_timestamp('01/15/2018 15:26:37', 'MM/dd/yyyy HH:mm:ss')-4*3600, 'MM/dd/yyyy HH:mm:ss')
In addition to the answer of @StrongYoung . 除了@StrongYoung的答案 。
I find it very useful to define such long expressions as a macros and place in the initialization file (eg hive -i init-file.hql ...
). 我发现将这样的长表达式定义为宏并放置在初始化文件中非常有用(例如
hive -i init-file.hql ...
)。
hive> create temporary macro sub_hours(dt string, hours int)
from_unixtime(unix_timestamp(dt, 'MM/dd/yyyy HH:mm:ss') - 3600 * hours, 'MM/dd/yyyy HH:mm:ss');
OK
Time taken: 0.005 seconds
hive> select sub_hours("01/01/2019 00:00:00", 5);
OK
12/31/2018 19:00:00
Time taken: 0.439 seconds, Fetched: 1 row(s)
Macros are available starting from Hive 0.12.0, further details can be found here (pay attention to the "bug fixes" section). 宏可以从Hive 0.12.0开始提供,更多详细信息可以在这里找到(注意“错误修复”部分)。
Eg Your current time in your specific timezone - 1 hour:例如,您在特定时区的当前时间 - 1 小时:
select date_format(
from_utc_timestamp(CURRENT_TIMESTAMP(),'Europe/Madrid') - INTERVAL 1 hours,
'yyyy-MM-dd HH:mm:ss')
as PREVIOUS_HOUR
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.