使用CQL3对Cassandra 2中的多对多关系进行建模

时间:2021-07-20 16:53:20

What is the canonical way to model many-to-many relations with CQL3 ? Let's say I have to tables

使用CQL3建立多对多关系的规范方法是什么?假设我有桌子

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>

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 个解决方案

#1


4  

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.

在Cassandra中执行此操作的正确方法是将数据非规范化为2个表。您不必担心必须在每个表上写两次,因为Cassandra旨在快速处理写入以支持此类模型。

Take a look at this data modelling tutorials that will help understanding these things:

看看这些有助于理解这些内容的数据建模教程:

Data modelling tutorials

数据建模教程

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

我也看到你提到了套装。作为旁注,虽然它不是您的问题的答案,但您可能希望了解一些新功能,例如:http://www.datastax.com/dev/blog/cql-in-2-1

#2


1  

The way to achieve it is denormalizing data creating an actors_by_fans and a fans_by_actors. You can also use sets but this have limitations you already mentioned.

实现它的方法是对数据进行非规范化,从而创建一个actors_by_fans和一个fans_by_actors。您也可以使用集合,但这有您已经提到的限制。

HTH, Carlo

HTH,卡罗

#1


4  

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.

在Cassandra中执行此操作的正确方法是将数据非规范化为2个表。您不必担心必须在每个表上写两次,因为Cassandra旨在快速处理写入以支持此类模型。

Take a look at this data modelling tutorials that will help understanding these things:

看看这些有助于理解这些内容的数据建模教程:

Data modelling tutorials

数据建模教程

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

我也看到你提到了套装。作为旁注,虽然它不是您的问题的答案,但您可能希望了解一些新功能,例如:http://www.datastax.com/dev/blog/cql-in-2-1

#2


1  

The way to achieve it is denormalizing data creating an actors_by_fans and a fans_by_actors. You can also use sets but this have limitations you already mentioned.

实现它的方法是对数据进行非规范化,从而创建一个actors_by_fans和一个fans_by_actors。您也可以使用集合,但这有您已经提到的限制。

HTH, Carlo

HTH,卡罗