使用 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 Keyspaces 中创建和管理多区域密钥空间和表。

本节提供了如何使用创建和管理多区域表的 Amazon CLI示例。您在多区域密钥空间中创建的所有表都会自动从密钥空间继承多区域设置。

有关本主题中描述的 Amazon Key Amazon CLI spaces 命令的更多信息,请参阅《亚马逊密钥空间Amazon CLI 命令参考》。

创建新的多区域键空间 (CLI)

要创建多区域键空间,您可以使用以下 CLI 语句。请指定您当前的区域和 regionList 中的至少一个其他区域。

aws keyspaces create-keyspace --keyspace-name mykeyspace \ --replication-specification replicationStrategy=MULTI_REGION,regionList=us-east-1,eu-west-1
注意

当您创建多区域键空间时,Amazon Keyspaces 会在您的账户中创建一个名为 AWSServiceRoleForAmazonKeyspacesReplication 的服务相关角色。此角色允许 Amazon Keyspaces 代表您将写入复制到多区域表的所有副本。要了解更多信息,请参阅使用角色进行 Amazon Keyspaces 多区域复制

使用默认设置创建新的多区域表 (CLI)

要使用默认设置创建多区域表,您只需要指定架构即可。你可以使用以下示例。

aws keyspaces create-table --keyspace-name mykeyspace --table-name mytable \ --schema-definition 'allColumns=[{name=pk,type=int}],partitionKeys={name= pk}'

该命令的输出是:

{ "resourceArn": "arn:aws:cassandra:us-east-1:111122223333:/keyspace/mykeyspace/table/mytable" }

要确认表的设置,可以使用以下语句。

aws keyspaces get-table --keyspace-name mykeyspace --table-name mytable

输出显示了多区域表的所有默认设置。

{ "keyspaceName": "mykeyspace", "tableName": "mytable", "resourceArn": "arn:aws:cassandra:us-east-1:111122223333:/keyspace/mykeyspace/table/mytable", "creationTimestamp": "2023-12-19T16:50:37.639000+00:00", "status": "ACTIVE", "schemaDefinition": { "allColumns": [ { "name": "pk", "type": "int" } ], "partitionKeys": [ { "name": "pk" } ], "clusteringKeys": [], "staticColumns": [] }, "capacitySpecification": { "throughputMode": "PAY_PER_REQUEST", "lastUpdateToPayPerRequestTimestamp": "2023-12-19T16:50:37.639000+00:00" }, "encryptionSpecification": { "type": "AWS_OWNED_KMS_KEY" }, "pointInTimeRecovery": { "status": "DISABLED" }, "defaultTimeToLive": 0, "comment": { "message": "" }, "clientSideTimestamps": { "status": "ENABLED" }, "replicaSpecifications": [ { "region": "us-east-1", "status": "ACTIVE", "capacitySpecification": { "throughputMode": "PAY_PER_REQUEST", "lastUpdateToPayPerRequestTimestamp": 1702895811.469 } }, { "region": "eu-north-1", "status": "ACTIVE", "capacitySpecification": { "throughputMode": "PAY_PER_REQUEST", "lastUpdateToPayPerRequestTimestamp": 1702895811.121 } } ] }

使用 auto Scaling (CLI) 在预配置模式下创建新的多区域表

要在预配置模式下创建具有 auto Scaling 配置的多区域表,可以使用。 Amazon CLI请注意,您必须使用 Amazon Keyspaces CLI create-table 命令来配置多区域自动扩展设置。这是因为 Amazon Keyspaces 用来代表您执行自动扩展的服务 Application Auto Scaling 不支持多个区域。

有关 auto Scaling 设置、目标跟踪策略、目标值和可选设置的更多信息,请参阅使用创建具有自动缩放功能的新表 Amazon CLI

当您在预配置模式下使用自动缩放设置创建新的多区域表时,您可以为该表指定对复制 Amazon Web Services 区域 该表的所有内容有效的常规设置。然后,您可以覆盖每个副本的读取容量设置并读取 auto Scaling 设置。但是,写入容量在所有副本之间保持同步,以确保有足够的容量跨所有区域复制写入。

要定义特定区域中表副本的读取容量,您可以将以下参数配置为表的一部分replicaSpecifications

  • 区域

  • 预配置的读取容量单位(可选)

  • 读取容量的自动缩放设置(可选)

当您使用复杂的自动缩放设置和不同的表副本配置创建预配置的多区域表时,从 JSON 文件加载表的自动缩放设置和副本配置会很有帮助。

要使用以下代码示例,您可以从 auto-scaling.zip 下载示例 JSON 文件,然后提取auto-scaling.jsonreplication.json。记下文件的路径。

在此示例中,JSON 文件位于当前目录中。有关不同的文件路径选项,请参阅如何从文件加载参数

aws keyspaces create-table --keyspace-name mykeyspace --table-name mytable \ --schema-definition 'allColumns=[{name=pk,type=int},{name=ck,type=int}],partitionKeys=[{name=pk},{name=ck}]' \ --capacity-specification throughputMode=PROVISIONED,readCapacityUnits=1,writeCapacityUnits=1 \ --auto-scaling-specification file://auto-scaling.json \ --replica-specifications file://replication.json

更新多区域表 (CLI) 的预配置容量和 auto Scaling 设置

要更新现有表的预配置模式和 auto Scaling 配置,您可以使用 Amazon CLI update-table命令。

