如何在cassandra中与自己建立多对多的关系

时间:2017-08-29 11:03:36

标签: database-design cassandra data-modeling database nosql

实际上我正在使用app,其中用户表可以与自己有多对多的关系。

这是一个应用程序,其中特定用户可以向不同的买家出售东西,并可以从不同的卖家购买东西。 也就是说,特定用户可以拥有许多买家和许多卖家。

用户正在向其他用户销售产品。 用户成为该特定用户的卖家&该特定用户成为该用户的买家。

我对cassandra很新,而且我不知道cassandra的关系是如何工作的。

AnyOne可以告诉我该如何建立这种关系

我的用户表格如下: -

<httpHandlers>
    <add path="*.cshtml" verb="*" type="System.Web.HttpNotFoundHandler"/>
</httpHandlers>

1 个答案:

答案 0 :(得分:1)

Cassandra是一个NoSQL数据库,其中一个特殊的特性就是存储非结构化数据。所以,你不应该考虑表的关系,而是想想你如何获得这些信息(换句话说,你的“select * from”字段是什么)。

你应该看看this cassandra data modeling introduction;

之后,您会注意到两件事:

  1. 主键在cassandra存储中起着巨大作用
  2. 您只能使用主键
  3. 过滤搜索(where子句)

    您正在寻找的是每个搜索cql都有一个表:

    create table sells_by_user_id (
    user_id text,
    buyer_id text,
    date timestamp,
    item text,
    item_description,
    ..., <this will depend on which info you would like to obtain
    primary key ((user_id), date)); //with this table you will obtain all the things that a user sold and to whom
    
    create table buys_by_user_id (
    user_id text,
    seller_id text,
    date timestamp,
    item text,
    item_description,
    ..., <this will depend on which info you would like to obtain
    primary key ((user_id), date)); //this table will store all things that an user bought 
    

    正如您所看到的,它与传统的RDBMS有着不同的心态。

    另一个例子: create table transactions_by_item_name(     item_name文本,     seller_id文字,     seller_name文字,     buyer_id文字,     buyer_name文字,     日期时间戳,     项目文字,     商品描述,     ...

    要完成写入的原子事务(因为您可能需要在多个表中编写相同的信息),请使用batch statements

相关问题