KNag
KNag

Reputation: 47

Cassandra: how to initialize the counter column with value?

I have to benchmark Cassandra with the Facebook Linkbench. There are two phase during the Benchmark, the load and the request phase. in the Load Phase, Linkbench fill the cassandra tables : nodes, links and counts (for links counting) with default values(graph data).

the count table looks like this:

keyspace.counttable ( link_id bigint, link_type bigint, time bigint, version bigint, count counter, PRIMARY KEY (link_id, link_type, time, version)

my question is how to insert the default counter values (before incrementing and decrementing the counter in the Linkbench request phase) ?

If it isn't possible to do that with cassandra, how should i increment/decrement a bigint variable (instead of counter variable)

Any suggest and comments? Thanks a lot.

Upvotes: 2

Views: 1786

Answers (3)

KNag
KNag

Reputation: 47

Thank you for the Answers. I implemented the following solution to initialize the counter Field. as the initial and default value of the counter Field is 0 ,i incremented it with my default value. it looks like Don Branson solution but with only one column:

create table counttable ( link_id bigint, link_type bigint, count counter, PRIMARY KEY (link_id, link_type) );

i set the value with this statement (during the load phase):

update counttable set count = count + myValue where link_id = 1 and link_type = 1;

select * from counttable ;

 link_id | link_type | count
---------+-----------+--------
       1 |         1 |  myValue (added to 0)

(1 row)

Upvotes: 0

medvekoma
medvekoma

Reputation: 1181

There is no elegant way to initialize a counter column with a non-zero value. The only operation you can do on a counter field is increment/decrement. I recommend to keep the offset (e.g. the your intended initial value) in a different column, and simply add the two values in your client application.

Upvotes: 1

Don Branson
Don Branson

Reputation: 13707

The default value is zero. Given

create table counttable ( link_id bigint, link_type bigint, time bigint, version bigint, count counter, PRIMARY KEY (link_id, link_type, time, version) );

and

update counttable set count = count + 1 where link_id = 1 and link_type = 1 and time = 1 and version = 1;

We see that the value of count is now 1.

select * from counttable ; link_id | link_type | time | version | count ---------+-----------+------+---------+------- 1 | 1 | 1 | 1 | 1 (1 rows)

So, if we want to set it to some other value we can:

update counttable set count = count + 500 where link_id = 1 and link_type = 1 and time = 1 and version = 2; select * from counttable ; link_id | link_type | time | version | count ---------+-----------+------+---------+------- 1 | 1 | 1 | 1 | 1 1 | 1 | 1 | 2 | 500 (2 rows)

Upvotes: 1

Related Questions