简体   繁体   English

如何在Cassandra中按日期范围进行搜索?

[英]How to search by date range in cassandra?

for eg 例如

SELECT *
FROM api_call_log
WHERE account_sid='XXXXXXXXXXXX'            AND 
      created_time >= 01-02-2016 00:00:00   AND
      created_time <= 31-02-2016 23:59:59
ALLOW FILTERING

I am getting records from first month as well though I searched for second month. 尽管我搜索了第二个月,但我也从第一个月获得了记录。

It's difficult to say without seeing your table structure or relevant portions of your result set. 如果不查看表结构或结果集的相关部分,很难说。 But I did notice that you are not specifying a GMT offset, which means you are effectively querying by your default local offset . 但是我确实注意到您没有指定GMT偏移量,这意味着您可以通过默认的本地偏移量有效地进行查询 The problem, is that Cassandra stores by GMT+0000. 问题在于,Cassandra按GMT + 0000进行存储。

For example, if you have a negative GMT offset of say -0600 (like me), a query for GMT-0600 would miss 6-hours-worth of data from February 1st. 例如,如果您的GMT偏移量为负-0600(例如我),那么从2月1日开始查询GMT-0600将丢失6小时的数据。 For instance, if I have a row out there for 2016-02-01 01:00:00+0000 , this query will not return it: 例如,如果我在那里有一行用于2016-02-01 01:00:00+0000 ,那么此查询将不会返回它:

aploetz@cqlsh:stackoverflow> SELECT * FROm events WHERe monthbucket='201602' 
    AND eventdate >= '2016-02-01 00:00:00';

 monthbucket | eventdate | beginend | eventid | eventname
-------------+-----------+----------+---------+-----------

(0 rows)

And that's because 2016-02-01 01:00:00+0000 is essentially 2016-01-31 19:00:00-0600 . 那是因为2016-02-01 01:00:00+0000 2016-01-31 19:00:00-0600 2016-02-01 01:00:00+0000本质上是2016-01-31 19:00:00-0600 So if I add the GMT offset of 0000 , I see the row. 因此,如果我将GMT偏移量添加为0000 ,则会看到该行。

aploetz@cqlsh:stackoverflow> SELECT * FROm events WHERe monthbucket='201602'
    AND eventdate >= '2016-02-01 00:00:00+0000';

 monthbucket | eventdate                | beginend | eventid                              | eventname
-------------+--------------------------+----------+--------------------------------------+-------------------
      201602 | 2016-02-01 01:00:00+0000 |        b | 78d2c2b7-c4ec-408f-be37-eccc0c05727d | test month border

(1 rows)

My guess, is that you probably have the opposite problem (extra rows vs. missing rows) due to having a positive GMT offset. 我的猜测是,由于GMT偏移为 ,您可能会遇到相反的问题(多余的行还是缺少的行)。 Not specifying your offset in your query could be why it is including rows from the previous month. 未在查询中指定偏移量可能是为什么它包含上个月的行。 And if that's the case, then you may want those rows. 如果是这种情况,那么您可能想要这些行。

Also, don't use ALLOW FILTERING . 另外,请勿使用ALLOW FILTERING Like ever. 像以往一样

Try this 尝试这个

SELECT * FROM api_call_log WHERE account_sid='XXXXXXXXXXXX' AND jobs.created_at between '01-02-2016 00:00:00' and '31-02-2016 23:59:59';

And also check the DATE/TIME format of the date column 并检查日期列的DATE / TIME格式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM