

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

# 使用连接 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。

在 Amazon Key AWS CLI spaces 中使用之前，您必须先获得访问密钥 ID 和私有访问密钥。有关更多信息，请参阅 [为 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)。

以下命令可创建一个名为 *catalog* 的密钥空间。

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

该命令将在输出中返回资源的 Amazon 资源名称 (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'
```