

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

# 在 Amazon Keyspaces 中创建表
<a name="getting-started.tables"></a>

在本部分中，将使用控制台、`cqlsh` 或 AWS CLI创建表。

表是组织和存储数据的位置。表的主键决定如何在表中对数据进行分区。主键由一个必需的分区键和一个或多个可选的聚类列组成。组成主键的组合值在表的所有数据中必须是唯一的。有关表的更多信息，请参阅以下主题：
+ 分区键设计：[如何在 Amazon Keyspaces 中高效使用分区键](bp-partition-key-design.md)
+ 使用表：[在 Amazon Keyspaces 中查看表的创建状态](tables-create.md)
+ CQL 语言参考中的 DDL 语句：[表](cql.ddl.table.md)
+ 表资源管理：[在 Amazon Keyspaces（Apache Cassandra 兼容）中管理无服务器资源](serverless_resource_management.md)
+ 监控表资源利用率：[使用亚马逊监控亚马逊密钥空间 CloudWatch](monitoring-cloudwatch.md)
+ [Amazon Keyspaces（Apache Cassandra 兼容）限额](quotas.md)

创建表时，应指定以下内容：
+ 表的名称。
+ 表中每一列的名称和数据类型。
+ 表的主键。
  + **分区键** – 必需
  + **聚类列** – 可选

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

## 使用控制台
<a name="getting-started.tables.con"></a>

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

```
year           int
award          text
rank           int 
category       text
book_title     text
author         text
publisher      text
```

**使用控制台创建表**

1. [登录并在家中打开 Amazon Keyspaces 控制台。 AWS 管理控制台 https://console.aws.amazon.com/keyspaces/](https://console.aws.amazon.com/keyspaces/home)

1. 在导航窗格中，选择 **Keyspaces (键空间)**。

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

1. 选择**创建表**。

1. 在 **Table name (表名称)** 框中，输入 **book\$1awards** 作为表的名称。

   **名称约束：**
   + 名称不能为空。
   + 允许的字符：字母数字字符和下划线 (`_`)。
   + 最大长度为 48 个字符。

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

   添加以下列和数据类型。

   ```
   year           int
   award          text
   rank           int 
   category       text
   book_title     text
   author         text
   publisher      text
   ```

   1. **名称** – 输入列的名称。

      **名称约束：**
      + 名称不能为空。
      + 允许的字符：字母数字字符和下划线 (`_`)。
      + 最大长度为 48 个字符。

   1. **类型** – 在数据类型列表中，为此列选择数据类型。

   1. 要添加其他列，请选择**添加列**。

1. 在**分区键**下，选择 `award` 和 `year` 作为分区键。每个表都需要一个分区键。分区键可以由一列或多列构成。

1. 将 `category` 和 `rank` 添加为**集群列**。聚类列是可选的，决定着每个分区内的排序顺序。

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

   1. 在**列**列表中，选择**类别**。在 **Order (顺序)** 列表中，选择 **ASC (升序)** 按照升序对此列中的值进行排序。（选择 **DESC (降序)** 可按降序排列。）

   1. 然后选择**添加集群列**并选择**排名**。

1. 在**表设置**部分中，选择**默认设置**。

1. 选择**创建表**。

1. 验证表是否已创建。

   1. 在导航窗格中，选择**表**。

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

   1. 选择表的名称。

   1. 确认所有列和数据类型都正确无误。
**注意**  
列的顺序可能与添加到表中的顺序不同。

## 使用 CQL
<a name="getting-started.tables.cql"></a>

以下过程使用 CQL 创建包含如下列和数据类型的表。`year` 和 `award` 列是将 `category` 和 `rank` 作为集群列的分区键，它们共同构成表的主键。

```
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.
   ```

1. 在键空间提示符 (`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` 按降序排列。

   请注意，`year` 和 `award` 是分区键列。然后，`category` 和 `rank` 是按升序（`ASC`）排列的集群列。这些列共同构成表的主键。

1. 验证表是否已创建。

   ```
   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)
   ```

1. 验证表的结构。

   ```
   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
<a name="getting-started.tables.cli"></a>

以下过程使用 AWS CLI创建包含如下列和数据类型的表。`year` 和 `award` 列构成分区键，将 `category` 和 `rank` 作为集群列。

```
year           int
award          text
rank           int 
category       text
book_title     text
author         text
publisher      text
```

**要使用创建表 AWS CLI**

以下命令可创建一个名为 *book\$1awards* 的表。该表的分区键由 `year` 和 `award` 两列组成，聚类键由 `category` 和 `rank` 两列组成，两个聚类列均使用升序排序。（为便于阅读，本部分中的表创建命令的 `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:111122223333:/keyspace/catalog/table/book_awards"
   }
   ```

1. 要确认表的元数据和属性，您可以使用以下命令。

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

   此命令将返回以下输出。

   ```
   {
       "keyspaceName": "catalog",
       "tableName": "book_awards",
       "resourceArn": "arn:aws:cassandra:us-east-1:111122223333:/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）](getting-started.dml.md)。