问题描述
使用CQL3建模多对多关系的规范方法是什么?
假设我要上表
What is the canonical way to model many-to-many relations with CQL3 ?Let's say I have to tables
CREATE TABLE actor (
id text PRIMARY KEY,
given text,
surname text,
)
CREATE TABLE fan (
id text PRIMARY KEY,
given text,
surname text,
)
我想模拟一个演员可以拥有许多演员的事实粉丝,每个粉丝都可以喜欢许多演员。
and I'd like to model the fact that an actor can have many fan and each fan can like many actors.
我的第一个想法是使用,如下所示(对于粉丝来说则相反):
The first idea that came to my my was to use sets, like in the following (and the other way around for fans):
CREATE TABLE actor (
id text PRIMARY KEY,
given text,
surname text,
fans set<text>
)
<similarly for fan>
但似乎它们是为小型设备准备的,我没有找到一种方法来检查
but it seems they are meant for small sets, and I don't see a way to check if a fan is related to an actor without loading either set completely.
我发现的第二个选择是制作两个映射表,每个映射表用于每个关系方向:
The second choice I found would be to make two mapping tables, each for each relation direction:
CREATE TABLE actor_fan (
text actor,
text fan,
PRIMARY KEY(actor,fan)
);
<similarly for fan_actor>
这是否能让我同时获得演员的粉丝名单和检查特定人物的能力是给定演员的粉丝吗?
关于Cassandra的文档很多,但通常与较旧的版本有关,发行版之间似乎有很多差异。
Would this give me the ability to get both the fan list of an actor and check if a specific person is a fan of a given actor ?There is a lot of documentation about Cassandra, but it is often related to older versions and there seem to be lot of differences between the releases.
推荐答案
在Cassandra中执行此操作的正确方法是将数据反规范化为2个表。您不必担心必须在每个表上写两次,因为Cassandra旨在非常快速地处理写操作以支持这种模型。
The proper way to do this in Cassandra is denormalizing the data into 2 tables. You shouldn't worry about having to write twice, once on each table, as Cassandra is designed to handle writes very fast to support such model.
看看这个数据建模教程,将有助于理解这些内容:
Take a look at this data modelling tutorials that will help understanding these things:
我也看到了您提到的集合。只是作为一个旁注,尽管它不能解决您的问题,但您可能希望了解一些新功能,例如:
Also I see you mentioned sets as well. Just as a side note and although it is not an answer to your questions, you might want to be aware of some new features like: http://www.datastax.com/dev/blog/cql-in-2-1
这篇关于使用CQL3在Cassandra 2中建模多对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!