ray
ray

Reputation: 21

How to re-insert record with counter column after delete it in Cassandra?

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?

Upvotes: 2

Views: 1506

Answers (1)

Jim Meyer
Jim Meyer

Reputation: 9475

  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.

Upvotes: 5

Related Questions