【问题标题】:Cassandra create duplicate table with different primary keyCassandra 创建具有不同主键的重复表
【发布时间】:2020-09-14 03:38:34
【问题描述】:

我是 Apache Cassandra 的新手,遇到以下问题:

我有一张PRIMARY KEY (userid, countrycode, carid) 的桌子。如许多教程中所述,可以使用以下过滤条件查询此表:

  • 用户 ID = x
  • 用户 ID = x 和国家代码 = y
  • userid = x 和 countrycode = y 和 carid = z

这在大多数情况下都很好,但现在我需要通过仅过滤来查询表

  • userid = x 和 carid = z

这里,文档说这是创建另一个具有修改的主键的表的最佳解决方案,在本例中为 PRIMARY KEY (userid, carid, countrycode)

这里的问题是,如何将“原始”表中的数据复制到具有不同索引的新表中?

  • 在小桌子上
  • 在大桌子上

还有一个关于复制大表的重要问题:保存两个表而不是只保存一个表所需的存储空间如何?

【问题讨论】:

    标签: cassandra cql cqlsh


    【解决方案1】:

    您可以使用 COPY 命令从一个表导出并导入到另一个表。

    从您的示例中 - 我创建了 2 个表。 user_country 和 user_car 具有各自的主键。

    CREATE KEYSPACE user WITH REPLICATION = { 'class' : 'NetworkTopologyStrategy',  'datacenter1' : 2 } ;
    CREATE TABLE user.user_country ( user_id text, country_code text, car_id text, PRIMARY KEY (user_id, country_code, car_id));
    CREATE TABLE user.user_car ( user_id text, country_code text, car_id text, PRIMARY KEY (user_id, car_id, country_code));
    

    让我们在一张表中插入一些虚拟数据。

    cqlsh> INSERT INTO user.user_country (user_id, country_code, car_id) VALUES ('1', 'IN', 'CAR1');
    cqlsh> INSERT INTO user.user_country (user_id, country_code, car_id) VALUES ('2', 'IN', 'CAR2');
    cqlsh> INSERT INTO user.user_country (user_id, country_code, car_id) VALUES ('3', 'IN', 'CAR3');
    cqlsh> select * from user.user_country ;
    
     user_id | country_code | car_id
    ---------+--------------+--------
           3 |           IN |   CAR3
           2 |           IN |   CAR2
           1 |           IN |   CAR1
    
    (3 rows)
    

    现在我们将数据导出为 CSV。 观察提到的列的顺序。

    cqlsh> COPY user.user_country (user_id,car_id, country_code) TO 'export.csv';
    Using 1 child processes
    
    Starting copy of user.user_country with columns [user_id, car_id, country_code].
    Processed: 3 rows; Rate:       4 rows/s; Avg. rate:       4 rows/s
    3 rows exported to 1 files in 0.824 seconds.
    

    export.csv 现在可以直接插入到其他表中。

    cqlsh> COPY user.user_car(user_id,car_id, country_code) FROM 'export.csv';
    Using 1 child processes
    
    Starting copy of user.user_car with columns [user_id, car_id, country_code].
    Processed: 3 rows; Rate:       6 rows/s; Avg. rate:       8 rows/s
    3 rows imported from 1 files in 0.359 seconds (0 skipped).
    cqlsh>
    cqlsh>
    cqlsh> select * from user.user_car ;
    
     user_id | car_id | country_code
    ---------+--------+--------------
           3 |   CAR3 |           IN
           2 |   CAR2 |           IN
           1 |   CAR1 |           IN
    
    (3 rows)
    cqlsh>
    

    关于您的其他问题 - 是的,数据将被复制,但这就是 cassandra 的使用方式。

    【讨论】:

    • 很棒的答案。你说的完全正确……Cassandra 就是为了性能而交易磁盘空间。
    • 很好的答案,谢谢!但出口到例如CSV 对于大表来说非常麻烦,不是吗?
    • 这肯定会很耗时,并且在导出/导入发生时可能会出现 CPU 峰值。您始终可以使用批量大小、超时等来调整 COPY 命令。但鉴于这将是一次匹配 2 个表的任务,应该没问题。在此之后,将数据插入现有表的应用程序也必须将数据插入新表。
    • COPY 在大表上会失败。它也不会正确处理元数据
    猜你喜欢
    • 1970-01-01
    • 2021-12-07
    • 2017-02-09
    • 2013-09-04
    • 1970-01-01
    • 2018-07-11
    • 1970-01-01
    • 2016-03-25
    • 2015-03-29
    相关资源
    最近更新 更多