2014-01-29 38 views
11

我試圖在2010年9月1日至2013年8月31日的Hive表格中過濾數據。包含日期的列是字符串格式(yyyy-mm-dd)。我可以在此列上使用month()和year()。但是,如何使用它們來過濾上述日期之間的數據?任何示例/示例代碼都會受到歡迎!Hive:日期爲字符串時在指定日期之間過濾數據

回答

11

偉大的事情有關yyyy-mm-dd日期格式是沒有必要提取month()year(),你可以對字符串直接做比較:

SELECT * 
    FROM your_table 
    WHERE your_date_column >= '2010-09-01' AND your_date_column <= '2013-08-31'; 
+4

它的工作原理相同的方式,如果日期是字符串 'YYYY-MM-DD HH:MM:SS'格式?另外,如果它同時包含「YYYY-MM-DD」和「YYYY-MM-DD HH:MM:SS」格式? – Gadam

+0

也適用於'YYYYMMDD'。 – ChuckCottrill

+0

對我來說它不適用'2016-05-18 00:01:24.413' –

6

蜂巢有很多好的日期解析的UDF:https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF#LanguageManualUDF-DateFunctions

只要做Nigel Tufnel建議的字符串比較可能是最簡單的解決方案,雖然從技術上講它是不安全的。但你可能不需要擔心,除非你的桌子有關於中世紀時期(僅有3年數字的日期)或來自科幻小說(日期超過4年的數字)的日期的歷史數據。無論如何,如果您發現自己處於想要進行更有趣的日期比較的情況下,或者您的日期格式不是「從最大到最小」的順序,例如, 「MM/DD/YYYY」的美國慣例,那麼你可以使用unix_timestamp有兩個參數:

select * 
from your_table 
where unix_timestamp(your_date_column, 'yyyy-MM-dd') >= unix_timestamp('2010-09-01', 'yyyy-MM-dd') 
and unix_timestamp(your_date_column, 'yyyy-MM-dd') <= unix_timestamp('2013-08-31', 'yyyy-MM-dd') 
+0

嗨,你能告訴我在HIVE中字符串比較是如何完成的嗎?我正在做日期字符串比較'2016-12-02'和'2017-01-01'之間的日期,其中我的日期是時間戳字符串,它不顯示2017年的數據。當我使用'to_date日期)'。 – neoFox

2

無需每月提取和year.Just需要使用UNIX_TIMESTAMP(日期字符串,字符串格式)功能。

例如:

select yourdate_column 
from your_table 
where unix_timestamp(yourdate_column, 'yyyy-MM-dd') >= unix_timestamp('2014-06-02', 'yyyy-MM-dd') 
and unix_timestamp(yourdate_column, 'yyyy-MM-dd') <= unix_timestamp('2014-07-02','yyyy-MM-dd') 
order by yourdate_column limit 10; 
0

如果你是無法提取相同與UNIX時間戳記,然後寫了蜂箱

0

一個Java UDF你必須串酸鹽轉化爲所需的日期格式如下然後你可以得到你需要的結果。

hive> select * from salesdata01 where from_unixtime(unix_timestamp(Order_date, 'dd-MM-yyyy'),'yyyy-MM-dd') >= from_unixtime(unix_timestamp('2010-09-01', 'yyyy-MM-dd'),'yyyy-MM-dd') and from_unixtime(unix_timestamp(Order_date, 'dd-MM-yyyy'),'yyyy-MM-dd') <= from_unixtime(unix_timestamp('2011-09-01', 'yyyy-MM-dd'),'yyyy-MM-dd') limit 10; 
OK 
1 3 13-10-2010 Low 6.0 261.54 0.04 Regular Air -213.25 38.94 
80 483 10-07-2011 High 30.0 4965.7593 0.08 Regular Air 1198.97 195.99 
97 613 17-06-2011 High 12.0 93.54 0.03 Regular Air -54.04 7.3 
98 613 17-06-2011 High 22.0 905.08 0.09 Regular Air 127.7 42.76 
103 643 24-03-2011 High 21.0 2781.82 0.07 Express Air -695.26 138.14 
127 807 23-11-2010 Medium 45.0 196.85 0.01 Regular Air -166.85 4.28 
128 807 23-11-2010 Medium 32.0 124.56 0.04 Regular Air -14.33 3.95 
160 995 30-05-2011 Medium 46.0 1815.49 0.03 Regular Air 782.91 39.89 
229 1539 09-03-2011 Low 33.0 511.83 0.1 Regular Air -172.88 15.99 
230 1539 09-03-2011 Low 38.0 184.99 0.05 Regular Air -144.55 4.89 
Time taken: 0.166 seconds, Fetched: 10 row(s) 
hive> select * from salesdata01 where from_unixtime(unix_timestamp(Order_date, 'dd-MM-yyyy'),'yyyy-MM-dd') >= from_unixtime(unix_timestamp('2010-09-01', 'yyyy-MM-dd'),'yyyy-MM-dd') and from_unixtime(unix_timestamp(Order_date, 'dd-MM-yyyy'),'yyyy-MM-dd') <= from_unixtime(unix_timestamp('2010-12-01', 'yyyy-MM-dd'),'yyyy-MM-dd') limit 10; 
OK 
1 3 13-10-2010 Low 6.0 261.54 0.04 Regular Air -213.25 38.94 
127 807 23-11-2010 Medium 45.0 196.85 0.01 Regular Air -166.85 4.28 
128 807 23-11-2010 Medium 32.0 124.56 0.04 Regular Air -14.33 3.95 
256 1792 08-11-2010 Low 28.0 370.48 0.04 Regular Air -5.45 13.48 
381 2631 23-09-2010 Low 27.0 1078.49 0.08 Regular Air 252.66 40.96 
656 4612 19-09-2010 Medium 9.0 89.55 0.06 Regular Air -375.64 4.48 
769 5506 07-11-2010 Critical 22.0 129.62 0.05 Regular Air 4.41 5.88 
1457 10499 16-11-2010 Not Specified 29.0 6250.936 0.01 Delivery Truck 31.21 262.11 
1654 11911 10-11-2010 Critical 25.0 397.84 0.0 Regular Air -14.75 15.22 
2323 16741 30-09-2010 Medium 6.0 157.97 0.01 Regular Air -42.38 22.84 
Time taken: 0.17 seconds, Fetched: 10 row(s) 
2

就像SQL,蜂巢支持BETWEEN操作更簡潔的語句:

SELECT * 
    FROM your_table 
    WHERE your_date_column BETWEEN '2010-09-01' AND '2013-08-31'; 
相關問題