Get a BigInteger attribute from Cassandra ResultSet
Asked Answered
Q

3

10

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?

Quaver answered 11/12, 2014 at 11:15 Comment(0)
R
12

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");
Rub answered 11/12, 2014 at 11:26 Comment(2)
Link is dead. You might want to check that.Sherard
The bigint Cassandra type is mapped with long Java Type. Documentation link: CQL to Java type mapping @SherardVitovitoria
Q
0

Following worked. I'm not sure why it is working though.

row.getLong(0)
Quaver answered 11/12, 2014 at 11:24 Comment(2)
It works because the aggregate function count(*) actually returns an int (which is a long in Java terms) not a varint (which would be a BigInteger in Java). Apparantly you can't have more than 2^63 rows in a table, but that should be enough.Fahy
It works because cassandra bigint is mapped with long java type. Full documentation link with Cassandra-Java mapping types: https://mcmap.net/q/1082654/-get-a-biginteger-attribute-from-cassandra-resultsetVitovitoria
V
0

The bigint in Cassandra is mapped with long Java Type. Documentation: CQL to Java type mapping.

In your case:

row.getLong(0)
Vitovitoria answered 18/11, 2020 at 14:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.