Get a BigInteger attribute from Cassandra ResultSet

Chamila Wijayarathna picture Chamila Wijayarathna · Dec 11, 2014 · Viewed 7.8k times · Source

I'm trying to get the number of key value pairs in a Cassandra column family. Following is the code I used.

PreparedStatement statement = client.session
            .prepare("select count(*) from corpus.word_usage");
ResultSet results = client.session.execute(statement.bind());   
Row row = results.one();
System.out.println(row.getVarint(0));

But when I ran this code, I'm getting following exception.

Exception in thread "main" com.datastax.driver.core.exceptions.InvalidTypeException: Column count is of type bigint
   at com.datastax.driver.core.ColumnDefinitions.checkType(ColumnDefinitions.java:291)
   at com.datastax.driver.core.ArrayBackedRow.getVarint(ArrayBackedRow.java:185)
   at SimpleClient.main(SimpleClient.java:57)

According to datastax documentation (http://www.datastax.com/drivers/java/2.0/com/datastax/driver/core/Row.html) getVarint should return a BigInteger. So why I am getting a exception here? What an I doing wrong?

Answer

Semih Eker picture Semih Eker · Dec 11, 2014

You can get value as a long, instead.

I couldn't test it but could you try this:

PreparedStatement statement = client.session.prepare("select count(*) from corpus.word_usage");
ResultSet results = client.session.execute(statement.bind());   
Row row = results.one();
long expected = row.getLong("count");