zohar
zohar

Reputation: 2378

How to change the default value of Cassandra counter

In Cassandra counter, if I try to increase counter in none existing row , it creates the row set the value to zero and then increase the value as requested.

My question is can I change the default value not to be zero but something else. for example: previous bucket or something similar (row2 = row1+value)

Upvotes: 3

Views: 1856

Answers (2)

Yurii
Yurii

Reputation: 321

You can set initial value for counter like this:

    CREATE TABLE counters (
      counter_name text,
      counter_value counter,
      PRIMARY KEY (counter_name)
    );

    UPDATE counters SET counter_value = counter_value + 100500 WHERE counter_name='FooCounter';
    SELECT * from counters;

     counter_name | counter_value
    --------------+---------------
       FooCounter |        100500

After that use counter as you need:

    UPDATE counters SET counter_value = counter_value + 1 WHERE counter_name='FooCounter';
    SELECT * from counters;

     counter_name | counter_value
    --------------+---------------
       FooCounter |        100501

Upvotes: 2

BioSafety
BioSafety

Reputation: 94

You can't set the value of a counter only increment or decrement.

A counter column value is a 64-bit signed integer. You cannot set the value of a counter, which supports two operations: increment and decrement.

http://docs.datastax.com/en/cql/3.1/cql/cql_reference/counter_type.html

Upvotes: 5

Related Questions