Reputation: 840
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)?
Upvotes: 0
Views: 842
Reputation: 9475
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.
Upvotes: 0
Reputation: 5249
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,..).
Upvotes: 0