【问题标题】:Data model in Cassandra for a recursive structureCassandra 中用于递归结构的数据模型
【发布时间】:2017-05-07 11:32:49
【问题描述】:

我收到的 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)
);

【问题讨论】:

  • 您可以将您的 protobuf 消息转换为字节数组并将它们作为 blob 存储在 cassandra 中。

标签: recursion cassandra protocol-buffers data-modeling


【解决方案1】:

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

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

【讨论】:

  • 感谢 DineMartine。感谢您的回复。这是我的想法,但想探索所有可能性并确保我朝着正确的方向前进。
【解决方案2】:

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

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

上面博文中的这句话总结得很好:

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

因此,在不知道要执行哪些查询的情况下,无法建议执行适当的数据模型。

【讨论】:

    猜你喜欢
    • 2015-01-26
    • 1970-01-01
    • 2021-08-17
    • 2010-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-27
    • 2018-04-01
    相关资源
    最近更新 更多