【问题标题】:Cassandra CQL select query not returning records which have timestamp as clusterkeyCassandra CQL选择查询不返回时间戳为clusterkey的记录
【发布时间】:2014-12-01 23:51:42
【问题描述】:

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 行。

【问题讨论】:

    标签: cassandra cassandra-2.0 cql3 nosql


    【解决方案1】:

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

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

    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

    【讨论】:

    • 感谢您的即时回复.. 但它不起作用:
    • 对于插入查询我使用 dateof(now()) 因为这个选择运算符没有返回任何值。请告诉我如何克服这个问题。
    • @madheswaran 您能否使用INSERT 查询编辑您的问题,仔细检查您的表架构以确保它与问题中的内容相似,并提供您的版本号?如果没有这些信息,将很难为您提供任何进一步的帮助。
    • INSERT INTO entity_data (received_date, entity, received_time, node) VALUES ('2014-09-24 00:00:00+0400','NA',dateOf(now()),0) ;
    • @madheswaran 已编辑。
    猜你喜欢
    • 2019-05-25
    • 2018-11-10
    • 2014-05-15
    • 1970-01-01
    • 1970-01-01
    • 2013-01-02
    • 2017-10-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多