How to check if a result set is empty or null using datastax cassandra driver for java

How to check empty result set using datastax java cassandra driver?

Suppose I execute the following query: "SELECT * FROM my_table WHERE mykey = something"

there is a high probability that the request will not be agreed. The following code does not work:

if (rs != null) rs.one().getString("some_column"); 
+7
java cassandra datastax
source share
2 answers

You were pretty close, the right solution:

 Row r = rs.one(); if (r != null) r.getString("some_column"); 

The driver will always return a result set, regardless of whether there were any returned results. The documentation for one () states that if no rows were returned, rs.one () returns null.

You can also use getAvailableWithoutFetching () , which returns the number of rows in the result set without getting more rows. Since pageSize must be> = 1, you can be sure that if there is at least 1 row, it will always return a value greater than 0.

+13
source share

You can also check the rs.isExhausted() value to determine if there is additional data in the result set ...

+1
source share

All Articles