Search code examples
cassandrapaginationerlangcql

Cassandra - Iterate through table rows


My goal is to go through my cassandra database, each entry in it, and check a certain field to see if I need to perform an action. I am using the erlang cql driver found here: https://github.com/matehat/cqerl

When I run select * from keyspace.table limit 10; in a table of 2000, I get the first 10 results... but I then need to get the next 1990. How do i get the next 10 using CQL? I see that there is support for paging but no documentation for how I can do it in cql.

I have researched this question extensively and it seems like the only answers on the topic are incomplete and many commenters on those questions did not receive the answer they were looking for: Iterating through Cassandra wide row with CQL3

Thank you for your help in advance!


Solution

  • Thanks to @Atomic_alarm, I found the answer.

    The docs here : https://www.datastax.com/dev/blog/client-side-improvements-in-cassandra-2-0 specify how to do automatic paging with CQL.

    I did not use the cql command line like the docs say, instead I used the erlang driver code. First I inserted 1000 rows into a table, and then I ran the function below. It iterated through all 1000 results, 2 pages at a time:

    test_page() ->
           {ok, Client} = cqerl:get_client({}),
           {ok, Res} = cqerl:run_query(Client, #cql_query{statement = "SELECT * FROM dks.devices;",
                           page_size = 2}),
           get_more(Res, 0).
       get_more(Res, Num) ->
           case cqerl:has_more_pages(Res) of
               true ->
                   {ok, Res2} = cqerl:fetch_more(Res),
                   get_more(Res2, Num+1);
               false -> 
                   Num
           end.