如何在Cassandra中使用CQL查询获取系统日期

4
我想获取今天日期所属的数据,我尝试了以下方法:
select * from tablename where fieldname = dateof(now());

这个查询结果包含日期和系统时间,因此结果不够合适。

我该如何仅获取当前(今天的)日期?

1个回答

2
根据您的问题,列族表名使用时间戳字段作为主键。在这种情况下,每行都是由时间戳而不是日期来标识的,因此您无法按日期查询。
基于此,您需要更改数据模型以另一种方式定义主键。一种可能性是使用类似以下创建的表:
USE test;

create table posts(username varchar, 
    time timeuuid, 
    post_text varchar, 
    primary key(username, time)
);

insert into posts(username, time, post_text) 
values ('me', now(), 'Example');

insert into posts(username, time, post_text) 
values ('me', now(), 'Example');

select username, dateOf(time), post_text from posts;

在这种情况下,您会得到两个类似于此的结果。

enter image description here

所以,如果你想查询特定日期的内容,可以使用以下语句:
select username, dateOf(time), post_text from posts where time >= minTimeuuid('2014-04-23 00:00') and time < minTimeuuid('2014-04-24 00:00') and username = 'me';
来源:

http://cassandra.apache.org/doc/cql3/CQL.html#usingdates

http://christopher-batey.blogspot.nl/2013/05/time-series-based-queries-in-cassandra.html


稍微更改了插入语句中的数据,如下所示:values ('me',now(),'Example'); values ('me1',now(),'Example1');并执行了最后一个查询,类似于:select username,dateOf(time),post_text from posts where time >= minTimeuuid('2014-04-23 00:00') and time < minTimeuuid('2014-04-24 00:00');但是它给了我一个错误提示:Bad Request: Cannot execute this query as it might involve data filtering and thus may have unpredictable performance. If you want to execute this query despite the performance unpredictability, use ALLOW FILTERING..你能告诉我原因或如何解决吗? - Helping Hand..
原因是您正在尝试仅使用主键的一部分进行过滤,这被认为是一个潜在的昂贵查询。您仍然可以使用类似以下的查询:select username,dateOf(time),post_text from posts where time >= minTimeuuid('2014-04-23 00:00') and time < minTimeuuid('2014-04-24 00:00')LIMIT 10 ALLOW FILTERING;
您可以在此处查看文档:http://www.datastax.com/documentation/cql/3.0/cql/cql_reference/select_r.html
- ftrujillo
明白了。感谢您的帮助。 - Helping Hand..

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接