如何在cassandra中与table自身建立多对多关系

how to establish a many to many relationship with itself of a table in cassandra

实际上,我正在开发一个用户 table 可以与其自身建立多对多关系的应用程序。

这是一个应用程序,特定用户可以在其中向不同的买家出售商品,并可以从不同的卖家那里购买商品。 也就是说,一个特定的用户可以有很多买家和很多卖家。

作为用户正在向其他用户销售东西。 用户成为该特定用户的卖家,而该特定用户成为该用户的买家。

我是 cassandra 的新手,我不知道 cassandra 中的关系是如何工作的。

任何人都可以告诉我我应该怎么做才能建立这种关系

我的用户table如下:-

CREATE TABLE IF NOT EXISTS user (
    id text,
    login text,
    password text,
    firstName text,
    lastName text,
    gender text,
    mobileNo text,
    aadhar text,
    email text,
    stateCode text,
    district text,
    city text,
    zipCode int,
    address text,
    PRIMARY KEY(id)
);

Cassandra 是一种 NoSQL 数据库,它的一个特点是存储非结构化数据。因此,您不应该考虑 table 的关系,而是考虑您希望如何获取此信息(换句话说,您的 "select * from" 字段是什么)。

你应该看看 this cassandra data modeling introduction;

在那之后,你会注意到两件事:

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

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

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 的思维方式不同。

另一个例子: 创建 table transactions_by_item_name ( item_name 文字, seller_id 文字, seller_name 文字, buyer_id 文字, buyer_name 文字, 日期时间戳, 项目文本, item_description, ...,

要在您的写入中完成原子事务(因为您可能不得不在多个 table 中写入相同的信息)是使用 batch statements