cassandra - 了解 Cassandra 复合键

标签 cassandra cql

我刚看了this youtube video of Patrick McFadin关于 cassandra 数据建模。

有一张 table ,如下:

create table user_activity_history {
  username varchar,
  interaction_date varchar,
  activity_code varchar,
  detail varchar,
  PRIMARY KEY((username,interaction_date),interaction_time)
);

为什么是主键((username,interaction_date),interaction_time) .
这与 (username,interaction_date,interaction_time) 有何不同.

最佳答案

区别与表的partition_key有关.通常,PRIMARY KEY 中的第一个元素也是分区键 - 这定义了数据在集群中的物理位置,例如,通过使用以下内容:

PRIMARY KEY(username,interaction_date,interaction_time)

插入表中的数据将根据 username 进行分区(物理定位) ,而通过使用以下内容:
PRIMARY KEY((username,interaction_date),interaction_time)

它将根据 username,interaction_date 进行分区组合。后一种方案的优点是与单个 username 相关的数据。可以跨集群中的节点存储。

CREATE TABLE 上的 datastax 的 CQL 文档中有关于 partition_keys 的更多详细信息:

When you use a compound PRIMARY KEY Cassandra treats the first column declared in a definition as the partition key and stores all columns of the row on the same physical node. When you use a composite partition key, Cassandra treats the columns in nested parentheses as partition keys and stores columns of a row on more than one node. You declare a composite partition key using an extra set of parentheses to define which columns partition the data.

关于cassandra - 了解 Cassandra 复合键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19557188/

相关文章:

Cassandra opscenter 未与集群通信

c# - Cassandra NumberFormatException : Zero length BigInteger

java - Cassandra-CQL ClusterBuilder 的 addContactPoints 的工作

cassandra - UUID 或 Integer 是作为分区键的好选择吗?

cassandra - Cassandra 集群中出现意外的 UnavailableException

cassandra - 选择与特定日期对应的 timeuuid 列

python - django shell、django Rest Framework 序列化器和 cassandra 中的内存泄漏

Cassandra:插入 timeuuid 错误

mysql - 执行 CQL 查询语句时收到错误警报 "invalid string"

scala - 如何在play框架中通过scala连接到cassandra