

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 在 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)
+ 監控資料表資源使用率： [使用 Amazon CloudWatch 監控 Amazon Keyspaces](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. 登入 AWS 管理主控台，並在 https：//[https://console.aws.amazon.com/keyspaces/home](https://console.aws.amazon.com/keyspaces/home) 開啟 Amazon Keyspaces 主控台。

1. 在導覽窗格中，選擇 **金鑰空間**。

1. 選擇 `catalog`做為您要建立此資料表的金鑰空間。

1. 選擇 **Create Table** (建立資料表)。

1. 在**資料表名稱**方塊中，輸入 **book\$1awards**做為資料表的名稱。

   **名稱限制條件：**
   + 名稱不可為空白。
   + 允許字元：英數字元和底線 `_` ()。
   + 長度上限為 48 個字元。

1. 在**資料欄**區段中，針對您要新增至此表格的每個資料欄重複下列步驟。

   新增下列資料欄和資料類型。

   ```
   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. 若要新增叢集資料欄，請選擇**新增叢集資料欄**。

   1. 在**資料欄**清單中，選擇**類別**。在**順序**清單中，選擇 **ASC** 以遞增順序排序此欄中的值。（選擇遞減順序的 **DESC**。)

   1. 然後選取**新增叢集欄**，然後選擇**排名**。

1. 在**資料表設定**區段中，選擇**預設設定**。

1. 選擇 **Create Table** (建立資料表)。

1. 確認您的資料表已建立。

   1. 在導覽窗格中，選擇 **Tables** (資料表)。

   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)