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

标签 cassandra cql cql3

我想检索属于今天日期的那些数据,我尝试了以下内容

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;

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

enter image description here

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

选择用户名,
日期(时间),
后文
来自帖子
其中时间 >= 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

关于cassandra - 如何在cassandra中使用cql查询获取系统日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21304381/

相关文章:

java - 正确处理大量异步查询

cassandra - 如何完全清除,重置和重新启动Cassandra集群?

Python 无法通过 Docker 连接到 Cassandra

hadoop - Cassandra CQL3 复合 key 不是由 Hadoop reducer 编写的

mongodb - 用于博客/内容管理系统的 NoSQL 数据库? (MongoDB/ Cassandra )

database - Cassandra数据库的最佳实践建模数据

python - 如何将日期时间插入 Cassandra 1.2 时间戳列

Cassandra,改变replication_factor后的奇怪行为

cassandra - 为什么以及何时在现实生产场景中使用 Cassandra 中的 Vnode?

Cassandra 性能 SELECT by id 或 SELECT by Nothing