2

I create a table with counter column in Cassandra, just like :

create table test_count(
    pk int,
    count counter,
    primary key (pk)
);

And after that I update some record like:

update test_count set count = count + 1 where pk = 1;

Then I want to reset the count to 0, but there's no reset command in cql, so I delete the record:

delete from test_count where pk = 1;

And then I re-execute the update CQL, but when I select * from test_count, there's no record with pk = 1, so is it a bug of Cassandra? When you delete the record with counter column, it disappears forever? How can I reset the count column to 0? How can I re-insert a record with counter column?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
ray
  • 21
  • 3
  • delete from test_count where pk = 1; this query here means you are deleting all the record where pk=1. So thats why your record are all deleted.If you want to reset your count to 0 can you try set count = 0. – Yasmeen Aug 03 '15 at 09:33
  • Thanks, but in cassandra, you can only write set count = count +/- *, set count = 0 is not allowed, and also cassandra don't allow you insert record with counter column, you just can update, so that's why I use update instead of insert after delete. – ray Aug 03 '15 at 10:06
  • Before I run Delete cql, update will insert record if non exists, but things has changed after I run Delete – ray Aug 03 '15 at 10:07
  • one option is you can subtract the count for e.g if from the select you received the count as 5 you do something like this Update test_count Set count = count-5 where pk=1; – Yasmeen Aug 03 '15 at 10:50
  • this would help http://wiki.apache.org/cassandra/Counters and even this http://stackoverflow.com/questions/13653681/apache-cassandra-delete-from-counter – Yasmeen Aug 03 '15 at 10:52
  • oh thank you very much, now I see...Seems that I have to give up counter column, cause "query and set -value" is very resource-consuming.. – ray Aug 03 '15 at 12:44

1 Answers1

4
  1. You can first query the counter for its current value and then issue an update to subtract that amount.

  2. You can delete the counter, and then start a new counter using a different row key. You would no longer try to use the original counter. With this approach you might want to partition your counters by day or by week, so that when a new day started, you'd start with a fresh set of zeroed out counters.

Jim Meyer
  • 9,275
  • 1
  • 24
  • 49
  • Thanks, but I have lots of records to reset, query all of them isn't good, and I just want to re-compute the same day's count, so must have the same row key...seems that I should give up using counter column. – ray Aug 06 '15 at 07:51