1

根据 Astyanax 的示例页面:

https://github.com/Netflix/astyanax/wiki/Cql-and-cql3

当您运行COUNT查询时,result返回一个“数字”,因此您可以使用以下命令进行查询:

try {
    OperationResult<CqlResult<String, String>> result
        = keyspace.prepareQuery(CF_STANDARD1)
            .withCql("SELECT count(*) FROM Standard1 where KEY='A';")
            .execute();

    System.out.println("CQL Count: " + result.getResult().getNumber());
} catch (ConnectionException e) {
}

它应该给你COUNT结果,但我用 cql3 查询我的表,它没有给我一个“数字”,而是“行”,只有一行和一列。

方法hasNumber给出False,方法hasRows给出True,所以现在,我怎样才能得到COUNT查询的结果?

4

2 回答 2

3

你应该使用

System.out.println("CQL Count: " + result.getResult().getRows().getRowByIndex(0).getColumns().getColumnByName("count").getLongValue());
于 2013-09-24T19:57:10.527 回答
1

基于 abhi 的回答,但添加了 1M 的限制以绕过 10K 的默认限制。

private long getCount(ColumnFamily<String, String> columnFamily) throws ConnectionException {
    OperationResult<CqlResult<String, String>> result = context.getClient().prepareQuery(columnFamily)
            .withCql("SELECT count(*) FROM Standard1 limit 1000000;")
            .execute();

    return result.getResult().getRows().getRowByIndex(0).getColumns().getColumnByName("count").getLongValue();
}
于 2014-06-02T14:44:49.190 回答