cassandra:: 我可以创建一个没有主键的 table 吗?

cassandra:: can i create a table without a primarykey?

现在我正在学习Cassandra,所以我得到了一个没有主键的table。但是它有一些索引。

所以这是我的疑问,我可以创建一个没有主键的 table 吗?

CREATE TABLE subscription (subscriberid varchar,productid varchar,panaccessproductid varchar,operatorproductid varchar,price float,fallback varchar,paymenttype varchar,operatorid varchar,subscriptiontype varchar,expiry timestamp,subscriptionstatus varchar,created timestamp);

没有primarykey,subscriberid,productid,operatorid,subscriptiontype是索引。这可能吗?

来自文档

Primary Key:: A primary key identifies the location and order of data storage. The primary key is defined at table creation time and cannot be altered. If the primary key must be changed, a new table schema is created and the data is written to the new table. Cassandra is a partition row store, and a component of the primary key, the partition key, identifies which node will hold a particular table row. At the minimum, the primary key must consist of a partition key. Composite partition keys can split a data set so that related data is stored on separate partitions. Compound primary keys include clustering columns which order the data on a partition. The definition of a table's primary key is critical in Cassandra. Carefully model how data in a table will be inserted and retrieved before choosing which columns will define the primary key. The size of the partitions, the order of the data within partitions, the distribution of the partitions amongst the nodes of the cluster - all of these considerations determine selection of the best primary key for a table.

简单的回答是否定的,主键是强制性的

Cassandra 不是关系数据库。以您打算使用索引的方式使用索引在 Cassandra 中效果不佳。这是真实的主要原因是 Cassandra 是为集群中有数十、数百或数千台服务器的用例而设计的——它使用主键的第一部分(分区键)来确定哪些服务器拥有该服务器数据。 Cassandra 的二级索引(你提到要使用的)是节点本地的——要使用这些,Cassandra 必须向集群中的每个服务器询问查询,将查询的影响乘以集群中的每个节点。

因此,与其创建一个包含 subscriberid、productid、operatorid 和 subscriptiontype 索引的 table,不如创建 4 个 table,每个索引一个,其中分区键是 subscriberid 、productid、operatorid 或 subscriptiontype。当你查询时,cassandra 会确切地知道哪个服务器拥有数据,而无需询问集群的其余部分。

是的,这确实重复了很多数据 - 这称为非规范化,在 Cassandra 中很常见。

在未来的版本(3.4 及更高版本)中,您将能够使用 "SASI",一种新形式的 Cassandra 索引,它可以显着帮助您的用例,而且所需的非规范化要少得多。

你不能在没有主键的情况下在 Cassandra 中创建 table,但是如果你想保存你的数据,你可以在你的 table 中添加一个额外的列(假设 "pk") 数据类型为 UUID。

示例:

CREATE TABLE subscription (pk uuid PRIMARY KEY, subscriberid varchar,productid varchar,panaccessproductid varchar,operatorproductid varchar,price float,fallback varchar,paymenttype varchar,operatorid varchar,subscriptiontype varchar,expiry timestamp,subscriptionstatus varchar,created timestamp);

并且可以插入如下数据:

INSERT INTO subscription(pk, subscriberid,...) VALUES(uuid(), 'S123',...);