

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

# 使用 AWS CLI 連線至 Amazon Keyspaces
<a name="access.cli"></a>

 您可以使用 AWS Command Line Interface (AWS CLI) 從命令列控制多個 AWS 服務，並透過指令碼將其自動化。透過 Amazon Keyspaces，您可以使用 AWS CLI 進行資料定義語言 (DDL) 操作，例如建立資料表。此外，您可以使用基礎設施做為程式碼 (IaC) 服務和工具，例如 AWS CloudFormation 和 Terraform。

您必須先取得存取金鑰 ID 和私密存取金鑰，才能 AWS CLI 搭配 Amazon Keyspaces 使用 。如需詳細資訊，請參閱[建立和設定 Amazon Keyspaces 的 AWS 登入資料](access.credentials.md)。

如需 中可用於 Amazon Keyspaces 的所有命令的完整清單 AWS CLI，請參閱 [https://awscli.amazonaws.com/v2/documentation/api/latest/reference/keyspaces/index.html](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/keyspaces/index.html)。

**Topics**
+ [下載和設定 AWS CLI](#access.cli.installcli)
+ [AWS CLI 搭配 Amazon Keyspaces 使用](#access.cli.usingcli)

## 下載和設定 AWS CLI
<a name="access.cli.installcli"></a>

 AWS CLI 可在 取得[https://aws.amazon.com/cli](https://aws.amazon.com/cli)。它可在 Windows、macOS，或 Linux 上執行。下載 之後 AWS CLI，請依照下列步驟進行安裝和設定：

1. 前往 [AWS Command Line Interface 使用者指南](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html)

1. 遵循[安裝 AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html)和[設定 AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html)的指示

## AWS CLI 搭配 Amazon Keyspaces 使用
<a name="access.cli.usingcli"></a>

命令列格式包含 Amazon Keyspaces 操作名稱，後面接著該操作的參數。 AWS CLI 支援參數值以及 JSON 的速記語法。下列 Amazon Keyspaces 範例使用 AWS CLI 速記語法。如需詳細資訊，請參閱[搭配 CLI AWS 使用速記語法](https://docs.aws.amazon.com/cli/latest/userguide/cli-usage-shorthand.html)。

下列命令會使用 名稱*目錄*建立金鑰空間。

```
aws keyspaces create-keyspace --keyspace-name 'catalog'
```

命令會在輸出中傳回資源 Amazon Resource Name (ARN)。

```
{
    "resourceArn": "arn:aws:cassandra:us-east-1:111122223333:/keyspace/catalog/"
}
```

若要確認金鑰空間*目錄*是否存在，您可以使用下列命令。

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

命令的輸出會傳回下列值。

```
{
    "keyspaceName": "catalog",
    "resourceArn": "arn:aws:cassandra:us-east-1:111122223333:/keyspace/catalog/"
}
```

下列命令會建立名稱為 *book\$1awards* 的資料表。資料表的分割區索引鍵由欄 `year``award`和 組成，叢集索引鍵由欄 `category`和 組成`rank`，兩個叢集欄都使用遞增排序順序。(為確保易讀性，本節的長命令以分行顯示。)

```
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"
}
```

若要確認資料表的中繼資料和屬性，您可以使用下列命令。

```
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": 1645564368.628,
    "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": 1645564368.628
    },
    "encryptionSpecification": {
        "type": "AWS_OWNED_KMS_KEY"
    },
    "pointInTimeRecovery": {
        "status": "DISABLED"
    },
    "ttl": {
        "status": "ENABLED"
    },
    "defaultTimeToLive": 0,
    "comment": {
        "message": ""
    }
}
```

建立具有複雜結構描述的資料表時，從 JSON 檔案載入資料表的結構描述定義會很有幫助。以下是此範例。從 [schema\$1definition.zip](samples/schema_definition.zip) 下載結構描述定義範例 JSON 檔案，並擷取 `schema_definition.json`，並記下檔案的路徑。在此範例中，結構描述定義 JSON 檔案位於目前的 目錄中。如需不同的檔案路徑選項，請參閱[如何從檔案載入參數](https://docs.aws.amazon.com/cli/latest/userguide/cli-usage-parameters-file.html#cli-usage-parameters-file-how)。

```
aws keyspaces create-table --keyspace-name 'catalog' 
            --table-name 'book_awards' --schema-definition 'file://schema_definition.json'
```

下列範例示範如何建立名為 *myTable* 的簡單資料表，以及其他選項。請注意，命令會細分為不同的資料列，以改善可讀性。此命令說明如何建立資料表和：
+ 設定資料表的容量模式
+ 啟用資料表的Point-in-time復原
+ 將資料表的預設存留時間 (TTL) 值設定為一年
+ 為資料表新增兩個標籤

```
aws keyspaces create-table --keyspace-name 'catalog' --table-name 'myTable' 
            --schema-definition 'allColumns=[{name=id,type=int},{name=name,type=text},{name=date,type=timestamp}],partitionKeys=[{name=id}]' 
            --capacity-specification 'throughputMode=PROVISIONED,readCapacityUnits=5,writeCapacityUnits=5' 
            --point-in-time-recovery 'status=ENABLED' 
            --default-time-to-live '31536000' 
            --tags 'key=env,value=test' 'key=dpt,value=sec'
```

此範例說明如何建立新的資料表，該資料表使用客戶受管金鑰進行加密，並啟用 TTL 以允許您設定資料欄和資料列的過期日期。若要執行此範例，您必須將客戶受管 AWS KMS 金鑰的資源 ARN 取代為您自己的金鑰，並確保 Amazon Keyspaces 可以存取該金鑰。

```
aws keyspaces create-table --keyspace-name 'catalog' --table-name 'myTable' 
            --schema-definition 'allColumns=[{name=id,type=int},{name=name,type=text},{name=date,type=timestamp}],partitionKeys=[{name=id}]' 
            --encryption-specification 'type=CUSTOMER_MANAGED_KMS_KEY,kmsKeyIdentifier=arn:aws:kms:us-east-1:111122223333:key/11111111-2222-3333-4444-555555555555'  
            --ttl 'status=ENABLED'
```