在 Amazon Keyspaces 中创建表 - Amazon Keyspaces(Apache Cassandra 兼容)
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

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

在 Amazon Keyspaces 中创建表

在本部分中,将使用控制台、cqlsh 或 Amazon CLI创建表。

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

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

  • 表的名称。

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

  • 表的主键。

    • 分区键 – 必需

    • 聚类列 – 可选

可使用以下过程创建具有指定列、数据类型、分区键和集群列的表。

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

year int award text rank int category text book_title text author text publisher text
使用控制台创建表
  1. 登录并在家中打开 Amazon Keyspaces 控制台。 Amazon Web Services Management Console 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. categoryrank 添加为集群列。聚类列是可选的,决定着每个分区内的排序顺序。

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

    2. 列表中,选择类别。在 Order (顺序) 列表中,选择 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. 使用以下 Amazon 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 语句中的顺序不同。

以下过程使用 Amazon CLI创建包含如下列和数据类型的表。yearaward 列构成分区键,将 categoryrank 作为集群列。

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

以下命令可创建一个名为 book_awards 的表。该表的分区键由 yearaward 两列组成,聚类键由 categoryrank 两列组成,两个聚类列均使用升序排序。(为便于阅读,本部分中的表创建命令的 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)