簡體   English   中英

需要從 hive 中的給定時間戳中減去一些小時

[英]Need to subtract some hours from given timestamp in hive

輸入:unix_timestamp('01/15/2018 15:26:37', 'mm/dd/YYYY hh:mm:ss')

預計 output 比 utc 輸入時間延遲 4 小時,即 01/15/2018 11:26:37

我知道 hive 中有 date_sub function 但它僅用於從給定時間戳中減去天數。 但我需要知道是否有一種方法可以減去小時、分鍾或秒。

我也嘗試過類似下面的內容,因為 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) 

那么有人可以幫我解決這個問題嗎?

它工作正常。

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') 

除了@StrongYoung的答案

我發現將這樣的長表達式定義為宏並放置在初始化文件中非常有用(例如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)

宏可以從Hive 0.12.0開始提供,更多詳細信息可以在這里找到(注意“錯誤修復”部分)。

例如,您在特定時區的當前時間 - 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM