Pergunta

According to the example page of Astyanax:

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

When you run a COUNT query, the result returns a "number", so you can query that with:

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) {
}

And it should give you the COUNT result, but I am querying my table with cql3 and it isn't giving me a "Number", but "Rows", only one Row and only one Column.

The method hasNumber gives False and the method hasRows gives True, so now, How can I get the Result of my COUNT query?

Foi útil?

Solução

You should use

System.out.println("CQL Count: " + result.getResult().getRows().getRowByIndex(0).getColumns().getColumnByName("count").getLongValue());

Outras dicas

Based on abhi's answer but added a limit of 1M to bypass default limit of 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();
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top