Cassandra - 使用 token() 函数选择查询

问题描述 投票:0回答:1

根据this文档,我尝试使用 token() 函数进行选择查询,但它给出了错误的结果。

我正在使用以下 cassandra 版本

[cqlsh 5.0.1 | Cassandra 2.2.5 | CQL spec 3.3.1 | Native protocol v4]

我正在尝试下表的令牌查询 -

CREATE TABLE price_key_test (
    objectid int,
    createdOn bigint,
    price int,
    foo text,
    PRIMARY KEY ((objectid, createdOn), price));

插入数据--

insert into nasa.price_key_test (objectid,createdOn,price,foo) values (1,1000,100,'x');
insert into nasa.price_key_test (objectid,createdOn,price,foo) values (1,2000,200,'x');
insert into nasa.price_key_test (objectid,createdOn,price,foo) values (1,3000,300,'x');

表中数据--

 objectid | createdon | price | foo
----------+-----------+-------+-----
        1 |      3000 |   300 |   x
        1 |      2000 |   200 |   x
        1 |      1000 |   100 |   x

选择查询是--

select * from nasa.price_key_test
    where token(objectid,createdOn) > token(1,1000)
      and token(objectid,createdOn) < token(1,3000);

此查询假设返回

createdOn
2000 的行,但它返回零行。

 objectid | createdon | price | foo
----------+-----------+-------+-----

(0 rows)

根据我的理解,

token(objectid,createdOn)
>
token(1,1000)
token(objectid,createdOn) < token(1,3000)
应该选择分区键值为 1 和 2000 的行。

我的理解正确吗?

cassandra token cql
1个回答
7
投票

尝试翻转大于/小于符号:

aploetz@cqlsh:stackoverflow> SELECT * FROM price_key_test 
    WHERE token(objectid,createdOn) < token(1,1000) 
    AND token(objectid,createdOn) > token(1,3000) ;

 objectid | createdon | price | foo
----------+-----------+-------+-----
        1 |      2000 |   200 |   x

(1 rows)

token()
函数添加到您的 SELECT 中应该可以帮助您理解原因:

aploetz@cqlsh:stackoverflow> SELECT objectid, createdon, token(objectid,createdon), 
    price, foo FROM price_key_test ;

 objectid | createdon | system.token(objectid, createdon) | price | foo
----------+-----------+-----------------------------------+-------+-----
        1 |      3000 |              -8449493444802114536 |   300 |   x
        1 |      2000 |              -2885017981309686341 |   200 |   x
        1 |      1000 |              -1219246892563628877 |   100 |   x

(3 rows)

生成的哈希令牌值不一定与其原始数值成比例。在你的例子中,

token(1,3000)
生成的哈希是三个中的最小,而不是最大的。

© www.soinside.com 2019 - 2024. All rights reserved.