I have a Cassandra 1.2 cluster and I\'m using it from Python using the cql library. Now I need to implement some paging functionality that seems pretty straightforward using
An update for modern versions of Cassandra. All of the CQL3 native drivers can use server side paging (starting with Cassandra 2.0), so no need to go through the hassle of setting it up manually anymore.
You can do paging in much the same way: set a limit and start at a column name greater than the previous one received. As an example, I created a table test1 in keyspace ks1:
CREATE TABLE test1 (
a text,
b text,
PRIMARY KEY (a, b)
)
Here a is my row key and b is the column name. I then inserted 12 records with a=a and b from a to l. So
cqlsh:ks1> select * from test1;
a | b
---+---
a | a
a | b
a | c
a | d
a | e
a | f
a | g
a | h
a | i
a | j
a | k
a | l
Then I paged with this python using the CQL driver:
import cql
con = cql.connect('localhost', keyspace='ks1', cql_version='3.0.0')
cursor = con.cursor()
last = ""
while last != None:
cursor.execute("select * from test1 where a=:a and b>:b limit 5", {"a": "a", "b": last})
last = None
for row in cursor:
print row
last = row[1]
which pages in batches of 5. The output is:
[u'a', u'a']
[u'a', u'b']
[u'a', u'c']
[u'a', u'd']
[u'a', u'e']
[u'a', u'f']
[u'a', u'g']
[u'a', u'h']
[u'a', u'i']
[u'a', u'j']
[u'a', u'k']
[u'a', u'l']