impala查询昨天的数据而不键入日期可能使用python

bfhwhh0e  于 2021-06-01  发布在  Hadoop
关注(0)|答案(1)|浏览(575)

数据已按日期时间“2018-03-08 00:00:00”进行分析。我可以通过说“2018-03-08 00:00:00”和“2018-03-08 24:00:00”来获取2018年3月8日的数据。但是我想使用一个 Impala 日期时间函数,这样我就可以每天运行,而不用手动输入每个日期。我已经阅读了一些文件,但仍然感到困惑。

查询 Impala

cursor.execute("SELECT sourceaddress,count(sourceaddress) as count FROM table
GROUP BY sourceaddress
ORDER BY count desc
LIMIT 10")

即使我可以在python中定义查询之前的昨天,比如:

yesterday = str((pd.to_datetime('today') - pd.Timedelta(days=1)).date())

并以某种方式将其合并到impala查询中。

y53ybaqx

y53ybaqx1#

您的问题还不完全清楚,但据我所知,您希望使用impala/python日期时间函数来自动执行查询,以选择一天的数据。下面是我的例子,你可以做在 Impala 。

-- to_date - function will cut the date from timestamp
-- now()   - gives you the current timestamp

select to_date(now()) as currentDate, 
to_date(now() + interval 1 days) as currentDatePlusaDay, 
now() as currentTimestamp, 
now() + interval 1 day as currentTimestampPlusaDay, 
concat(to_date(now() - interval 1 days), ' 00:00:00') as whereBetweenMin, 
concat(to_date(now() - interval 1 days), ' 24:00:00') as whereBetweenMax ;

--Result

+-------------+---------------------+-------------------------------+-------------------------------+---------------------+---------------------+
| currentdate | currentdateplusaday | currenttimestamp              | currenttimestampplusaday      | wherebetweenmin     | wherebetweenmax     |
+-------------+---------------------+-------------------------------+-------------------------------+---------------------+---------------------+
| 2018-03-23  | 2018-03-24          | 2018-03-23 12:14:36.073281000 | 2018-03-24 12:14:36.073281000 | 2018-03-22 00:00:00 | 2018-03-22 24:00:00 |
+-------------+---------------------+-------------------------------+-------------------------------+---------------------+---------------------+

--You can probably use wherebetweenmin & wherebetweenmax

相关问题