Search code examples
cassandracassandra-2.0cql3

how to do a query with cassandradb counter table


i have a table in Cassandradb as mentioned below:

CREATE TABLE remaining (owner varchar,buddy varchar,remain counter,primary key(owner,buddy));

generally i do some inc/dec operations on REMAIN field ,using cql like below:

update remaining set remain=remain + 1 where owner='userA' and buddy='userB';
update remaining set remain=remain + 1 where owner='userA' and buddy='userC';
....

and now i need to find out all buddies for userA which it's REMAIN field greater then 1. when i using:

select buddy,remain from remaining where owner='userA' and remain > 0;

gives me an error:

No indexed columns present in by-columns clause with Equal operator

how to do this in a cassandradb way?


Solution

  • The short answer to this is that you cannot do queries with conditionals on counter columns in Cassandra.

    The reason behind this is that all Cassandra queries need to be modeled around the primary key of that particular table. Counter columns are not allowed as parts of the primary key of a table (their changing values would cause constant reorganization of the dat on disk). Counter columns are more used for tracking the state of a known piece of data, for example number of times a photo has been up-voted. This could be quickly recalled as long as we knew which photo we were interested in. To actually sort photos by numbers of votes you would need to perform an analytics style query using spark or Hadoop.