Cassandra CQL选择查询不返回时间戳为clusterkey的记录

时间:2014-10-07 14:05:39

标签: cassandra cassandra-2.0 cql3 nosql

Cassandra CQL:使用组合键和群集密钥创建的表。当我尝试从分区键执行select *然后我能够检索所有数据,它也适用于关系运算符(<或>)。 但是当我使用具有适当值的等于(=)运算符查询特定簇密钥时,它返回0行。

表格

CREATE TABLE entity_data (
received_date timestamp,
entity text,
received_time timestamp,
node int,
primary key ((received_date ,entity),received_time));

数据(从实体中选择*):

received_date              | entity | received_time            | node_id
2014-09-24 00:00:00+0400   |     NA | 2014-09-24 18:56:55+0400 |       0  | 
条件查询

- 此处不起作用

select * from entity_data 
where received_date = '2014-09-24 00:00:00+0400' and entity = 'NA' 
and received_time='2014-09-24 18:56:55+0400';
(0 rows)

- 它返回0行。

1 个答案:

答案 0 :(得分:3)

我知道发生了什么。您正在使用now()生成时间UUID。但是当你使用timestamp将其转换为dateOf()时,你会截断它的毫秒数。因此,查询等于2014-09-24 18:56:55 + 0400的received_time将不会产生任何结果,因为timestamp类型仍然以毫秒存储(您无法看到这是由于你的dateOf())。

解决此问题的最佳方法是将您的时间存储为timeuuid s(注意:我将received_date留作时间戳仅用于示例目的)。然后在dateOf时使用SELECT,并将minTimeuuid()函数用于WHERE子句:

CREATE TABLE entity_data2 (
    received_date timestamp,
    entity text,
    received_time timeuuid,
    node int,
PRIMARY KEY ((received_date, entity), received_time));

INSERT INTO entity_data2 (received_date, entity, received_time , node) 
VALUES ('2014-09-24 00:00:00+0400','NA',now(),0);

aploetz@cqlsh:stackoverflow> SELECT * FROM entity_data2 
    WHERE received_date = '2014-09-24 00:00:00+0400' AND entity = 'NA'  
    AND received_time>minTimeuuid('2014-10-08 08:13:53-0500') 
    AND received_time<minTimeuuid('2014-10-08 08:13:54-0500');

 received_date            | entity | received_time                        | node
--------------------------+--------+--------------------------------------+------
 2014-09-23 15:00:00-0500 |     NA | f3b548b0-4eec-11e4-9d05-7991a041665c |    0

(1 rows)

aploetz@cqlsh:stackoverflow> SELECT received_date, entity, dateof(received_time), node 
    FROM entity_data2 WHERE received_date = '2014-09-24 00:00:00+0400' AND entity = 'NA'
    AND received_time>minTimeuuid('2014-10-08 08:13:53-0500') 
    AND received_time<minTimeuuid('2014-10-08 08:13:54-0500');

 received_date            | entity | dateof(received_time)    | node
--------------------------+--------+--------------------------+------
 2014-09-23 15:00:00-0500 |     NA | 2014-10-08 08:13:53-0500 |    0

(1 rows)

基本上dateOf()函数用于查询数据,而不是存储数据。这是一篇博客文章,详细描述了如何使这项工作:

Time series based queries in Cassandra 1.2+ and CQL3