Cassandra中的数据模型用于递归结构

时间:2016-12-21 22:31:34

标签: recursion cassandra protocol-buffers data-modeling

protobuf消息' sessionproto'我收到的字段是递归的。

itemrelationproto引用itemgroupproto和itemgroupproto引用itemrelationproto。

如何在Cassandra中定义数据模型来存储这些数据?

感谢。

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 个答案:

答案 0 :(得分:2)

Cassandra中的数据建模与您要存储的对象无关,而与您要对数据执行的查询有关。

以下链接可能会有所帮助:

上面博客文章中的这句话总结得非常好:

  

不要围绕关系建模。不要围绕物体建模。围绕您的查询建模。

因此,如果不知道您要执行哪些查询,则无法建议正确的数据模型。

答案 1 :(得分:1)

Cassandra不是关系数据库,因为您无法存储引用彼此的项目。所以在Cassandra中无法进行递归。

但是你要做的是递归地定义一个当前不可能的类型。我建议的解决方案是将您的proto转换为字节数组或json或其他任何内容,并将其存储在textblob字段中。另一个解决方案是创建多个表并分别存储每个消息,但是您需要多个请求才能选择整个sessionproto

相关问题