Cassandra Java driver query ResultSet not stored in variable

Viewed 57
        //Read data from Cassandra
        String query = "SELECT * FROM LABA_2.DATA LIMIT 5;";
        //Creating Cluster object
        Cluster cluster = Cluster.builder().addContactPoint("127.0.0.1").build();
        //Creating Session object
        Session session = cluster.connect();
        //Getting the ResultSet
        ResultSet result = session.execute(query);
        System.out.println(result.all().size());
        System.out.println("AJFGJKABGSDKGJS");
        System.out.println(result.all().size());
        System.out.println("AJFGJKABGSDKGJS");

Output:

22/04/02 23:11:56 INFO Cluster: New Cassandra host /127.0.0.1:9042 added
5
AJFGJKABGSDKGJS
0
AJFGJKABGSDKGJS

In the first case size = 5, in the second = 0.

cassandra-driver - 3.11.0

Why results are different?

1 Answers

Your test is invalid. Calling ResultSet.all() forces the driver to retrieve the whole result set in one go. The important thing to note here is that after calling all(), there is nothing left to retrieve.

When you call all() a second time, it returns nothing because you have already retrieved everything -- the list is exhausted and there's nothing left. Cheers!

Related