How to reset record with counter column after delete it in Cassandra? -
i create table counter column in using com.datastax.driver.core packages, , function in class:
public void dostartingjob(){ session.execute("create keyspace myks replication " + "= {'class':'simplestrategy', 'replication_factor':1};"); session.execute("create table myks.clients_count(ip_address text primary key," + "request_count counter);"); }
after deleted table entry cqlsh like:
jay@jayesh-380:~$ cqlsh connected test cluster @ 127.0.0.1:9042. [cqlsh 5.0.1 | cassandra 3.9 | cql spec 3.4.2 | native protocol v4] use help. cqlsh:myks> delete clients_count ip_address='127.0.0.1';
then to insert row same primary-key used following statement(via cqlsh):
update myks.clients_count set request_count = 1 ip_address ='127.0.0.1';
and is not allowed as:
invalidrequest: error server: code=2200 [invalid query] message="cannot set value of counter column request_count (counters can incremented/decremented, not set)"
but, want value of record's counter column should set 1, , same primary-key. (functional requirement)
how same ??
the usage of counters bit strange, you'll used to. main thing however, counters cannot reused. once delete counter value particular primary key, counter lost forever. design , think not going change.
back question, first of problems initial delete
. don't.
second, if counter value primary key doesn't exists, c* treat it zero default. following the documentation, load data in counter first time have issue:
update mysecurity.clients_count set request_count = request_count + 1 ip_address ='127.0.0.1';
and select
return correct answer: 1
again, beware of deletes! don't! if do, subsequent query:
update mysecurity.clients_count set request_count = request_count + 1 ip_address ='127.0.0.1';
will not fail, counter not updated.
another thing note c* don't support atomic read , update (or update , read on counter columns. cannot issue update , within same query new (or old) value of counter. you'll need perform 2 distinct queries, 1 select
, 1 update
, in multi-client environment select
value not reflect counter value during update
.
your app fail if underestimate this.
Comments
Post a Comment