Dimas Rizky
Dimas Rizky

Reputation: 374

Storing counter inside cassandra collection

I want to store aggregation data from my sensor, here's my schema plan for cassandra table

UPDATED

CREATE TABLE every_second_aggregate_signature(
    device_id text,
    year int,
    month int,
    day int,
    hour int,
    minute int,
    second int,
    signature map<text,counter>,
    PRIMARY KEY ((device_id, year, month, day, hour, minute, second))
)WITH CLUSTERING ORDER BY (year DESC, month DESC, day DESC, hour DESC, minute DESC, second DESC);

The signature data is in form of increment value and dynamic value, ex.

ts1 - {"4123sad" : 80, "djas10" : 99}
ts2 - {"4123sad" : 83, "djas10" : 103}
ts3 - {"4123sad" : 87, "djas10" : 198, "asfac9" : 281}
ts4 - {"4123sad" : 89, "djas10" : 201, "asfac9" : 540, "asd81" : 12}

The problem is I knew that cassandra didn't support counter inside the collection. Are there any alternative approach or solution to this problem ? Thanks for your help

Upvotes: 0

Views: 313

Answers (1)

Alex Ott
Alex Ott

Reputation: 87349

The only alternative approach here is to move the "key" of the map into primary key of the table. Right now you're trying to model like this (as I understood):

create table counters (
  ts timestamp primary key,
  counters map<text, counter>
);

Then you'll need to change it to following:

create table counters (
  ts timestamp,
  key text,
  counter counter,
  primary key (ts, key)
);

And to select all values that should go into map, you simply do

select ts, key, counter from counters where ts = 'some value';

it will return you every pair of key/counter for given ts in the separate rows, so you'll need to have a code that merge them into map...

Upvotes: 1

Related Questions