I'm trying to get the number of key value pairs in a Cassandra column family. Following is the code I used.
我正在尝试获取Cassandra列族中的键值对的数量。以下是我使用的代码。
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?
根据datastax文档(http://www.datastax.com/drivers/java/2.0/com/datastax/driver/core/Row.html),getVarint应该返回一个BigInteger。那么为什么我在这里得到例外?我做错了什么?
2 个解决方案
#1
11
As specified here, 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");
#2
0
Following worked. I'm not sure why it is working though.
以下工作。我不确定它为什么会起作用。
row.getLong(0)
#1
11
As specified here, 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");
#2
0
Following worked. I'm not sure why it is working though.
以下工作。我不确定它为什么会起作用。
row.getLong(0)