Data model in Cassandra for a recursive structure

1.1k Views Asked by At

The protobuf message 'sessionproto' that I receive has a field that is recursive.

itemrelationproto references itemgroupproto and itemgroupproto references itemrelationproto .

How do I define a data model in Cassandra to store this data?

Thanks.

message itemrelationproto {
    optional string id = 1;
    optional itemgroupproto itemgroup = 2;
}

message itemgroupproto {
    optional string id = 1;
    optional string displayname = 2;
    repeated itemrelationproto itemrelations = 3;
}

message sessionproto {
    optional string sessionid = 1;
    optional string displayname = 3;
    repeated itemrelationproto itemrelations = 4;
}

create type itemrelationproto (
 id text,
 itemgroup frozen<itemgroupproto>
);

create type itemgroupproto (
 id text,
 displayname text,
 itemrelations set<frozen<itemrelationproto>>
);

create table sessionproto (
 sessionid text,
 displayname text,
 itemrelations set<frozen<itemrelationproto>,
 primary key (sessionid)
);
2

There are 2 best solutions below

1
On

Cassandra is not a relational database as you cannot store items that reference each others. So there is no way of doing recursion in Cassandra.

But what you are trying to do is to define a type recursively which is not currently possible. The solution I suggest is to convert your proto into a byte array or json or whatever else and to store it in a text or blob field. Another solution is to create multiple tables and to store each message separately but you will need several requests to select the whole sessionproto.

0
On

Data modelling in Cassandra is not about the objects you want to store but about the queries you want to perform on your data.

The following links might be helpful:

This statement from the blog post above sums it up very well:

Don’t model around relations. Don’t model around objects. Model around your queries.

Therefore without knowing what queries you want to execute a proper data model can't be suggested.