请注意,您必须使用 Amazon Keyspaces CLI 命令来创建或修改多区域自动扩展设置。这是因为 Amazon Keyspaces 用来代表您自动扩展表容量的服务 Application Auto Scaling 不支持多个。 Amazon Web Services 区域

更新多区域表的预配置模式或自动扩展设置时,可以更新表每个副本的读取容量设置和读取自动扩展配置。

但是,写入容量在所有副本之间保持同步,以确保有足够的容量跨所有区域复制写入。要更新特定区域中表副本的读取容量,您可以更改表的以下可选参数之一replicaSpecifications

  • 预配置的读取容量单位(可选)

  • 读取容量的自动缩放设置(可选)

当您使用复杂的自动缩放设置和不同的表副本配置来更新多区域表时,从 JSON 文件加载表的自动缩放设置和副本配置会很有帮助。

要使用以下代码示例,您可以从 auto-scaling.zip 下载示例 JSON 文件,然后提取auto-scaling.jsonreplication.json。记下文件的路径。

在此示例中,JSON 文件位于当前目录中。有关不同的文件路径选项,请参阅如何从文件加载参数

aws keyspaces update-table --keyspace-name mykeyspace --table-name mytable \ --capacity-specification throughputMode=PROVISIONED,readCapacityUnits=1,writeCapacityUnits=1 \ --auto-scaling-specification file://auto-scaling.json \ --replica-specifications file://replication.json

查看多区域表 (CLI) 的预配置容量和 auto Scaling 设置

要查看多区域表的 auto Scaling 配置,您可以使用get-table-auto-scaling-settings操作。以下 CLI 命令就是一个示例。

aws keyspaces get-table-auto-scaling-settings --keyspace-name mykeyspace --table-name mytable

您应当看到如下输出。

{ "keyspaceName": "mykeyspace", "tableName": "mytable", "resourceArn": "arn:aws:cassandra:us-east-1:777788889999:/keyspace/mykeyspace/table/mytable", "autoScalingSpecification": { "writeCapacityAutoScaling": { "autoScalingDisabled": false, "minimumUnits": 5, "maximumUnits": 10, "scalingPolicy": { "targetTrackingScalingPolicyConfiguration": { "disableScaleIn": false, "scaleInCooldown": 0, "scaleOutCooldown": 0, "targetValue": 50.0 } } }, "readCapacityAutoScaling": { "autoScalingDisabled": false, "minimumUnits": 5, "maximumUnits": 20, "scalingPolicy": { "targetTrackingScalingPolicyConfiguration": { "disableScaleIn": false, "scaleInCooldown": 60, "scaleOutCooldown": 60, "targetValue": 70.0 } } } }, "replicaSpecifications": [ { "region": "us-east-1", "autoScalingSpecification": { "writeCapacityAutoScaling": { "autoScalingDisabled": false, "minimumUnits": 5, "maximumUnits": 10, "scalingPolicy": { "targetTrackingScalingPolicyConfiguration": { "disableScaleIn": false, "scaleInCooldown": 0, "scaleOutCooldown": 0, "targetValue": 50.0 } } }, "readCapacityAutoScaling": { "autoScalingDisabled": false, "minimumUnits": 5, "maximumUnits": 20, "scalingPolicy": { "targetTrackingScalingPolicyConfiguration": { "disableScaleIn": false, "scaleInCooldown": 60, "scaleOutCooldown": 60, "targetValue": 70.0 } } } } }, { "region": "eu-north-1", "autoScalingSpecification": { "writeCapacityAutoScaling": { "autoScalingDisabled": false, "minimumUnits": 5, "maximumUnits": 10, "scalingPolicy": { "targetTrackingScalingPolicyConfiguration": { "disableScaleIn": false, "scaleInCooldown": 0, "scaleOutCooldown": 0, "targetValue": 50.0 } } }, "readCapacityAutoScaling": { "autoScalingDisabled": false, "minimumUnits": 5, "maximumUnits": 10, "scalingPolicy": { "targetTrackingScalingPolicyConfiguration": { "disableScaleIn": false, "scaleInCooldown": 60, "scaleOutCooldown": 60, "targetValue": 50.0 } } } } } ] }

关闭多区域表的自动缩放 (CLI)

您可以使用 Amazon CLI update-table命令关闭现有表的 auto 缩放。请注意,您无法为单个表副本禁用 auto 缩放。

在以下示例中,针对表的读取容量关闭了 auto Scaling。

aws keyspaces update-table --keyspace-name mykeyspace --table-name mytable \ --auto-scaling-specification readCapacityAutoScaling={autoScalingDisabled=true}
注意

要删除 Application Auto Scaling 使用的服务相关角色,必须禁用账户中所有表的自动缩放功能。 Amazon Web Services 区域

手动设置多区域表的预配置容量 (CLI)

如果您必须关闭多区域表的 auto Scaling,则可以使用update-table手动为副本表配置表的读取容量。

aws keyspaces update-table --keyspace-name mykeyspace --table-name mytable \ --capacity-specification throughputMode=PROVISIONED,readCapacityUnits=1,writeCapacityUnits=1 \ --replica-specifications region="us-east-1",readCapacityUnits=5
注意

我们建议对使用预配置容量的多区域表使用 auto scaling。有关更多信息,请参阅 使用 Amazon Keyspaces 中的多区域表