简体   繁体   English

用于分页的Cassandra CQL令牌功能

[英]Cassandra CQL token function for pagination

I am new to CQL and trying to add pagination support for my tables defined in cassandra as shown below - 我是CQL的新手,并尝试为在cassandra中定义的表添加分页支持,如下所示-

cqlsh:dev> create table emp4 (empid uuid , year varchar , month varchar , day varchar, primary key((year, month, day), empid));
cqlsh:dev> insert into emp4 (empid, year, month, day) values (08f823ac-4dd2-11e5-8ad6-0c4de9ac7563,'2014','03','19');
cqlsh:dev> insert into emp4 (empid, year, month, day) values (08f823ac-4dd2-11e5-8ad6-0c4de9ac7562,'2016','03','19');

cqlsh:dev> select * from emp4;

 year | month | day | empid
------+-------+-----+--------------------------------------
 2016 |    03 |  19 | 08f823ac-4dd2-11e5-8ad6-0c4de9ac7562
 2015 |    03 |  19 | 08f823ac-4dd2-11e5-8ad6-0c4de9ac756f
 2014 |    03 |  19 | 08f823ac-4dd2-11e5-8ad6-0c4de9ac7563

When I try to execute a query to fetch the records based on the the following comparison, the statement seems to be incomplete as show below - 

cqlsh:dev> select * from emp4 where token(year, month, day, empid) > token('2014','04',28',08f823ac-4dd2-11e5-8ad6-0c4de9ac7563) LIMIT 1;
       ... ;
       ... 

I am trying to fetch records which have year,month, day greater than a specific value and also a given uuid. 我试图获取年,月,日大于特定值以及给定uuid的记录。 I think I am executing the query in the wrong way. 我认为我以错误的方式执行查询。 Can someone help me with this ? 有人可以帮我弄这个吗 ?

First of all, inputs that you send to the token() function must match your partition key... not your complete primary key: 首先,您发送给token()函数的输入必须与分区键匹配, 而不是与完整的主键匹配:

Secondly, the order of your partition values is not necessarily the same as the tokens generated for them. 其次,分区值的顺序不一定与为其生成的标记相同。 Look what happens when I insert three more rows, and the query using the token function: 看看我再插入三行,并使用token函数查询时会发生什么:

 system.token(year, month, day) | year | month | day | empid
--------------------------------+------+-------+-----+--------------------------------------
           -8209483605981607433 | 2016 |    03 |  19 | 08f823ac-4dd2-11e5-8ad6-0c4de9ac7562
           -6378102587642519893 | 2015 |    03 |  19 | 08f823ac-4dd2-11e5-8ad6-0c4de9ac7562
           -5253110411337677325 | 2013 |    03 |  19 | 08f823ac-4dd2-11e5-8ad6-0c4de9ac7562
           -3665221797724106443 | 2011 |    03 |  19 | 08f823ac-4dd2-11e5-8ad6-0c4de9ac7562
           -2421035798234525153 | 2012 |    03 |  19 | 08f823ac-4dd2-11e5-8ad6-0c4de9ac7562
            -742508345287024993 | 2014 |    03 |  19 | 08f823ac-4dd2-11e5-8ad6-0c4de9ac7563

(6 rows)

As you can see, the rows are decidedly not in order by year. 如您所见,行肯定不是按年份排序的。 And in this case your 2014 row has generated the largest token. 在这种情况下,您的2014行产生了最大的令牌。 Therefore querying for rows with a token value larger than that year will yield nothing. 因此,查询令牌值大于该年的行将不会产生任何结果。 But, if I want to query for rows with a token year greater than 2013, it works: 但是,如果我要查询令牌年份大于2013的行,则可以:

SELECT token(year,month,day),year,month,day,empid FROM emp4 
  WHERE token(year,month,day) > token('2013','03','19');

 system.token(year, month, day) | year | month | day | empid
--------------------------------+------+-------+-----+--------------------------------------
           -3665221797724106443 | 2011 |    03 |  19 | 08f823ac-4dd2-11e5-8ad6-0c4de9ac7562
           -2421035798234525153 | 2012 |    03 |  19 | 08f823ac-4dd2-11e5-8ad6-0c4de9ac7562
            -742508345287024993 | 2014 |    03 |  19 | 08f823ac-4dd2-11e5-8ad6-0c4de9ac7563

(3 rows)

Also note that you will need to use dates of your existing rows to return results that are of more value to you. 还要注意,您将需要使用现有行的日期来返回对您更有价值的结果。 After all, the token generated by token('2014','04','28') may not actually be greater than the token generated by token('2014','03','19') . 毕竟, token('2014','04','28')生成的token('2014','03','19')可能实际上不大于token('2014','03','19')生成的token('2014','03','19')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM