在 Amazon Keyspaces 中创建表 - Amazon Keyspaces(Apache Cassandra 兼容)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

在 Amazon Keyspaces 中创建表

在本节中,您将使用控制台创建表cqlsh、或 AWS CLI.

表是组织和存储数据的位置。表的主键决定了数据在表中的分区方式。主键由一个必需的分区键和一个或多个可选的聚类列组成。组成主键的组合值在表的所有数据中必须是唯一的。有关表的更多信息,请参阅以下主题:

创建表时,应指定以下内容:

  • 表的名称。

  • 表中每一列的名称和数据类型。

  • 表的主键。

    • 分区键 – 必需

    • 聚类列 – 可选

使用以下步骤创建包含指定列、数据类型、分区键和聚类列的表。

以下过程创建包含如下列和数据类型的表 book_awards

year int award text rank int category text book_title text author text publisher text
使用控制台创建表
  1. 登录 AWS Management Console,然后在家中打开 Amazon Keyspaces 控制台。https://console.aws.amazon.com/keyspaces/

  2. 在导航窗格中,选择 Keyspaces (键空间)

  3. 选择 catalog 作为要在其中创建此表的键空间。

  4. 选择创建表

  5. Table name (表名称) 框中,输入 book_awards 作为表的名称。

    名称限制:

    • 名称不能为空。

    • 允许的字符:字母数字字符和下划线 (_)。

    • 最大长度为 48 个字符。

  6. Columns (列) 部分中,为要添加到此表的每一列重复以下步骤。

    添加以下列和数据类型。

    year int award text rank int category text book_title text author text publisher text
    1. 名称 – 输入列的名称。

      名称限制:

      • 名称不能为空。

      • 允许的字符:字母数字字符和下划线 (_)。

      • 最大长度为 48 个字符。

    2. 类型 – 在数据类型列表中,为此列选择数据类型。

    3. 要添加另一列,请选择添加列

  7. 选择awardyear作为分区键下的分区键。每个表都需要一个分区键。分区键可以由一列或多列构成。

  8. category和添加rank聚类列。聚类列是可选的,决定着每个分区内的排序顺序。

    1. 要添加聚类列,请选择 Add clustering column (添加聚类列)

    2. 在 “列” 列表中,选择类别。在 “序” 列表中 ASC,选择按此列中的值的升序排序。(选择DESC按降序排列。)

    3. 然后选择添加聚类列并选择排名

  9. 表设置部分中,选择默认设置

  10. 选择创建表

  11. 验证表是否已创建。

    1. 在导航窗格中,选择

    2. 确认您的表位于表列表中。

    3. 选择表的名称。

    4. 确认所有列和数据类型都正确无误。

      注意

      列的顺序可能与添加到表中的顺序不同。

此过程使用创建包含以下列和数据类型的表CQL。yearaward列是带有categoryrank作为聚类列的分区键,它们共同构成表的主键。

