What are the alternative ways to model M:M relations in Cassandra?

偶尔善良 提交于 2019-12-31 20:44:10

问题


Consider a M:M relation that needs to be represented in a Cassandra data store.

What M:M modeling options are available? For each alternative, when is it to prefer? What M:M modeling choices have you made in your Cassandra powered projects?


回答1:


Instead of using a join table the way you would with an rdbms, you would have one ColumnFamily containing a row for each X and a list of Ys associated with it, then a CF containing a row for each Y and a list of each X associated with it.

If it turns out you don't really care about querying one of those directions then only keep the CF that you do care about.




回答2:


Cassandra by design is Key value database, so to achieve M:M there are two ways to do it.

  1. De-normalize your data so every relation ship should duplicate data.

    ie. x->y(value) and x->z(value) and a->y(value)

    y should be saved for x and a

    This is how it should be done as it's give you strength of database

  2. Save reference for relational key as value.

    x->y(key) and x->z(Key) and a->y(Key)

    So if you need x with value of y it should be two operation, get x which will give you value of y. Then get y itself in a separate operation.

Cassandra is not RDBMS so don't wrap you mind around traditional way of doing it by dropping values and define relationship.



来源:https://stackoverflow.com/questions/2573106/what-are-the-alternative-ways-to-model-mm-relations-in-cassandra

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!