Cassandra CQL token function for pagination

情到浓时终转凉″ 提交于 2020-02-02 11:35:00

问题


I am new to CQL and trying to add pagination support for my tables defined in cassandra as shown below -

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. I think I am executing the query in the wrong way. Can someone help me with this ?


回答1:


First of all, inputs that you send to the token() function must match your partition key...not your complete primary key:

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:

 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. 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:

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').



来源:https://stackoverflow.com/questions/34601280/cassandra-cql-token-function-for-pagination

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!