year int award text rank int category text book_title text author text publisher text
要使用创建表 CQL
  1. 打开 AWS CloudShell 然后使用以下命令连接到 Amazon Keyspaces。请务必更新 us-east-1 使用您自己的区域。

    cqlsh-expansion cassandra.us-east-1.amazonaws.com 9142 --ssl

    该命令应生成如下所示的输出。

    Connected to Amazon Keyspaces at cassandra.us-east-1.amazonaws.com:9142 [cqlsh 6.1.0 | Cassandra 3.11.2 | CQL spec 3.4.4 | Native protocol v4] Use HELP for help. cqlsh current consistency level is ONE.
  2. 在键空间提示符 (cqlsh:keyspace_name>) 处,通过在命令窗口中输入以下代码来创建表。

    CREATE TABLE catalog.book_awards ( year int, award text, rank int, category text, book_title text, author text, publisher text, PRIMARY KEY ((year, award), category, rank) );
    注意

    ASC 是默认的聚类顺序。也可以指定 DESC 按降序排列。

    请注意,yearaward是分区键列。然后,categoryrank是按升序排序的聚类列 (ASC)。这些列共同构成表的主键。

  3. 验证表是否已创建。

    SELECT * from system_schema.tables WHERE keyspace_name='catalog.book_awards' ;

    输出应如下所示:

    keyspace_name | table_name | bloom_filter_fp_chance | caching | cdc | comment | compaction | compression | crc_check_chance | dclocal_read_repair_chance | default_time_to_live | extensions | flags | gc_grace_seconds | id | max_index_interval | memtable_flush_period_in_ms | min_index_interval | read_repair_chance | speculative_retry ---------------+------------+------------------------+---------+-----+---------+------------+-------------+------------------+----------------------------+----------------------+------------+-------+------------------+----+--------------------+-----------------------------+--------------------+--------------------+------------------- (0 rows)>
  4. 验证表的结构。

    SELECT * FROM system_schema.columns WHERE keyspace_name = 'catalog' AND table_name = 'book_awards';

    此语句的输出应类似于此示例。

    keyspace_name | table_name | column_name | clustering_order | column_name_bytes | kind | position | type ---------------+-------------+-------------+------------------+------------------------+---------------+----------+------ catalog | book_awards | year | none | 0x79656172 | partition_key | 0 | int catalog | book_awards | award | none | 0x6177617264 | partition_key | 1 | text catalog | book_awards | category | asc | 0x63617465676f7279 | clustering | 0 | text catalog | book_awards | rank | asc | 0x72616e6b | clustering | 1 | int catalog | book_awards | author | none | 0x617574686f72 | regular | -1 | text catalog | book_awards | book_title | none | 0x626f6f6b5f7469746c65 | regular | -1 | text catalog | book_awards | publisher | none | 0x7075626c6973686572 | regular | -1 | text (7 rows)

    确认所有列和数据类型均符合预期。列的顺序可能与 CREATE 语句中的顺序不同。

此过程使用以下列和数据类型创建一个表 AWS CLI。 yearaward列用categoryrank作为聚类列构成分区键。

year int award text rank int category text book_title text author text publisher text
要使用创建表 AWS CLI

以下命令可创建一个名为 book_awards 的表。该表的分区键由 yearaward 两列组成,聚类键由 categoryrank 两列组成,两个聚类列均使用升序排序。(为了便于阅读,本节中的 table create 命令被分成不同的行。)schema-definition

  1. 您可以使用以下语句创建表。

    aws keyspaces create-table --keyspace-name 'catalog' \ --table-name 'book_awards' \ --schema-definition 'allColumns=[{name=year,type=int},{name=award,type=text},{name=rank,type=int}, {name=category,type=text}, {name=author,type=text},{name=book_title,type=text},{name=publisher,type=text}], partitionKeys=[{name=year},{name=award}],clusteringKeys=[{name=category,orderBy=ASC},{name=rank,orderBy=ASC}]'

    该命令的输出结果如下。

    { "resourceArn": "arn:aws:cassandra:us-east-1:111222333444:/keyspace/catalog/table/book_awards" }
  2. 要确认表的元数据和属性,您可以使用以下命令。

    aws keyspaces get-table --keyspace-name 'catalog' --table-name 'book_awards'

    此命令将返回以下输出。

    { "keyspaceName": "catalog", "tableName": "book_awards", "resourceArn": "arn:aws:cassandra:us-east-1:123SAMPLE012:/keyspace/catalog/table/book_awards", "creationTimestamp": "2024-07-11T15:12:55.571000+00:00", "status": "ACTIVE", "schemaDefinition": { "allColumns": [ { "name": "year", "type": "int" }, { "name": "award", "type": "text" }, { "name": "category", "type": "text" }, { "name": "rank", "type": "int" }, { "name": "author", "type": "text" }, { "name": "book_title", "type": "text" }, { "name": "publisher", "type": "text" } ], "partitionKeys": [ { "name": "year" }, { "name": "award" } ], "clusteringKeys": [ { "name": "category", "orderBy": "ASC" }, { "name": "rank", "orderBy": "ASC" } ], "staticColumns": [] }, "capacitySpecification": { "throughputMode": "PAY_PER_REQUEST", "lastUpdateToPayPerRequestTimestamp": "2024-07-11T15:12:55.571000+00:00" }, "encryptionSpecification": { "type": "AWS_OWNED_KMS_KEY" }, "pointInTimeRecovery": { "status": "DISABLED" }, "defaultTimeToLive": 0, "comment": { "message": "" }, "replicaSpecifications": [] }

要对表中的数据执行CRUD(创建、读取、更新和删除)操作,请继续在 Amazon Keyspaces CQL 中使用创建、读取、更新和删除数据 (CRUD)