0

Is there any other way to implement counters in Cassandra ?

I have a following table structure

CREATE TABLE userlog (
term text,
ts timestamp,
year int,
month int,
day int,
hour int,
weekofyear int,
dayofyear int,
count counter,
PRIMARY KEY (term, ts, year,month,day,hour,weekofyear,dayofyear)
);

But because of counter I need to put all the others columns in primary key,which is creating problems to my application.

So,is there any other way where I can avoid doing this (preferably using Java)?

Ritesh Sinha
  • 820
  • 5
  • 22
  • 50

2 Answers2

0

You can avoid counters in Cassandra altogether by using an analytics engine such as Spark. The idea is to only store events in Cassandra and either periodically trigger Spark or continuously run Spark as a background job that would read the events and create aggregates such as counts. Those aggregate results can be written back into Cassandra again into a separate table (e.g. userlog_by_month, userlog_by_week,..).

Stefan Podkowinski
  • 5,206
  • 1
  • 20
  • 25
  • I am using `apache storm` which is somewhat similar to `spark` to write data into Cassandra.Wouldn't it complicate things ? – Ritesh Sinha Jul 29 '15 at 07:41
  • Is there a way where i can read the count variable and update it by adding 1 to it.Something what counter do in a efficient manner.Since, I'll be getting high volume of data in a minute. – Ritesh Sinha Jul 29 '15 at 07:45
  • Storm should be working instead of Spark as well. Yes, the described approach requires more effort compared to using a counter data type supported by Cassandra or other datastores. But it gets you a lot more flexibility which might be worth it. – Stefan Podkowinski Jul 29 '15 at 07:56
0

Usually you would put the counter column in a separate table from the data table. In that way you can use whatever key you find convenient to access the counters.

The downside is you need to update two tables rather than just one, but this is unavoidable due to the way counters are implemented.

Jim Meyer
  • 9,275
  • 1
  • 24
  • 49