How do I increment the value of a non-counter column in Cassandra?

Viewed 289

If I have an Account table with 2 columns such as: Account (id uuid, balance decimal)

how can I achieve the following in Cassandra?

update Account(id, balance) SET balance = balance + 20.0 WHERE id = 80327e25-c32d-4b23-9499-f7bbae76e799 IF balance < 100.0;

I have a scenario where I would like to use Cassandra to keep track of balances in a cluster spanning 3 regional data-centers and be able to update the balance field with QUORUM consistency.

1 Answers

It is not possible to do that in CQL because it requires a read-before-write.

Instead, you need to perform this logic within your application by (1) reading the value of the column first then (2) setting the value with the UPDATE command. Cheers!

Related