ivan R
ivan R

Reputation: 296

ResultSet exhausted error is coming while accessing value of cassandra table through scala

I had written a simple code to connect to cassandra and fetch the table value in scala .Here is my code

def t()={
 var cluster:Cluster=connect("127.0.0.1")
 println(cluster)
 var session:Session=cluster.connect("podv1");
   // var tx= session.execute("CREATE KEYSPACE myfirstcassandradb WITH " +
        //                  "replication = {'class':'SimpleStrategy','replication_factor':1}")

                          var p=session.execute("select data from section_tbl limit 2;")


 session.close()
 cluster.close
}     

Although create keyspace thing is working(which i had commented) but when i am trying to fetch the data from table it is giving error like ResultSet[ exhausted: false, Columns[data(varchar)]] .I just started connecting scala to cassandra so may be i am doing some mistake or something.Any help regarding how to get data will be appreciable.

Upvotes: 1

Views: 2050

Answers (1)

Andy Tolbert
Andy Tolbert

Reputation: 11638

it is giving error like ResultSet[ exhausted: false, Columns[data(varchar)]]

This is not an error, ResultSet is the object returned from execute. exhausted indicates that not all the rows have been consumed by you yet, and Columns indicates the name of each column and its type.

So for example in your case of 'p' being the variable assigned to session.execute, you can do something like:

var p = session.execute("select data from section_tbl limit 2;")
println(p.all())

all() the grabs all the rows associated with your query from Cassandra and captures it as a List[Row]. If you have a really large result set (which in your case you don't since you are limiting to two rows), you may want to iterate of the ResultSet instead, which will allow the driver to use paging to retrieve chunks of Rows at a time from cassandra.

A ResultSet implements java.util.Iteratable<Row>, so you can go all the operations you can with iterable. Refer to the ResultSet api for more operations.

Upvotes: 1

Related Questions