我試圖在2010年9月1日至2013年8月31日的Hive表格中過濾數據。包含日期的列是字符串格式(yyyy-mm-dd)。我可以在此列上使用month()和year()。但是,如何使用它們來過濾上述日期之間的數據?任何示例/示例代碼都會受到歡迎!Hive:日期爲字符串時在指定日期之間過濾數據
回答
偉大的事情有關yyyy-mm-dd
日期格式是沒有必要提取month()
和year()
,你可以對字符串直接做比較:
SELECT *
FROM your_table
WHERE your_date_column >= '2010-09-01' AND your_date_column <= '2013-08-31';
蜂巢有很多好的日期解析的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')
嗨,你能告訴我在HIVE中字符串比較是如何完成的嗎?我正在做日期字符串比較'2016-12-02'和'2017-01-01'之間的日期,其中我的日期是時間戳字符串,它不顯示2017年的數據。當我使用'to_date日期)'。 – neoFox
無需每月提取和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;
如果你是無法提取相同與UNIX時間戳記,然後寫了蜂箱
一個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)
就像SQL,蜂巢支持BETWEEN操作更簡潔的語句:
SELECT *
FROM your_table
WHERE your_date_column BETWEEN '2010-09-01' AND '2013-08-31';
- 1. Django過濾日期時間後,將字符串轉換爲日期時間
- 2. 過濾字符串日期
- 3. javascript日期/時間字符串轉換爲SQL日期時間
- 4. 將字符串日期時間轉換爲Ruby日期時間
- 5. 將日期時間字符串轉換爲日期時間
- 6. SQL Azure:在兩個日期之間獲取數據,日期爲字符串
- 7. 字符串日期時間
- 8. SQL:按日期時間過濾數據
- 9. 將字符串轉換爲Hive中的日期/時間戳
- 10. HQL查詢過濾日期時間數據的日期
- 11. 如何將日期時間字符串轉換爲自定義日期時間
- 12. 轉換日期/時間字符串值到.NET日期時間
- 13. 將UTC日期轉換爲日期時間字符串Javascript
- 14. 將日期字符串轉換爲SSIS中的日期時間
- 15. 將日期字符串轉換爲日期時間對象
- 16. 將日期時間轉換爲代表日期的字符串
- 17. 將日期字符串轉換爲日期時間
- 18. 從日期時間字符串轉換爲JavaScript日期對象
- 19. php過期日期爲字符串
- 20. 可以過濾日期,但不能過濾日期時間?
- 21. DataGrid日期字符串過濾器
- 22. 轉換日期字符串日誌文件,然後從字符串到日期時間爲數據庫
- 23. 數字日期到字符串日期
- 24. pandas groupBy日期然後過濾日期和字符串到新的數據框
- 25. 如何將日期和時間字符串轉換爲日期字符串?
- 26. 使用過濾器設置日期字段過濾日期時間字段
- 27. 在數據窗口過濾器中使用日期字符串
- 28. 過濾日期不在兩個日期之間
- 29. 如何在日期之間過濾codeigniter
- 30. Django:日期時間按日期過濾忽略時間
它的工作原理相同的方式,如果日期是字符串 'YYYY-MM-DD HH:MM:SS'格式?另外,如果它同時包含「YYYY-MM-DD」和「YYYY-MM-DD HH:MM:SS」格式? – Gadam
也適用於'YYYYMMDD'。 – ChuckCottrill
對我來說它不適用'2016-05-18 00:01:24.413' –