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

Gun*_*ant 6 cql token cassandra

根据这个文档,我正在尝试一个带有 token() 函数的选择查询,但它给出了错误的结果。

我正在使用以下 cassandra 版本

[cqlsh 5.0.1 | Cassandra 2.2.5 | CQL spec 3.3.1 | Native protocol v4]
Run Code Online (Sandbox Code Playgroud)

我正在尝试对下表进行令牌查询 -

CREATE TABLE price_key_test (
objectid int,
createdOn bigint,
price int,
foo text,
PRIMARY KEY ((objectid, createdOn), price));
Run Code Online (Sandbox Code Playgroud)

插入数据——

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');
Run Code Online (Sandbox Code Playgroud)

表中数据——

        objectid | createdon | price | foo
    ----------+-----------+-------+-----
            1 |      3000 |   300 |   x
            1 |      2000 |   200 |   x
            1 |      1000 |   100 |   x
Run Code Online (Sandbox Code Playgroud)

选择查询是——

select * from nasa.price_key_test where token(objectid,createdOn) > token(1,1000) and token(objectid,createdOn) < token(1,3000)
Run Code Online (Sandbox Code Playgroud)

此查询假设返回带有 createdOn 2000 的行,但它返回零行。

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

            (0 rows)
Run Code Online (Sandbox Code Playgroud)

根据我的理解, token(objectid,createdOn) > token(1,1000) 和 token(objectid,createdOn) < token(1,3000) 应该选择具有值为 1 和 2000 的分区键的行。

我的理解正确吗?

Aar*_*ron 6

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

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)
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)

生成的散列标记值不一定与其原始数值成正比。在您的情况下,token(1,3000)生成的哈希是三个中最小的,而不是最大的。

  • @Gunwant `token()` 当你想查询整个大表时是有意义的。通常,对大型结果集的查询会超时,因此您可以一次查询一个令牌范围以使其更有可能成功。 (3认同)
  • @Aron 感谢先生的回复。这意味着我们不能依赖 token() 函数。您能告诉我们什么时候可以在选择查询中使用令牌吗? (2认同)