如何将 Scylla DB 中的计数器列重置为零?

Agu*_*tto 2 database counter cassandra nosql scylla

是否可以重置计数器列Scylla DB

在 中Cassandra不可能直接将计数器列重置为零:

There is no operation that will allow resetting the counter value. You need to read the value and decrement it using that value.

Be aware that this operation might not be successful since the counter can be changed between your "reset" operations.
Run Code Online (Sandbox Code Playgroud)

这也是真的吗Scylla DB

小智 6

Scylla 中也有类似的行为。我们无法将计数器值重置为 0,计数器只能递增或递减。

scylla@cqlsh:ks> CREATE TABLE cf (pk int PRIMARY KEY, my_counter counter);
scylla@cqlsh:ks> UPDATE cf SET my_counter = my_counter + 3 WHERE pk = 0;
scylla@cqlsh:ks> SELECT * FROM cf;

 pk | my_counter
----+------------
  0 |          3

(1 rows)
scylla@cqlsh:ks> UPDATE cf SET my_counter = 0  WHERE pk = 0;
InvalidRequest: Error from server: code=2200 [Invalid query] message="Cannot set the value of counter column my_counter (counters can only be incremented/decremented, not set)"
scylla@cqlsh:ks> UPDATE cf SET my_counter = my_counter - 3 WHERE pk = 0;
scylla@cqlsh:ks> SELECT * FROM cf;

 pk | my_counter
----+------------
  0 |          0

(1 rows)
Run Code Online (Sandbox Code Playgroud)

请注意尝试直接设置值时的错误。

更多相关信息:

https://docs.scylladb.com/using-scylla/counters/#scylla-counters

https://docs.scylladb.com/getting-started/types/#counters

https://www.scyladb.com/2017/04/04/counters/