Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cassandra 为什么集群键被命名为;“群集键”;在卡桑德拉?_Cassandra_Data Modeling - Fatal编程技术网

Cassandra 为什么集群键被命名为;“群集键”;在卡桑德拉?

Cassandra 为什么集群键被命名为;“群集键”;在卡桑德拉?,cassandra,data-modeling,Cassandra,Data Modeling,表1: create table mylistofitems (listid int, itemid int, quantity int, itemdesc text, primary key ((listid, itemid), itemdesc)); create table myitems (listid int, itemid int, idesc text, qty int, primary key (listid, itemid));

表1:

create table mylistofitems (listid int, 
  itemid int, 
  quantity int, 
  itemdesc text, 
  primary key ((listid, itemid), itemdesc));
create table myitems (listid int,
  itemid int, 
  idesc text, 
  qty int,
  primary key (listid, itemid));
在上表中,我做了以下插入:

insert into mylistofitems (listid, itemid, itemdesc, quantity) values (1, 1000, 'apple', 5);
insert into mylistofitems (listid, itemid, itemdesc, quantity) values (1, 1000, 'banana', 10);
insert into mylistofitems (listid, itemid, itemdesc, quantity) values (1, 1000, 'orange', 6);
insert into mylistofitems (listid, itemid, itemdesc, quantity) values (1, 1000, 'orange', 50);
当我从MyListFitems中选择*时,我得到以下信息:

 listid | itemid | itemdesc | quantity
--------+--------+----------+----------
      1 |   1000 |    apple |        5
      1 |   1000 |   banana |       10
      1 |   1000 |   orange |       50
UPDATE mylistofitems SET quantity = 100 WHERE listid = 1 AND itemid = 1000 AND idesc = 'orange';
第二条insert语句没有覆盖第一行。但是第四条insert语句覆盖了第三行

在这种情况下,集群键的含义是什么

表2:

create table mylistofitems (listid int, 
  itemid int, 
  quantity int, 
  itemdesc text, 
  primary key ((listid, itemid), itemdesc));
create table myitems (listid int,
  itemid int, 
  idesc text, 
  qty int,
  primary key (listid, itemid));
我在表2中插入以下记录:

insert into myitems (listid, itemid, idesc, qty) values (1, 1000,
'apple', 5);
insert into myitems (listid, itemid, idesc, qty) values (1, 1000, 'banana', 10);
insert into myitems (listid, itemid, idesc, qty) values (1, 1000, 'orange', 6);
insert into myitems (listid, itemid, idesc, qty) values (1, 1000, 'orange', 50);
表2中的Insert查询与表1完全相同。 但当我从myitems中选择*时,我惊讶地发现最后插入的只有一行。其余所有行都将丢失。即,每个insert语句都重写了以前的记录

 listid | itemid | idesc  | qty
--------+--------+--------+-----
      1 |   1000 | orange |  50
问题: 为什么表2中的行为与表1中的不同? 在这种情况下,集群键的含义是什么?为什么集群密钥被命名为“集群密钥”。这和cassandra集群有什么关系吗

更新问题: 我尝试对表1进行更新:

update mylistofitems set quantity = 100 where listid = 1 and itemid = 1000;
这表示错误2200缺少一些群集密钥。为什么这会受到限制?

什么是群集密钥? 集群键指示数据如何存储在磁盘上。这也是卡桑德拉表现如此出色的原因之一。因为列的顺序改变了数据的存储方式,所以您必须知道它们是在Cassandra的内部进行管理的

将磁盘上的数据可视化为阵列。这就是Cassandra存储数据的有效方式。这是前3个查询后第一个表的外观:

table1 =
(listid(1) - itemid(1000)) // Partition key
    idesc('apple') // Clustering key
        = {listid: 1, itemid: 1000, idesc: apple, qty: 5}
    idesc('banana') // Clustering key
        = {listid: 1, itemid: 1000, idesc: banana, qty: 10}
    idesc('orange') // Clustering key
        = {listid: 1, itemid: 1000, idesc: orange, qty: 6}
在第四次插入时,它将使用每个集群键(或本例中的索引)遍历数据,以找到要覆盖的最后一段数据。因此,在第四次插入之后,它将如下所示:

table1 =
(listid(1) - itemid(1000)) // Partition key
    idesc('apple') // Clustering key
        = {listid: 1, itemid: 1000, idesc: apple, qty: 5}
    idesc('banana') // Clustering key
        = {listid: 1, itemid: 1000, idesc: banana, qty: 10}
    idesc('orange') // Clustering key
        = {listid: 1, itemid: 1000, idesc: orange, qty: 50}
