Pinnacle
Pinnacle

Reputation: 165

nested map in cassandra data modelling

I have following requirement of my dataset, need to unserstand what datatype should I use and how to save my data accordingly :-

CREATE TABLE events (
id text,
evntoverlap map<text, map<timestamp,int>>,
PRIMARY KEY (id)
) 

evntoverlap = {
               'Dig1': {{'2017-10-09 04:10:05', 0}}, 
               'Dig2': {{'2017-10-09 04:11:05', 0},{'2017-10-09 04:15:05', 0}},
               'Dig3': {{'2017-10-09 04:11:05', 0},{'2017-10-09 04:15:05', 0},{'2017-10-09 04:11:05', 0}}
              }

This gives an error :-

Error from server: code=2200 [Invalid query] message="Non-frozen collections are not allowed inside collections: map<text, map<timestamp, int>>"

How should I store this type of data in single column . Please suggest datatype and insert command for the same. Thanks,

Upvotes: 1

Views: 3648

Answers (2)

Chris Lohfink
Chris Lohfink

Reputation: 16410

CQL collections limited to 64kb, if putting things like maps in maps you might push that limit. Especially with frozen maps you are deserializing the entire map, modifying it, and re inserting. Might be better off with a

CREATE TABLE events (
  id text,
  evnt_key, text
  value map<timestamp, int>,
  PRIMARY KEY ((id), evnt_key)
  ) 

Or even a

CREATE TABLE events (
  id text,
  evnt_key, text
  evnt_time timestamp
  value int,
  PRIMARY KEY ((id), evnt_key, evnt_time)
  )

It would be more efficient and safer while giving additional benefits like being able to order the event_time's in ascending or descending order.

Upvotes: 1

Alex Ott
Alex Ott

Reputation: 87249

There is limitation of Cassandra - you can't nest collection (or UDT) inside collection without making it frozen. So you need to "froze" one of the collections - either nested:

CREATE TABLE events (
id text,
evntoverlap map<text, frozen<map<timestamp,int>>>,
PRIMARY KEY (id)
);

or top-level:

CREATE TABLE events (
id text,
evntoverlap frozen<map<text, map<timestamp,int>>>,
PRIMARY KEY (id)
);

See documentation for more details.

Upvotes: 2

Related Questions