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

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

使用 Amazon CLI

您可以使用 Amazon Command Line Interface (Amazon CLI) 从命令行管理多个 Amazon 服务并通过脚本自动执行这些服务。使用 Amazon Keyspaces,您可以使用 Amazon CLI 进行数据定义语言 (DDL) 操作,例如创建表格。此外,您还可以使用基础设施即代码 (IaC) 服务和工具,如 Amazon CloudFormation 和 Terraform。

您必先获取访问密钥 ID 和秘密访问密钥,然后才能将 Amazon CLI 与 Amazon Keyspaces 结合使用。有关更多信息,请参阅如何为 Amazon Keyspaces 创建和配置 Amazon 凭证

有关 Amazon CLI 中可用于 Amazon Keyspaces 的所有命令的完整列表,请参阅 Amazon CLI 命令参考

下载和配置 Amazon CLI

Amazon CLI 在 https://www.amazonaws.cn/cli 上提供。它在 Windows、macOS 或 Linux 上运行。下载 Amazon CLI 后,请按照以下步骤进行安装和配置:

将 Amazon CLI 与 Amazon Keyspaces 结合使用

命令行格式包含 Amazon Keyspaces 操作名称,后跟该操作的参数。Amazon CLI 支持参数值的速记语法以及 JSON。以下 Amazon Keyspaces 示例使用了 Amazon CLI 速记语法。有关详细信息,请参阅将速记语法与 Amazon CLI 结合使用。

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

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

该命令将在输出中返回资源的 Amazon 资源名称 (ARN)。

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

要确认密钥空间目录是否存在,您可以使用以下命令。

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

该命令将在输出中返回以下值。

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

以下命令可创建一个名为 book_awards 的表。该表的分区键由 yearaward 两列组成,聚类键由 categoryrank 两列组成,两个聚类列均使用升序排序。(为便于阅读,本部分中的长命令分行显示。)

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

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

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

此命令将返回以下输出。

{ "keyspaceName": "catalog", "tableName": "book_awards", "resourceArn": "arn:aws:cassandra:us-east-1:111222333444:/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_definition.zip 下载架构定义示例 JSON 文件并提取 schema_definition.json,记下文件路径。在本示例中,按机构定义 JSON 文件位于当前目录下。有关不同的文件路径选项,请参阅如何从文件加载参数

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

以下示例展示了如何创建一个名为 myTable 的简单表(带有其他选项)。请注意,为提高可读性,命令被分成了多个独立的行。该命令显示了如何创建表以及如何执行以下操作:

  • 设置表的容量模式

  • 为表启用时间点恢复

  • 将表的默认生存时间 (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 以允许您为列和行设置过期日期。要运行此示例,您必须使用自己的密钥替换客户托管的 Amazon 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:111222333444:key/11111111-2222-3333-4444-555555555555' --ttl 'status=ENABLED'