table1 =
(listid(1) - itemid(1000)) // Partition key
    idesc('apple') // Clustering key
        quantity(5) // Clustering key
            = {listid: 1, itemid: 1000, idesc: 'apple', qty: 5}
    idesc('banana') // Clustering key
        quantity(10) // Clustering key
            = {listid: 1, itemid: 1000, idesc: 'banana', qty: 10}
    idesc('orange') // Clustering key
        quantity(6) // Clustering key
            = {listid: 1, itemid: 1000, idesc: 'orange', qty: 6}
        quantity(50 // Clustering key
            = {listid: 1, itemid: 1000, idesc: 'orange', qty: 50}
缺少分区/群集密钥 以下面的查询和我像Cassandra一样访问数据的示例为例

  • 其中listid在(1,2)中,itemid=1000

    result=(数据[1-1000],数据[2-1000])

  • 其中listid=1,itemid=1000,idesc='apple'

    result=data[1-1000][apple']

  • 其中idesc='apple'

    result=data[??]['apple']

    C*不知道搜索哪个索引
    apple

  • 重要的是要注意,插入或更新数据时也是如此。让我们以您的
    UPDATE
    查询为例

    UPDATE mylistofitems SET quantity = 100 WHERE listid = 1 AND itemid = 1000;
    
    通过此查询,您将尝试执行以下操作:

    `data[1-1000][????] = {listid: 1, itemid: 1000, idesc:????, qty: 1000}`
    
    C*不知道在哪个索引中存储数据

    您应该将查询更新为以下内容:

     listid | itemid | itemdesc | quantity
    --------+--------+----------+----------
          1 |   1000 |    apple |        5
          1 |   1000 |   banana |       10
          1 |   1000 |   orange |       50
    
    UPDATE mylistofitems SET quantity = 100 WHERE listid = 1 AND itemid = 1000 AND idesc = 'orange';
    
    在数组形式中,如下所示:

    `data[1-1000]['orange'] = {listid: 1, itemid: 1000, idesc: 'orange', qty: 1000}`
    
    添加数量作为集群键 如果添加
    quantity
    作为聚类键,则数据结构如下所示:

    table1 =
    (listid(1) - itemid(1000)) // Partition key
        idesc('apple') // Clustering key
            = {listid: 1, itemid: 1000, idesc: apple, qty: 5}
        idesc('banana') // Clustering key
            = {listid: 1, itemid: 1000, idesc: banana, qty: 10}
        idesc('orange') // Clustering key
            = {listid: 1, itemid: 1000, idesc: orange, qty: 50}
    
    table1 =
    (listid(1) - itemid(1000)) // Partition key
        idesc('apple') // Clustering key
            quantity(5) // Clustering key
                = {listid: 1, itemid: 1000, idesc: 'apple', qty: 5}
        idesc('banana') // Clustering key
            quantity(10) // Clustering key
                = {listid: 1, itemid: 1000, idesc: 'banana', qty: 10}
        idesc('orange') // Clustering key
            quantity(6) // Clustering key
                = {listid: 1, itemid: 1000, idesc: 'orange', qty: 6}
            quantity(50 // Clustering key
                = {listid: 1, itemid: 1000, idesc: 'orange', qty: 50}
    
    这将允许您为每个组合拥有多行,尽管您将无法拥有具有相同数据的多行

    一般规则
  • 分区+群集键是每行的唯一字段
  • 如果不在查询中包含以前的键,则无法按键进行查询
  • Cassandra没有INSERT/update-only UPSERT
  • 插入行时,必须指定所有键
  • 什么是集群密钥? 集群键指示数据如何存储在磁盘上。这也是卡桑德拉表现如此出色的原因之一。因为列的顺序改变了数据的存储方式,所以您必须知道它们是在Cassandra的内部进行管理的

    将磁盘上的数据可视化为阵列。这就是Cassandra存储数据的有效方式。这是前3个查询后第一个表的外观:

    table1 =
    (listid(1) - itemid(1000)) // Partition key
        idesc('apple') // Clustering key
            = {listid: 1, itemid: 1000, idesc: apple, qty: 5}
        idesc('banana') // Clustering key
            = {listid: 1, itemid: 1000, idesc: banana, qty: 10}
        idesc('orange') // Clustering key
            = {listid: 1, itemid: 1000, idesc: orange, qty: 6}
    
    在第四次插入时,它将使用每个集群键(或本例中的索引)遍历数据,以找到要覆盖的最后一段数据。因此,在第四次插入之后,它将如下所示:

    table1 =
    (listid(1) - itemid(1000)) // Partition key
        idesc('apple') // Clustering key
            = {listid: 1, itemid: 1000, idesc: apple, qty: 5}
        idesc('banana') // Clustering key
            = {listid: 1, itemid: 1000, idesc: banana, qty: 10}
        idesc('orange') // Clustering key
            = {listid: 1, itemid: 1000, idesc: orange, qty: 50}
    
    table1 =
    (listid(1) - itemid(1000)) // Partition key
        idesc('apple') // Clustering key
            quantity(5) // Clustering key
                = {listid: 1, itemid: 1000, idesc: 'apple', qty: 5}
        idesc('banana') // Clustering key
            quantity(10) // Clustering key
                = {listid: 1, itemid: 1000, idesc: 'banana', qty: 10}
        idesc('orange') // Clustering key
            quantity(6) // Clustering key
                = {listid: 1, itemid: 1000, idesc: 'orange', qty: 6}
            quantity(50 // Clustering key
                = {listid: 1, itemid: 1000, idesc: 'orange', qty: 50}
    
    缺少分区/群集密钥 以下面的查询和我像Cassandra一样访问数据的示例为例

  • 其中listid在(1,2)中,itemid=1000

    result=(数据[1-1000],数据[2-1000])

  • 其中listid=1,itemid=1000,idesc='apple'

    result=data[1-1000][apple']

  • 其中idesc='apple'

    result=data[??]['apple']

    C*不知道搜索哪个索引
    apple

  • 重要的是要注意,插入或更新数据时也是如此。让我们以您的
    UPDATE
    查询为例

    UPDATE mylistofitems SET quantity = 100 WHERE listid = 1 AND itemid = 1000;
    
    通过此查询,您将尝试执行以下操作:

    `data[1-1000][????] = {listid: 1, itemid: 1000, idesc:????, qty: 1000}`
    
    C*不知道在哪个索引中存储数据

    您应该将查询更新为以下内容:

     listid | itemid | itemdesc | quantity
    --------+--------+----------+----------
          1 |   1000 |    apple |        5
          1 |   1000 |   banana |       10
          1 |   1000 |   orange |       50
    
    UPDATE mylistofitems SET quantity = 100 WHERE listid = 1 AND itemid = 1000 AND idesc = 'orange';
    
    在数组形式中,如下所示:

    `data[1-1000]['orange'] = {listid: 1, itemid: 1000, idesc: 'orange', qty: 1000}`
    
    添加数量作为集群键 如果添加
    quantity
    作为聚类键,则数据结构如下所示:

    table1 =
    (listid(1) - itemid(1000)) // Partition key
        idesc('apple') // Clustering key
            = {listid: 1, itemid: 1000, idesc: apple, qty: 5}
        idesc('banana') // Clustering key
            = {listid: 1, itemid: 1000, idesc: banana, qty: 10}
        idesc('orange') // Clustering key
            = {listid: 1, itemid: 1000, idesc: orange, qty: 50}
    
    table1 =
    (listid(1) - itemid(1000)) // Partition key
        idesc('apple') // Clustering key
            quantity(5) // Clustering key
                = {listid: 1, itemid: 1000, idesc: 'apple', qty: 5}
        idesc('banana') // Clustering key
            quantity(10) // Clustering key
                = {listid: 1, itemid: 1000, idesc: 'banana', qty: 10}
        idesc('orange') // Clustering key
            quantity(6) // Clustering key
                = {listid: 1, itemid: 1000, idesc: 'orange', qty: 6}
            quantity(50 // Clustering key
                = {listid: 1, itemid: 1000, idesc: 'orange', qty: 50}
    
    这将允许您为每个组合拥有多行,尽管您将无法拥有具有相同数据的多行

    一般规则
  • 分区+群集键是每行的唯一字段
  • 如果不在查询中包含以前的键,则无法按键进行查询
  • Cassandra没有INSERT/update-only UPSERT
  • 插入行时,必须指定所有键

  • 我想回答我自己的问题来结束这篇文章。也为了帮助其他有同样困惑的人:

    基本上我是在浏览主键、分区键、集群键的概念

    表1的主键是:

    primary key ((listid, itemid), itemdesc));
    
    这意味着listid+itemid只是一个分区键,帮助记录找到它的节点

    只有listid+itemid+itemdesc的组合才会保持实际的唯一性

    总之:

    listid+itemid = composite partition key
    listid+itemid+itemdesc = composite primary key
    itemdesc = clustering key 
    
    (聚类键仅用于对每个项目下的项目进行排序