如何在cassandra中使用cql查询获取系统日期

我想检索那些属于今天日期的数据,我尝试下面的事情

select * from tablename where fieldname = dateof(now());

此查询同时给出日期和系统时间,因此结果将不正确.

我怎样才能获得当前(今天)的日期?

最佳答案 根据您的问题,列族表名使用时间戳字段名作为主键.在这种情况下,每一行都由时间戳而不是日期标识,因此您无法按日期查询.

基于此,您必须更改数据模型以另一种方式定义主键.可能是使用像这样创建的表:

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;

在这种情况下,您将获得两个这样的结果

因此,如果您想查询特定日期,您可以使用:

选择用户名,
dateOf(时间),
POST_TEXT
来自帖子
其中时间> = minTimeuuid(‘2014-04-23 00:00’)
和时间< minTimeuuid(‘2014-04-24 00:00’)
和用户名=’我’;

资料来源:

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

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

点赞