Cassandra C# Driver returns first 5000 rows only

1.1k Views Asked by At

I am trying to get all the records form Cassandra table but getting 5 thousand rows only. Is there any way to get all the result form a table?

Below are the version details:
It is the driver version 3.11.0
My server version is [cqlsh 5.0.1 | Cassandra 3.11.4 | CQL spec 3.4.4 | Native protocol v4]

Below is my code

//Create a cluster instance using 3 cassandra nodes.
var cluster = Cluster.Builder()
.AddContactPoints("xx.xx.xx.xx")
 .Build();
var session = cluster.Connect("keyspace");
var rs = session.Execute("select * from table ALLOW FILTERING");
ini i=0;
 foreach (var row in rs)
 {
    i++;
}

The value of i is always 5000. I tried to implement paging to get all the data but was not able to do so.

 while (!rs.IsFullyFetched)
 {
     rs.FetchMoreResults();
     foreach (var row in rs)
     {
         i++;
     }
 }

I'm still getting the same result

Also trying to follow the URL https://docs.datastax.com/en/developer/csharp-driver/3.13/features/paging/ but that too is not working because I am not using any filter.
But when I tried to introduce a filter the result is 0 rows.

    var ps = session.Prepare("select * from table where state = ? ALLOW FILTERING");
    //// Set the page size at statement level.
    var statement = ps.Bind("xyz").SetPageSize(1000);
    var rs = session.Execute(statement);
    var i = 0;
    foreach (var row in rs)
    {
       i++;
    }

There are record for the particular state but not getting any data. The number of records is zero.
Earlier I had issue too with the driver version
Query Cassandra form C# no result is shown

2

There are 2 best solutions below

2
Synystter On

by default the fetchsize is set to 5000 to shield an application against accidentally retrieving large result sets in a single response, maybe u can play with .setFetchSize(int) property

0
Erick Ramirez On

I think you already know that the C# driver has paging enabled by default (AutoPage = true) and the default page size is 5000 (DefaultPageSize = 5000).

I think the problem with your test is that you are using the RowSet.IsFullyFetched property as the condition in the loop:

while (!rs.IsFullyFetched)
{
    ...
}

Try replacing it with the RowSet.IsExhausted() method instead:

while (!rs.IsExhausted())
{
    rs.FetchMoreResults();
    foreach (var row in rs)
    {
        i++;
    }
}

RowSet.IsExhausted() returns true/false depending on whether there are more results similar to IsFullyFetched but with the difference that it will page for more results. Cheers!