Database Reference
In-Depth Information
Now that we have our first page, we want to get the second. It would be nice if we could
simply ask for the next primary key in order after
dave
using the following SELECT
statement:
SELECT * FROM "users"
WHERE "username" > 'dave'
LIMIT 2;
Unfortunately, this will give an error:
The message is a bit cryptic, and we don't know what exactly a
partition key
is yet, but it
does contain a helpful hint: we can use the
token()
function to do what we want. The
reason that our attempted query doesn't work is that, as we noticed before, the primary
keys in our
users
table are not stored in lexical order; Cassandra can only return rows in
the order in which they are stored.
The actual ordering is determined by the
token
of the primary key; the way the token is
calculated is opaque to us, but Cassandra lets us use the
token()
function to retrieve the
token for a given value:
SELECT "username", token("username")
FROM "users";
Now we can see why the rows are returned in the order they are; they ascend by token:
Armed with this function, we can retrieve the next page of results as follows: