使用的 DynamoDB 示例 AWS CLI - AWS SDK代码示例

AWS 文档 AWS SDK示例 GitHub 存储库中还有更多SDK示例

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

使用的 DynamoDB 示例 AWS CLI

以下代码示例向您展示了如何在 DynamoDB 中使用来执行操作和实现常见场景。 AWS Command Line Interface

操作是大型程序的代码摘录,必须在上下文中运行。您可以通过操作了解如何调用单个服务函数,还可以通过函数相关场景的上下文查看操作。

每个示例都包含一个指向完整源代码的链接,您可以在其中找到有关如何在上下文中设置和运行代码的说明。

主题

操作

以下代码示例演示如何使用 batch-get-item

AWS CLI

检索表中的多个项

以下 batch-get-items 示例将使用一批三个 GetItem 请求从 MusicCollection 表中读取多个项,并请求该操作所用的读取容量单位数。该命令仅返回 AlbumTitle 属性。

aws dynamodb batch-get-item \ --request-items file://request-items.json \ --return-consumed-capacity TOTAL

request-items.json 的内容:

{ "MusicCollection": { "Keys": [ { "Artist": {"S": "No One You Know"}, "SongTitle": {"S": "Call Me Today"} }, { "Artist": {"S": "Acme Band"}, "SongTitle": {"S": "Happy Day"} }, { "Artist": {"S": "No One You Know"}, "SongTitle": {"S": "Scared of My Shadow"} } ], "ProjectionExpression":"AlbumTitle" } }

输出:

{ "Responses": { "MusicCollection": [ { "AlbumTitle": { "S": "Somewhat Famous" } }, { "AlbumTitle": { "S": "Blue Sky Blues" } }, { "AlbumTitle": { "S": "Louder Than Ever" } } ] }, "UnprocessedKeys": {}, "ConsumedCapacity": [ { "TableName": "MusicCollection", "CapacityUnits": 1.5 } ] }

有关更多信息,请参阅《Amazon DynamoDB 开发人员指南》中的基本操作

以下代码示例演示如何使用 batch-write-item

AWS CLI

向表中添加多个项

以下 batch-write-item 示例将使用一批三个 PutItem 请求向 MusicCollection 表中添加三个新项。它还会请求有关操作所用的写入容量单位数以及操作修改的任何项集合的信息。

aws dynamodb batch-write-item \ --request-items file://request-items.json \ --return-consumed-capacity INDEXES \ --return-item-collection-metrics SIZE

request-items.json 的内容:

{ "MusicCollection": [ { "PutRequest": { "Item": { "Artist": {"S": "No One You Know"}, "SongTitle": {"S": "Call Me Today"}, "AlbumTitle": {"S": "Somewhat Famous"} } } }, { "PutRequest": { "Item": { "Artist": {"S": "Acme Band"}, "SongTitle": {"S": "Happy Day"}, "AlbumTitle": {"S": "Songs About Life"} } } }, { "PutRequest": { "Item": { "Artist": {"S": "No One You Know"}, "SongTitle": {"S": "Scared of My Shadow"}, "AlbumTitle": {"S": "Blue Sky Blues"} } } } ] }

输出:

{ "UnprocessedItems": {}, "ItemCollectionMetrics": { "MusicCollection": [ { "ItemCollectionKey": { "Artist": { "S": "No One You Know" } }, "SizeEstimateRangeGB": [ 0.0, 1.0 ] }, { "ItemCollectionKey": { "Artist": { "S": "Acme Band" } }, "SizeEstimateRangeGB": [ 0.0, 1.0 ] } ] }, "ConsumedCapacity": [ { "TableName": "MusicCollection", "CapacityUnits": 6.0, "Table": { "CapacityUnits": 3.0 }, "LocalSecondaryIndexes": { "AlbumTitleIndex": { "CapacityUnits": 3.0 } } } ] }

有关更多信息,请参阅《Amazon DynamoDB 开发人员指南》中的基本操作

以下代码示例演示如何使用 create-backup

AWS CLI

为现有 DynamoDB 表创建备份

以下create-backup示例创建了MusicCollection表的备份。

aws dynamodb create-backup \ --table-name MusicCollection \ --backup-name MusicCollectionBackup

输出:

{ "BackupDetails": { "BackupArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection/backup/01576616366715-b4e58d3a", "BackupName": "MusicCollectionBackup", "BackupSizeBytes": 0, "BackupStatus": "CREATING", "BackupType": "USER", "BackupCreationDateTime": 1576616366.715 } }

有关更多信息,请参阅《亚马逊 DynamoDB 开发者指南》中的 DynamoDB 按需备份和恢复

以下代码示例演示如何使用 create-global-table

AWS CLI

创建全局表

以下create-global-table示例根据指定的、独立的 AWS 区域中的两个相同表创建全局表。

aws dynamodb create-global-table \ --global-table-name MusicCollection \ --replication-group RegionName=us-east-2 RegionName=us-east-1 \ --region us-east-2

输出:

{ "GlobalTableDescription": { "ReplicationGroup": [ { "RegionName": "us-east-2" }, { "RegionName": "us-east-1" } ], "GlobalTableArn": "arn:aws:dynamodb::123456789012:global-table/MusicCollection", "CreationDateTime": 1576625818.532, "GlobalTableStatus": "CREATING", "GlobalTableName": "MusicCollection" } }

有关更多信息,请参阅亚马逊 DynamoDB 开发者指南中的 Dyn amoD B 全局表。

以下代码示例演示如何使用 create-table

AWS CLI

示例 1:创建带标签的表

以下 create-table 示例将使用指定的属性和键架构来创建名为 MusicCollection 的表。此表使用预配置的吞吐量,并使用默认 AWS 拥有CMK的吞吐量进行静态加密。该命令还将标签应用于该表,其键为 Owner,值为 blueTeam

aws dynamodb create-table \ --table-name MusicCollection \ --attribute-definitions AttributeName=Artist,AttributeType=S AttributeName=SongTitle,AttributeType=S \ --key-schema AttributeName=Artist,KeyType=HASH AttributeName=SongTitle,KeyType=RANGE \ --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 \ --tags Key=Owner,Value=blueTeam

输出:

{ "TableDescription": { "AttributeDefinitions": [ { "AttributeName": "Artist", "AttributeType": "S" }, { "AttributeName": "SongTitle", "AttributeType": "S" } ], "ProvisionedThroughput": { "NumberOfDecreasesToday": 0, "WriteCapacityUnits": 5, "ReadCapacityUnits": 5 }, "TableSizeBytes": 0, "TableName": "MusicCollection", "TableStatus": "CREATING", "KeySchema": [ { "KeyType": "HASH", "AttributeName": "Artist" }, { "KeyType": "RANGE", "AttributeName": "SongTitle" } ], "ItemCount": 0, "CreationDateTime": "2020-05-26T16:04:41.627000-07:00", "TableArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection", "TableId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111" } }

有关更多信息,请参阅《Amazon DynamoDB 开发人员指南》中的表的基本操作

示例 2:在按需模式下创建表

以下示例将使用按需模式(而不是预调配吞吐量模式)创建名为 MusicCollection 的表。这对于工作负载不可预测的表很有用。

aws dynamodb create-table \ --table-name MusicCollection \ --attribute-definitions AttributeName=Artist,AttributeType=S AttributeName=SongTitle,AttributeType=S \ --key-schema AttributeName=Artist,KeyType=HASH AttributeName=SongTitle,KeyType=RANGE \ --billing-mode PAY_PER_REQUEST

输出:

{ "TableDescription": { "AttributeDefinitions": [ { "AttributeName": "Artist", "AttributeType": "S" }, { "AttributeName": "SongTitle", "AttributeType": "S" } ], "TableName": "MusicCollection", "KeySchema": [ { "AttributeName": "Artist", "KeyType": "HASH" }, { "AttributeName": "SongTitle", "KeyType": "RANGE" } ], "TableStatus": "CREATING", "CreationDateTime": "2020-05-27T11:44:10.807000-07:00", "ProvisionedThroughput": { "NumberOfDecreasesToday": 0, "ReadCapacityUnits": 0, "WriteCapacityUnits": 0 }, "TableSizeBytes": 0, "ItemCount": 0, "TableArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection", "TableId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111", "BillingModeSummary": { "BillingMode": "PAY_PER_REQUEST" } } }

有关更多信息,请参阅《Amazon DynamoDB 开发人员指南》中的表的基本操作

示例 3:创建表并使用客户管理的表进行加密 CMK

以下示例创建了一个名为的表,MusicCollection并使用客户管理CMK的表对其进行加密。

aws dynamodb create-table \ --table-name MusicCollection \ --attribute-definitions AttributeName=Artist,AttributeType=S AttributeName=SongTitle,AttributeType=S \ --key-schema AttributeName=Artist,KeyType=HASH AttributeName=SongTitle,KeyType=RANGE \ --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 \ --sse-specification Enabled=true,SSEType=KMS,KMSMasterKeyId=abcd1234-abcd-1234-a123-ab1234a1b234

输出:

{ "TableDescription": { "AttributeDefinitions": [ { "AttributeName": "Artist", "AttributeType": "S" }, { "AttributeName": "SongTitle", "AttributeType": "S" } ], "TableName": "MusicCollection", "KeySchema": [ { "AttributeName": "Artist", "KeyType": "HASH" }, { "AttributeName": "SongTitle", "KeyType": "RANGE" } ], "TableStatus": "CREATING", "CreationDateTime": "2020-05-27T11:12:16.431000-07:00", "ProvisionedThroughput": { "NumberOfDecreasesToday": 0, "ReadCapacityUnits": 5, "WriteCapacityUnits": 5 }, "TableSizeBytes": 0, "ItemCount": 0, "TableArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection", "TableId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111", "SSEDescription": { "Status": "ENABLED", "SSEType": "KMS", "KMSMasterKeyArn": "arn:aws:kms:us-west-2:123456789012:key/abcd1234-abcd-1234-a123-ab1234a1b234" } } }

有关更多信息,请参阅《Amazon DynamoDB 开发人员指南》中的表的基本操作

示例 4:创建具有本地二级索引的表

以下示例将使用指定的属性和键架构来创建名为 MusicCollection 且其本地二级索引名为 AlbumTitleIndex 的表。

aws dynamodb create-table \ --table-name MusicCollection \ --attribute-definitions AttributeName=Artist,AttributeType=S AttributeName=SongTitle,AttributeType=S AttributeName=AlbumTitle,AttributeType=S \ --key-schema AttributeName=Artist,KeyType=HASH AttributeName=SongTitle,KeyType=RANGE \ --provisioned-throughput ReadCapacityUnits=10,WriteCapacityUnits=5 \ --local-secondary-indexes \ "[ { \"IndexName\": \"AlbumTitleIndex\", \"KeySchema\": [ {\"AttributeName\": \"Artist\",\"KeyType\":\"HASH\"}, {\"AttributeName\": \"AlbumTitle\",\"KeyType\":\"RANGE\"} ], \"Projection\": { \"ProjectionType\": \"INCLUDE\", \"NonKeyAttributes\": [\"Genre\", \"Year\"] } } ]"

输出:

{ "TableDescription": { "AttributeDefinitions": [ { "AttributeName": "AlbumTitle", "AttributeType": "S" }, { "AttributeName": "Artist", "AttributeType": "S" }, { "AttributeName": "SongTitle", "AttributeType": "S" } ], "TableName": "MusicCollection", "KeySchema": [ { "AttributeName": "Artist", "KeyType": "HASH" }, { "AttributeName": "SongTitle", "KeyType": "RANGE" } ], "TableStatus": "CREATING", "CreationDateTime": "2020-05-26T15:59:49.473000-07:00", "ProvisionedThroughput": { "NumberOfDecreasesToday": 0, "ReadCapacityUnits": 10, "WriteCapacityUnits": 5 }, "TableSizeBytes": 0, "ItemCount": 0, "TableArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection", "TableId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111", "LocalSecondaryIndexes": [ { "IndexName": "AlbumTitleIndex", "KeySchema": [ { "AttributeName": "Artist", "KeyType": "HASH" }, { "AttributeName": "AlbumTitle", "KeyType": "RANGE" } ], "Projection": { "ProjectionType": "INCLUDE", "NonKeyAttributes": [ "Genre", "Year" ] }, "IndexSizeBytes": 0, "ItemCount": 0, "IndexArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection/index/AlbumTitleIndex" } ] } }

有关更多信息,请参阅《Amazon DynamoDB 开发人员指南》中的表的基本操作

示例 5:创建具有全局二级索引的表

以下示例将创建一个名为 GameScores 且其全局二级索引名为 GameTitleIndex 的表。基表的UserId分区键为,排序键为GameTitle,允许你高效地找到单个用户在特定游戏中的最佳分数,而分区键为,排序键为TopScore,这样你就可以快速找到特定游戏的总体最高分数。GSI GameTitle

aws dynamodb create-table \ --table-name GameScores \ --attribute-definitions AttributeName=UserId,AttributeType=S AttributeName=GameTitle,AttributeType=S AttributeName=TopScore,AttributeType=N \ --key-schema AttributeName=UserId,KeyType=HASH \ AttributeName=GameTitle,KeyType=RANGE \ --provisioned-throughput ReadCapacityUnits=10,WriteCapacityUnits=5 \ --global-secondary-indexes \ "[ { \"IndexName\": \"GameTitleIndex\", \"KeySchema\": [ {\"AttributeName\":\"GameTitle\",\"KeyType\":\"HASH\"}, {\"AttributeName\":\"TopScore\",\"KeyType\":\"RANGE\"} ], \"Projection\": { \"ProjectionType\":\"INCLUDE\", \"NonKeyAttributes\":[\"UserId\"] }, \"ProvisionedThroughput\": { \"ReadCapacityUnits\": 10, \"WriteCapacityUnits\": 5 } } ]"

输出:

{ "TableDescription": { "AttributeDefinitions": [ { "AttributeName": "GameTitle", "AttributeType": "S" }, { "AttributeName": "TopScore", "AttributeType": "N" }, { "AttributeName": "UserId", "AttributeType": "S" } ], "TableName": "GameScores", "KeySchema": [ { "AttributeName": "UserId", "KeyType": "HASH" }, { "AttributeName": "GameTitle", "KeyType": "RANGE" } ], "TableStatus": "CREATING", "CreationDateTime": "2020-05-26T17:28:15.602000-07:00", "ProvisionedThroughput": { "NumberOfDecreasesToday": 0, "ReadCapacityUnits": 10, "WriteCapacityUnits": 5 }, "TableSizeBytes": 0, "ItemCount": 0, "TableArn": "arn:aws:dynamodb:us-west-2:123456789012:table/GameScores", "TableId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111", "GlobalSecondaryIndexes": [ { "IndexName": "GameTitleIndex", "KeySchema": [ { "AttributeName": "GameTitle", "KeyType": "HASH" }, { "AttributeName": "TopScore", "KeyType": "RANGE" } ], "Projection": { "ProjectionType": "INCLUDE", "NonKeyAttributes": [ "UserId" ] }, "IndexStatus": "CREATING", "ProvisionedThroughput": { "NumberOfDecreasesToday": 0, "ReadCapacityUnits": 10, "WriteCapacityUnits": 5 }, "IndexSizeBytes": 0, "ItemCount": 0, "IndexArn": "arn:aws:dynamodb:us-west-2:123456789012:table/GameScores/index/GameTitleIndex" } ] } }

有关更多信息,请参阅《Amazon DynamoDB 开发人员指南》中的表的基本操作

示例 6:一次创建一个具有多个全局二级索引的表

以下示例将创建一个名为 GameScores 且具有两个全局二级索引的表。GSI架构是通过文件传递的,而不是通过命令行传递的。

aws dynamodb create-table \ --table-name GameScores \ --attribute-definitions AttributeName=UserId,AttributeType=S AttributeName=GameTitle,AttributeType=S AttributeName=TopScore,AttributeType=N AttributeName=Date,AttributeType=S \ --key-schema AttributeName=UserId,KeyType=HASH AttributeName=GameTitle,KeyType=RANGE \ --provisioned-throughput ReadCapacityUnits=10,WriteCapacityUnits=5 \ --global-secondary-indexes file://gsi.json

gsi.json 的内容:

[ { "IndexName": "GameTitleIndex", "KeySchema": [ { "AttributeName": "GameTitle", "KeyType": "HASH" }, { "AttributeName": "TopScore", "KeyType": "RANGE" } ], "Projection": { "ProjectionType": "ALL" }, "ProvisionedThroughput": { "ReadCapacityUnits": 10, "WriteCapacityUnits": 5 } }, { "IndexName": "GameDateIndex", "KeySchema": [ { "AttributeName": "GameTitle", "KeyType": "HASH" }, { "AttributeName": "Date", "KeyType": "RANGE" } ], "Projection": { "ProjectionType": "ALL" }, "ProvisionedThroughput": { "ReadCapacityUnits": 5, "WriteCapacityUnits": 5 } } ]

输出:

{ "TableDescription": { "AttributeDefinitions": [ { "AttributeName": "Date", "AttributeType": "S" }, { "AttributeName": "GameTitle", "AttributeType": "S" }, { "AttributeName": "TopScore", "AttributeType": "N" }, { "AttributeName": "UserId", "AttributeType": "S" } ], "TableName": "GameScores", "KeySchema": [ { "AttributeName": "UserId", "KeyType": "HASH" }, { "AttributeName": "GameTitle", "KeyType": "RANGE" } ], "TableStatus": "CREATING", "CreationDateTime": "2020-08-04T16:40:55.524000-07:00", "ProvisionedThroughput": { "NumberOfDecreasesToday": 0, "ReadCapacityUnits": 10, "WriteCapacityUnits": 5 }, "TableSizeBytes": 0, "ItemCount": 0, "TableArn": "arn:aws:dynamodb:us-west-2:123456789012:table/GameScores", "TableId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111", "GlobalSecondaryIndexes": [ { "IndexName": "GameTitleIndex", "KeySchema": [ { "AttributeName": "GameTitle", "KeyType": "HASH" }, { "AttributeName": "TopScore", "KeyType": "RANGE" } ], "Projection": { "ProjectionType": "ALL" }, "IndexStatus": "CREATING", "ProvisionedThroughput": { "NumberOfDecreasesToday": 0, "ReadCapacityUnits": 10, "WriteCapacityUnits": 5 }, "IndexSizeBytes": 0, "ItemCount": 0, "IndexArn": "arn:aws:dynamodb:us-west-2:123456789012:table/GameScores/index/GameTitleIndex" }, { "IndexName": "GameDateIndex", "KeySchema": [ { "AttributeName": "GameTitle", "KeyType": "HASH" }, { "AttributeName": "Date", "KeyType": "RANGE" } ], "Projection": { "ProjectionType": "ALL" }, "IndexStatus": "CREATING", "ProvisionedThroughput": { "NumberOfDecreasesToday": 0, "ReadCapacityUnits": 5, "WriteCapacityUnits": 5 }, "IndexSizeBytes": 0, "ItemCount": 0, "IndexArn": "arn:aws:dynamodb:us-west-2:123456789012:table/GameScores/index/GameDateIndex" } ] } }

有关更多信息,请参阅《Amazon DynamoDB 开发人员指南》中的表的基本操作

示例 7:创建启用了 Streams 的表

以下示例将创建一个名为 GameScores 且启用了 DynamoDB Streams 的表。每个项的新旧映像都将写入流中。

aws dynamodb create-table \ --table-name GameScores \ --attribute-definitions AttributeName=UserId,AttributeType=S AttributeName=GameTitle,AttributeType=S \ --key-schema AttributeName=UserId,KeyType=HASH AttributeName=GameTitle,KeyType=RANGE \ --provisioned-throughput ReadCapacityUnits=10,WriteCapacityUnits=5 \ --stream-specification StreamEnabled=TRUE,StreamViewType=NEW_AND_OLD_IMAGES

输出:

{ "TableDescription": { "AttributeDefinitions": [ { "AttributeName": "GameTitle", "AttributeType": "S" }, { "AttributeName": "UserId", "AttributeType": "S" } ], "TableName": "GameScores", "KeySchema": [ { "AttributeName": "UserId", "KeyType": "HASH" }, { "AttributeName": "GameTitle", "KeyType": "RANGE" } ], "TableStatus": "CREATING", "CreationDateTime": "2020-05-27T10:49:34.056000-07:00", "ProvisionedThroughput": { "NumberOfDecreasesToday": 0, "ReadCapacityUnits": 10, "WriteCapacityUnits": 5 }, "TableSizeBytes": 0, "ItemCount": 0, "TableArn": "arn:aws:dynamodb:us-west-2:123456789012:table/GameScores", "TableId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111", "StreamSpecification": { "StreamEnabled": true, "StreamViewType": "NEW_AND_OLD_IMAGES" }, "LatestStreamLabel": "2020-05-27T17:49:34.056", "LatestStreamArn": "arn:aws:dynamodb:us-west-2:123456789012:table/GameScores/stream/2020-05-27T17:49:34.056" } }

有关更多信息,请参阅《Amazon DynamoDB 开发人员指南》中的表的基本操作

示例 8:创建启用了 Keys-Only Stream 的表

以下示例将创建一个名为 GameScores 且启用了 DynamoDB Streams 的表。仅将所修改项的键属性写入流中。

aws dynamodb create-table \ --table-name GameScores \ --attribute-definitions AttributeName=UserId,AttributeType=S AttributeName=GameTitle,AttributeType=S \ --key-schema AttributeName=UserId,KeyType=HASH AttributeName=GameTitle,KeyType=RANGE \ --provisioned-throughput ReadCapacityUnits=10,WriteCapacityUnits=5 \ --stream-specification StreamEnabled=TRUE,StreamViewType=KEYS_ONLY

输出:

{ "TableDescription": { "AttributeDefinitions": [ { "AttributeName": "GameTitle", "AttributeType": "S" }, { "AttributeName": "UserId", "AttributeType": "S" } ], "TableName": "GameScores", "KeySchema": [ { "AttributeName": "UserId", "KeyType": "HASH" }, { "AttributeName": "GameTitle", "KeyType": "RANGE" } ], "TableStatus": "CREATING", "CreationDateTime": "2023-05-25T18:45:34.140000+00:00", "ProvisionedThroughput": { "NumberOfDecreasesToday": 0, "ReadCapacityUnits": 10, "WriteCapacityUnits": 5 }, "TableSizeBytes": 0, "ItemCount": 0, "TableArn": "arn:aws:dynamodb:us-west-2:123456789012:table/GameScores", "TableId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111", "StreamSpecification": { "StreamEnabled": true, "StreamViewType": "KEYS_ONLY" }, "LatestStreamLabel": "2023-05-25T18:45:34.140", "LatestStreamArn": "arn:aws:dynamodb:us-west-2:123456789012:table/GameScores/stream/2023-05-25T18:45:34.140", "DeletionProtectionEnabled": false } }

有关更多信息,请参阅《Amazon DynamoDB 开发人员指南》中的更改 DynamoDB Streams 的数据捕获

示例 9:使用 Standard Infrequent Access 类创建表

以下示例将创建名为 GameScores 的表并分配 Standard-Infrequent Access(DynamoDB 标准-IA)表类。此表类针对主要的存储成本进行了优化。

aws dynamodb create-table \ --table-name GameScores \ --attribute-definitions AttributeName=UserId,AttributeType=S AttributeName=GameTitle,AttributeType=S \ --key-schema AttributeName=UserId,KeyType=HASH AttributeName=GameTitle,KeyType=RANGE \ --provisioned-throughput ReadCapacityUnits=10,WriteCapacityUnits=5 \ --table-class STANDARD_INFREQUENT_ACCESS

输出:

{ "TableDescription": { "AttributeDefinitions": [ { "AttributeName": "GameTitle", "AttributeType": "S" }, { "AttributeName": "UserId", "AttributeType": "S" } ], "TableName": "GameScores", "KeySchema": [ { "AttributeName": "UserId", "KeyType": "HASH" }, { "AttributeName": "GameTitle", "KeyType": "RANGE" } ], "TableStatus": "CREATING", "CreationDateTime": "2023-05-25T18:33:07.581000+00:00", "ProvisionedThroughput": { "NumberOfDecreasesToday": 0, "ReadCapacityUnits": 10, "WriteCapacityUnits": 5 }, "TableSizeBytes": 0, "ItemCount": 0, "TableArn": "arn:aws:dynamodb:us-west-2:123456789012:table/GameScores", "TableId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111", "TableClassSummary": { "TableClass": "STANDARD_INFREQUENT_ACCESS" }, "DeletionProtectionEnabled": false } }

有关更多信息,请参阅《Amazon DynamoDB 开发人员指南》中的表类

示例 10:创建启用了删除保护功能的表

以下示例将创建一个名为 GameScores 的表并启用删除保护。

aws dynamodb create-table \ --table-name GameScores \ --attribute-definitions AttributeName=UserId,AttributeType=S AttributeName=GameTitle,AttributeType=S \ --key-schema AttributeName=UserId,KeyType=HASH AttributeName=GameTitle,KeyType=RANGE \ --provisioned-throughput ReadCapacityUnits=10,WriteCapacityUnits=5 \ --deletion-protection-enabled

输出:

{ "TableDescription": { "AttributeDefinitions": [ { "AttributeName": "GameTitle", "AttributeType": "S" }, { "AttributeName": "UserId", "AttributeType": "S" } ], "TableName": "GameScores", "KeySchema": [ { "AttributeName": "UserId", "KeyType": "HASH" }, { "AttributeName": "GameTitle", "KeyType": "RANGE" } ], "TableStatus": "CREATING", "CreationDateTime": "2023-05-25T23:02:17.093000+00:00", "ProvisionedThroughput": { "NumberOfDecreasesToday": 0, "ReadCapacityUnits": 10, "WriteCapacityUnits": 5 }, "TableSizeBytes": 0, "ItemCount": 0, "TableArn": "arn:aws:dynamodb:us-west-2:123456789012:table/GameScores", "TableId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111", "DeletionProtectionEnabled": true } }

有关更多信息,请参阅《Amazon DynamoDB 开发人员指南》中的使用删除保护

以下代码示例演示如何使用 delete-backup

AWS CLI

删除现有 DynamoDB 备份

以下delete-backup示例删除了指定的现有备份。

aws dynamodb delete-backup \ --backup-arn arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection/backup/01576616366715-b4e58d3a

输出:

{ "BackupDescription": { "BackupDetails": { "BackupArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection/backup/01576616366715-b4e58d3a", "BackupName": "MusicCollectionBackup", "BackupSizeBytes": 0, "BackupStatus": "DELETED", "BackupType": "USER", "BackupCreationDateTime": 1576616366.715 }, "SourceTableDetails": { "TableName": "MusicCollection", "TableId": "b0c04bcc-309b-4352-b2ae-9088af169fe2", "TableArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection", "TableSizeBytes": 0, "KeySchema": [ { "AttributeName": "Artist", "KeyType": "HASH" }, { "AttributeName": "SongTitle", "KeyType": "RANGE" } ], "TableCreationDateTime": 1576615228.571, "ProvisionedThroughput": { "ReadCapacityUnits": 5, "WriteCapacityUnits": 5 }, "ItemCount": 0, "BillingMode": "PROVISIONED" }, "SourceTableFeatureDetails": {} } }

有关更多信息,请参阅《亚马逊 DynamoDB 开发者指南》中的 DynamoDB 按需备份和恢复

以下代码示例演示如何使用 delete-item

AWS CLI

示例 1:删除项

以下 delete-item 示例将从 MusicCollection 表中删除项,并请求有关已删除的项以及请求使用的容量的详细信息。

aws dynamodb delete-item \ --table-name MusicCollection \ --key file://key.json \ --return-values ALL_OLD \ --return-consumed-capacity TOTAL \ --return-item-collection-metrics SIZE

key.json 的内容:

{ "Artist": {"S": "No One You Know"}, "SongTitle": {"S": "Scared of My Shadow"} }

输出:

{ "Attributes": { "AlbumTitle": { "S": "Blue Sky Blues" }, "Artist": { "S": "No One You Know" }, "SongTitle": { "S": "Scared of My Shadow" } }, "ConsumedCapacity": { "TableName": "MusicCollection", "CapacityUnits": 2.0 }, "ItemCollectionMetrics": { "ItemCollectionKey": { "Artist": { "S": "No One You Know" } }, "SizeEstimateRangeGB": [ 0.0, 1.0 ] } }

有关更多信息,请参阅《Amazon DynamoDB 开发人员指南》中的写入项

示例 2:有条件地删除项

以下示例仅在某项的 ProductCategorySporting GoodsGardening Supplies 且其价格介于 500 和 600 之间时,才会将其从 ProductCatalog 表中删除。它会返回有关已删除项的详细信息。

aws dynamodb delete-item \ --table-name ProductCatalog \ --key '{"Id":{"N":"456"}}' \ --condition-expression "(ProductCategory IN (:cat1, :cat2)) and (#P between :lo and :hi)" \ --expression-attribute-names file://names.json \ --expression-attribute-values file://values.json \ --return-values ALL_OLD

names.json 的内容:

{ "#P": "Price" }

values.json 的内容:

{ ":cat1": {"S": "Sporting Goods"}, ":cat2": {"S": "Gardening Supplies"}, ":lo": {"N": "500"}, ":hi": {"N": "600"} }

输出:

{ "Attributes": { "Id": { "N": "456" }, "Price": { "N": "550" }, "ProductCategory": { "S": "Sporting Goods" } } }

有关更多信息,请参阅《Amazon DynamoDB 开发人员指南》中的写入项

以下代码示例演示如何使用 delete-table

AWS CLI

删除表

以下 delete-table 示例将删除 MusicCollection 表。

aws dynamodb delete-table \ --table-name MusicCollection

输出:

{ "TableDescription": { "TableStatus": "DELETING", "TableSizeBytes": 0, "ItemCount": 0, "TableName": "MusicCollection", "ProvisionedThroughput": { "NumberOfDecreasesToday": 0, "WriteCapacityUnits": 5, "ReadCapacityUnits": 5 } } }

有关更多信息,请参阅《Amazon DynamoDB 开发人员指南》中的删除表

以下代码示例演示如何使用 describe-backup

AWS CLI

获取有关表现有备份的信息

以下describe-backup示例显示有关指定现有备份的信息。

aws dynamodb describe-backup \ --backup-arn arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection/backup/01576616366715-b4e58d3a

输出:

{ "BackupDescription": { "BackupDetails": { "BackupArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection/backup/01576616366715-b4e58d3a", "BackupName": "MusicCollectionBackup", "BackupSizeBytes": 0, "BackupStatus": "AVAILABLE", "BackupType": "USER", "BackupCreationDateTime": 1576616366.715 }, "SourceTableDetails": { "TableName": "MusicCollection", "TableId": "b0c04bcc-309b-4352-b2ae-9088af169fe2", "TableArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection", "TableSizeBytes": 0, "KeySchema": [ { "AttributeName": "Artist", "KeyType": "HASH" }, { "AttributeName": "SongTitle", "KeyType": "RANGE" } ], "TableCreationDateTime": 1576615228.571, "ProvisionedThroughput": { "ReadCapacityUnits": 5, "WriteCapacityUnits": 5 }, "ItemCount": 0, "BillingMode": "PROVISIONED" }, "SourceTableFeatureDetails": {} } }

有关更多信息,请参阅《亚马逊 DynamoDB 开发者指南》中的 DynamoDB 按需备份和恢复

以下代码示例演示如何使用 describe-continuous-backups

AWS CLI

获取有关 DynamoDB 表连续备份的信息

以下describe-continuous-backups示例显示了有关MusicCollection表连续备份设置的详细信息。

aws dynamodb describe-continuous-backups \ --table-name MusicCollection

输出:

{ "ContinuousBackupsDescription": { "ContinuousBackupsStatus": "ENABLED", "PointInTimeRecoveryDescription": { "PointInTimeRecoveryStatus": "DISABLED" } } }

有关更多信息,请参阅《亚马逊 DynamoDB 开发者指南》中的 Dyn amoD B Point-in-Time 恢复

以下代码示例演示如何使用 describe-contributor-insights

AWS CLI

查看 DynamoDB 表的 “贡献者见解” 设置

以下describe-contributor-insights示例显示了MusicCollection表和AlbumTitle-index全局二级索引的 “贡献者见解” 设置。

aws dynamodb describe-contributor-insights \ --table-name MusicCollection \ --index-name AlbumTitle-index

输出:

{ "TableName": "MusicCollection", "IndexName": "AlbumTitle-index", "ContributorInsightsRuleList": [ "DynamoDBContributorInsights-PKC-MusicCollection-1576629651520", "DynamoDBContributorInsights-SKC-MusicCollection-1576629651520", "DynamoDBContributorInsights-PKT-MusicCollection-1576629651520", "DynamoDBContributorInsights-SKT-MusicCollection-1576629651520" ], "ContributorInsightsStatus": "ENABLED", "LastUpdateDateTime": 1576629654.78 }

有关更多信息,请参阅《亚马逊 DynamoDB 开发者指南》中的 “使用 CloudWatch 贡献者洞察分析 DynamoDB 的数据访问权限”。

以下代码示例演示如何使用 describe-endpoints

AWS CLI

查看区域终端节点信息

以下describe-endpoints示例显示有关当前 AWS 区域终端节点的详细信息。

aws dynamodb describe-endpoints

输出:

{ "Endpoints": [ { "Address": "dynamodb.us-west-2.amazonaws.com", "CachePeriodInMinutes": 1440 } ] }

有关更多信息,请参阅一般参考中的亚马逊 DynamoDB 终端节点和配额。AWS

以下代码示例演示如何使用 describe-global-table-settings

AWS CLI

获取有关 DynamoDB 全局表设置的信息

以下describe-global-table-settings示例显示了MusicCollection全局表的设置。

aws dynamodb describe-global-table-settings \ --global-table-name MusicCollection

输出:

{ "GlobalTableName": "MusicCollection", "ReplicaSettings": [ { "RegionName": "us-east-1", "ReplicaStatus": "ACTIVE", "ReplicaProvisionedReadCapacityUnits": 10, "ReplicaProvisionedReadCapacityAutoScalingSettings": { "AutoScalingDisabled": true }, "ReplicaProvisionedWriteCapacityUnits": 5, "ReplicaProvisionedWriteCapacityAutoScalingSettings": { "AutoScalingDisabled": true } }, { "RegionName": "us-east-2", "ReplicaStatus": "ACTIVE", "ReplicaProvisionedReadCapacityUnits": 10, "ReplicaProvisionedReadCapacityAutoScalingSettings": { "AutoScalingDisabled": true }, "ReplicaProvisionedWriteCapacityUnits": 5, "ReplicaProvisionedWriteCapacityAutoScalingSettings": { "AutoScalingDisabled": true } } ] }

有关更多信息,请参阅亚马逊 DynamoDB 开发者指南中的 Dyn amoD B 全局表。

以下代码示例演示如何使用 describe-global-table

AWS CLI

显示有关 DynamoDB 全局表的信息

以下describe-global-table示例显示了有关MusicCollection全局表的详细信息。

aws dynamodb describe-global-table \ --global-table-name MusicCollection

输出:

{ "GlobalTableDescription": { "ReplicationGroup": [ { "RegionName": "us-east-2" }, { "RegionName": "us-east-1" } ], "GlobalTableArn": "arn:aws:dynamodb::123456789012:global-table/MusicCollection", "CreationDateTime": 1576625818.532, "GlobalTableStatus": "ACTIVE", "GlobalTableName": "MusicCollection" } }

有关更多信息,请参阅亚马逊 DynamoDB 开发者指南中的 Dyn amoD B 全局表。

以下代码示例演示如何使用 describe-limits

AWS CLI

查看预配置容量限制

以下describe-limits示例显示了您账户在当前 AWS 区域的预配置容量限制。

aws dynamodb describe-limits

输出:

{ "AccountMaxReadCapacityUnits": 80000, "AccountMaxWriteCapacityUnits": 80000, "TableMaxReadCapacityUnits": 40000, "TableMaxWriteCapacityUnits": 40000 }

有关更多信息,请参阅《亚马逊 DynamoDB 开发者指南》中的 Dyn amoD B 限制

以下代码示例演示如何使用 describe-table-replica-auto-scaling

AWS CLI

查看跨全局表副本的 auto 缩放设置

以下describe-table-replica-auto-scaling示例显示了全MusicCollection局表副本之间的 auto Scaling 设置。

aws dynamodb describe-table-replica-auto-scaling \ --table-name MusicCollection

输出:

{ "TableAutoScalingDescription": { "TableName": "MusicCollection", "TableStatus": "ACTIVE", "Replicas": [ { "RegionName": "us-east-1", "GlobalSecondaryIndexes": [], "ReplicaProvisionedReadCapacityAutoScalingSettings": { "MinimumUnits": 5, "MaximumUnits": 40000, "AutoScalingRoleArn": "arn:aws:iam::123456789012:role/aws-service-role/dynamodb.application-autoscaling.amazonaws.com/AWSServiceRoleForApplicationAutoScaling_DynamoDBTable", "ScalingPolicies": [ { "PolicyName": "DynamoDBReadCapacityUtilization:table/MusicCollection", "TargetTrackingScalingPolicyConfiguration": { "TargetValue": 70.0 } } ] }, "ReplicaProvisionedWriteCapacityAutoScalingSettings": { "MinimumUnits": 5, "MaximumUnits": 40000, "AutoScalingRoleArn": "arn:aws:iam::123456789012:role/aws-service-role/dynamodb.application-autoscaling.amazonaws.com/AWSServiceRoleForApplicationAutoScaling_DynamoDBTable", "ScalingPolicies": [ { "PolicyName": "DynamoDBWriteCapacityUtilization:table/MusicCollection", "TargetTrackingScalingPolicyConfiguration": { "TargetValue": 70.0 } } ] }, "ReplicaStatus": "ACTIVE" }, { "RegionName": "us-east-2", "GlobalSecondaryIndexes": [], "ReplicaProvisionedReadCapacityAutoScalingSettings": { "MinimumUnits": 5, "MaximumUnits": 40000, "AutoScalingRoleArn": "arn:aws:iam::123456789012:role/aws-service-role/dynamodb.application-autoscaling.amazonaws.com/AWSServiceRoleForApplicationAutoScaling_DynamoDBTable", "ScalingPolicies": [ { "PolicyName": "DynamoDBReadCapacityUtilization:table/MusicCollection", "TargetTrackingScalingPolicyConfiguration": { "TargetValue": 70.0 } } ] }, "ReplicaProvisionedWriteCapacityAutoScalingSettings": { "MinimumUnits": 5, "MaximumUnits": 40000, "AutoScalingRoleArn": "arn:aws:iam::123456789012:role/aws-service-role/dynamodb.application-autoscaling.amazonaws.com/AWSServiceRoleForApplicationAutoScaling_DynamoDBTable", "ScalingPolicies": [ { "PolicyName": "DynamoDBWriteCapacityUtilization:table/MusicCollection", "TargetTrackingScalingPolicyConfiguration": { "TargetValue": 70.0 } } ] }, "ReplicaStatus": "ACTIVE" } ] } }

有关更多信息,请参阅亚马逊 DynamoDB 开发者指南中的 Dyn amoD B 全局表。

以下代码示例演示如何使用 describe-table

AWS CLI

描述表

以下 describe-table 示例将描述 MusicCollection 表。

aws dynamodb describe-table \ --table-name MusicCollection

输出:

{ "Table": { "AttributeDefinitions": [ { "AttributeName": "Artist", "AttributeType": "S" }, { "AttributeName": "SongTitle", "AttributeType": "S" } ], "ProvisionedThroughput": { "NumberOfDecreasesToday": 0, "WriteCapacityUnits": 5, "ReadCapacityUnits": 5 }, "TableSizeBytes": 0, "TableName": "MusicCollection", "TableStatus": "ACTIVE", "KeySchema": [ { "KeyType": "HASH", "AttributeName": "Artist" }, { "KeyType": "RANGE", "AttributeName": "SongTitle" } ], "ItemCount": 0, "CreationDateTime": 1421866952.062 } }

有关更多信息,请参阅《Amazon DynamoDB 开发人员指南》中的描述表

以下代码示例演示如何使用 describe-time-to-live

AWS CLI

查看表的生存时间设置

以下 describe-time-to-live 示例显示 MusicCollection 表的生存时间设置。

aws dynamodb describe-time-to-live \ --table-name MusicCollection

输出:

{ "TimeToLiveDescription": { "TimeToLiveStatus": "ENABLED", "AttributeName": "ttl" } }

有关更多信息,请参阅《Amazon DynamoDB 开发人员指南》中的生存时间

以下代码示例演示如何使用 get-item

AWS CLI

示例 1:读取表中的项

以下 get-item 示例将从 MusicCollection 表中检索项。该表具有 hash-and-range主键(ArtistSongTitle),因此必须同时指定这两个属性。该命令还请求有关操作所用的读取容量的信息。

aws dynamodb get-item \ --table-name MusicCollection \ --key file://key.json \ --return-consumed-capacity TOTAL

key.json 的内容:

{ "Artist": {"S": "Acme Band"}, "SongTitle": {"S": "Happy Day"} }

输出:

{ "Item": { "AlbumTitle": { "S": "Songs About Life" }, "SongTitle": { "S": "Happy Day" }, "Artist": { "S": "Acme Band" } }, "ConsumedCapacity": { "TableName": "MusicCollection", "CapacityUnits": 0.5 } }

有关更多信息,请参阅《Amazon DynamoDB 开发人员指南》中的读取项

示例 2:使用一致性读取来读取项

以下示例将使用强一致性读取从 MusicCollection 表中检索项。

aws dynamodb get-item \ --table-name MusicCollection \ --key file://key.json \ --consistent-read \ --return-consumed-capacity TOTAL

key.json 的内容:

{ "Artist": {"S": "Acme Band"}, "SongTitle": {"S": "Happy Day"} }

输出:

{ "Item": { "AlbumTitle": { "S": "Songs About Life" }, "SongTitle": { "S": "Happy Day" }, "Artist": { "S": "Acme Band" } }, "ConsumedCapacity": { "TableName": "MusicCollection", "CapacityUnits": 1.0 } }

有关更多信息,请参阅《Amazon DynamoDB 开发人员指南》中的读取项

示例 3:检索项的特定属性

以下示例将使用投影表达式仅检索所需项的三个属性。

aws dynamodb get-item \ --table-name ProductCatalog \ --key '{"Id": {"N": "102"}}' \ --projection-expression "#T, #C, #P" \ --expression-attribute-names file://names.json

names.json 的内容:

{ "#T": "Title", "#C": "ProductCategory", "#P": "Price" }

输出:

{ "Item": { "Price": { "N": "20" }, "Title": { "S": "Book 102 Title" }, "ProductCategory": { "S": "Book" } } }

有关更多信息,请参阅《Amazon DynamoDB 开发人员指南》中的读取项

以下代码示例演示如何使用 list-backups

AWS CLI

示例 1:列出所有现有 DynamoDB 备份

以下list-backups示例列出了您的所有现有备份。

aws dynamodb list-backups

输出:

{ "BackupSummaries": [ { "TableName": "MusicCollection", "TableId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111", "TableArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection", "BackupArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection/backup/01234567890123-a1bcd234", "BackupName": "MusicCollectionBackup1", "BackupCreationDateTime": "2020-02-12T14:41:51.617000-08:00", "BackupStatus": "AVAILABLE", "BackupType": "USER", "BackupSizeBytes": 170 }, { "TableName": "MusicCollection", "TableId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111", "TableArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection", "BackupArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection/backup/01234567890123-b2abc345", "BackupName": "MusicCollectionBackup2", "BackupCreationDateTime": "2020-06-26T11:08:35.431000-07:00", "BackupStatus": "AVAILABLE", "BackupType": "USER", "BackupSizeBytes": 400 } ] }

有关更多信息,请参阅《亚马逊 DynamoDB 开发者指南》中的 DynamoDB 按需备份和恢复

示例 2:列出特定时间范围内用户创建的备份

以下示例仅列出了用户创建的MusicCollection表的备份(不是由 DynamoDB 自动创建的备份),其创建日期介于 2020 年 1 月 1 日至 2020 年 3 月 1 日之间。

aws dynamodb list-backups \ --table-name MusicCollection \ --time-range-lower-bound 1577836800 \ --time-range-upper-bound 1583020800 \ --backup-type USER

输出:

{ "BackupSummaries": [ { "TableName": "MusicCollection", "TableId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111", "TableArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection", "BackupArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection/backup/01234567890123-a1bcd234", "BackupName": "MusicCollectionBackup1", "BackupCreationDateTime": "2020-02-12T14:41:51.617000-08:00", "BackupStatus": "AVAILABLE", "BackupType": "USER", "BackupSizeBytes": 170 } ] }

有关更多信息,请参阅《亚马逊 DynamoDB 开发者指南》中的 DynamoDB 按需备份和恢复

示例 3:限制页面大小

以下示例返回所有现有备份的列表,但在每次调用中仅检索一个项目,必要时执行多次调用以获取整个列表。在对大量资源运行列表命令时,限制页面大小非常有用,使用默认页面大小 1000 时,可能会导致“超时”错误。

aws dynamodb list-backups \ --page-size 1

输出:

{ "BackupSummaries": [ { "TableName": "MusicCollection", "TableId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111", "TableArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection", "BackupArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection/backup/01234567890123-a1bcd234", "BackupName": "MusicCollectionBackup1", "BackupCreationDateTime": "2020-02-12T14:41:51.617000-08:00", "BackupStatus": "AVAILABLE", "BackupType": "USER", "BackupSizeBytes": 170 }, { "TableName": "MusicCollection", "TableId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111", "TableArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection", "BackupArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection/backup/01234567890123-b2abc345", "BackupName": "MusicCollectionBackup2", "BackupCreationDateTime": "2020-06-26T11:08:35.431000-07:00", "BackupStatus": "AVAILABLE", "BackupType": "USER", "BackupSizeBytes": 400 } ] }

有关更多信息,请参阅《亚马逊 DynamoDB 开发者指南》中的 DynamoDB 按需备份和恢复

示例 4:限制退回的商品数量

以下示例将返回的商品数量限制为 1。响应包含用于检索下一页结果的 NextToken 值。

aws dynamodb list-backups \ --max-items 1

输出:

{ "BackupSummaries": [ { "TableName": "MusicCollection", "TableId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111", "TableArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection", "BackupArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection/backup/01234567890123-a1bcd234", "BackupName": "MusicCollectionBackup1", "BackupCreationDateTime": "2020-02-12T14:41:51.617000-08:00", "BackupStatus": "AVAILABLE", "BackupType": "USER", "BackupSizeBytes": 170 } ], "NextToken": "abCDeFGhiJKlmnOPqrSTuvwxYZ1aBCdEFghijK7LM51nOpqRSTuv3WxY3ZabC5dEFGhI2Jk3LmnoPQ6RST9" }

有关更多信息,请参阅《亚马逊 DynamoDB 开发者指南》中的 DynamoDB 按需备份和恢复

示例 5:检索下一页结果

以下命令将使用先前对 list-backups 命令的调用中的 NextToken 值来检索另一页结果。由于本例中的响应不包含 NextToken 值,因此,我们知道已经到达结果末尾。

aws dynamodb list-backups \ --starting-token abCDeFGhiJKlmnOPqrSTuvwxYZ1aBCdEFghijK7LM51nOpqRSTuv3WxY3ZabC5dEFGhI2Jk3LmnoPQ6RST9

输出

{ "BackupSummaries": [ { "TableName": "MusicCollection", "TableId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111", "TableArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection", "BackupArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection/backup/01234567890123-b2abc345", "BackupName": "MusicCollectionBackup2", "BackupCreationDateTime": "2020-06-26T11:08:35.431000-07:00", "BackupStatus": "AVAILABLE", "BackupType": "USER", "BackupSizeBytes": 400 } ] }

有关更多信息,请参阅《亚马逊 DynamoDB 开发者指南》中的 DynamoDB 按需备份和恢复

以下代码示例演示如何使用 list-contributor-insights

AWS CLI

示例 1:查看投稿人见解摘要列表

以下list-contributor-insights示例显示了 “贡献者见解” 摘要列表。

aws dynamodb list-contributor-insights

输出:

{ "ContributorInsightsSummaries": [ { "TableName": "MusicCollection", "IndexName": "AlbumTitle-index", "ContributorInsightsStatus": "ENABLED" }, { "TableName": "ProductCatalog", "ContributorInsightsStatus": "ENABLED" }, { "TableName": "Forum", "ContributorInsightsStatus": "ENABLED" }, { "TableName": "Reply", "ContributorInsightsStatus": "ENABLED" }, { "TableName": "Thread", "ContributorInsightsStatus": "ENABLED" } ] }

有关更多信息,请参阅《亚马逊 DynamoDB 开发者指南》中的 “使用 CloudWatch 贡献者洞察分析 DynamoDB 的数据访问权限”。

示例 2:限制退回的商品数量

以下示例将返回的商品数量限制为 4。响应包含用于检索下一页结果的 NextToken 值。

aws dynamodb list-contributor-insights \ --max-results 4

输出:

{ "ContributorInsightsSummaries": [ { "TableName": "MusicCollection", "IndexName": "AlbumTitle-index", "ContributorInsightsStatus": "ENABLED" }, { "TableName": "ProductCatalog", "ContributorInsightsStatus": "ENABLED" }, { "TableName": "Forum", "ContributorInsightsStatus": "ENABLED" } ], "NextToken": "abCDeFGhiJKlmnOPqrSTuvwxYZ1aBCdEFghijK7LM51nOpqRSTuv3WxY3ZabC5dEFGhI2Jk3LmnoPQ6RST9" }

有关更多信息,请参阅《亚马逊 DynamoDB 开发者指南》中的 “使用 CloudWatch 贡献者洞察分析 DynamoDB 的数据访问权限”。

示例 3:检索下一页结果

以下命令将使用先前对 list-contributor-insights 命令的调用中的 NextToken 值来检索另一页结果。由于本例中的响应不包含 NextToken 值,因此,我们知道已经到达结果末尾。

aws dynamodb list-contributor-insights \ --max-results 4 \ --next-token abCDeFGhiJKlmnOPqrSTuvwxYZ1aBCdEFghijK7LM51nOpqRSTuv3WxY3ZabC5dEFGhI2Jk3LmnoPQ6RST9

输出:

{ "ContributorInsightsSummaries": [ { "TableName": "Reply", "ContributorInsightsStatus": "ENABLED" }, { "TableName": "Thread", "ContributorInsightsStatus": "ENABLED" } ] }

有关更多信息,请参阅《亚马逊 DynamoDB 开发者指南》中的 “使用 CloudWatch 贡献者洞察分析 DynamoDB 的数据访问权限”。

以下代码示例演示如何使用 list-global-tables

AWS CLI

列出现有 DynamoDB 全局表

以下list-global-tables示例列出了所有现有的全局表。

aws dynamodb list-global-tables

输出:

{ "GlobalTables": [ { "GlobalTableName": "MusicCollection", "ReplicationGroup": [ { "RegionName": "us-east-2" }, { "RegionName": "us-east-1" } ] } ] }

有关更多信息,请参阅亚马逊 DynamoDB 开发者指南中的 Dyn amoD B 全局表。

以下代码示例演示如何使用 list-tables

AWS CLI

示例 1:列出表

以下list-tables示例列出了与当前 AWS 账户和区域关联的所有表。

aws dynamodb list-tables

输出:

{ "TableNames": [ "Forum", "ProductCatalog", "Reply", "Thread" ] }

有关更多信息,请参阅《Amazon DynamoDB 开发人员指南》中的列出表名称

示例 2:限制页面大小

以下示例将返回所有现有表的列表,但在每次调用中仅检索一个项,必要时执行多次调用以获取整个列表。在对大量资源运行列表命令时,限制页面大小非常有用,使用默认页面大小 1000 时,可能会导致“超时”错误。

aws dynamodb list-tables \ --page-size 1

输出:

{ "TableNames": [ "Forum", "ProductCatalog", "Reply", "Thread" ] }

有关更多信息,请参阅《Amazon DynamoDB 开发人员指南》中的列出表名称

示例 3:限制返回项的数量

以下示例将返回项的数量限制为 2。响应包含用于检索下一页结果的 NextToken 值。

aws dynamodb list-tables \ --max-items 2

输出:

{ "TableNames": [ "Forum", "ProductCatalog" ], "NextToken": "abCDeFGhiJKlmnOPqrSTuvwxYZ1aBCdEFghijK7LM51nOpqRSTuv3WxY3ZabC5dEFGhI2Jk3LmnoPQ6RST9" }

有关更多信息,请参阅《Amazon DynamoDB 开发人员指南》中的列出表名称

示例 4:检索下一页结果

以下命令将使用先前对 list-tables 命令的调用中的 NextToken 值来检索另一页结果。由于本例中的响应不包含 NextToken 值,因此,我们知道已经到达结果末尾。

aws dynamodb list-tables \ --starting-token abCDeFGhiJKlmnOPqrSTuvwxYZ1aBCdEFghijK7LM51nOpqRSTuv3WxY3ZabC5dEFGhI2Jk3LmnoPQ6RST9

输出:

{ "TableNames": [ "Reply", "Thread" ] }

有关更多信息,请参阅《Amazon DynamoDB 开发人员指南》中的列出表名称

以下代码示例演示如何使用 list-tags-of-resource

AWS CLI

示例 1:列出 DynamoDB 资源的标签

以下list-tags-of-resource示例显示了MusicCollection表的标签。

aws dynamodb list-tags-of-resource \ --resource-arn arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection

输出:

{ "Tags": [ { "Key": "Owner", "Value": "blueTeam" }, { "Key": "Environment", "Value": "Production" } ] }

有关更多信息,请参阅亚马逊 Dynamo DB 开发者指南中的为 DynamoDB 添加标签

示例 2:限制返回的标签数量

以下示例将返回的标签数量限制为 1。响应包含用于检索下一页结果的 NextToken 值。

aws dynamodb list-tags-of-resource \ --resource-arn arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection \ --max-items 1

输出:

{ "Tags": [ { "Key": "Owner", "Value": "blueTeam" } ], "NextToken": "abCDeFGhiJKlmnOPqrSTuvwxYZ1aBCdEFghijK7LM51nOpqRSTuv3WxY3ZabC5dEFGhI2Jk3LmnoPQ6RST9" }

有关更多信息,请参阅亚马逊 Dynamo DB 开发者指南中的为 DynamoDB 添加标签

示例 3:检索下一页结果

以下命令将使用先前对 list-tags-of-resource 命令的调用中的 NextToken 值来检索另一页结果。由于本例中的响应不包含 NextToken 值,因此,我们知道已经到达结果末尾。

aws dynamodb list-tags-of-resource \ --resource-arn arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection \ --starting-token abCDeFGhiJKlmnOPqrSTuvwxYZ1aBCdEFghijK7LM51nOpqRSTuv3WxY3ZabC5dEFGhI2Jk3LmnoPQ6RST9

输出:

{ "Tags": [ { "Key": "Environment", "Value": "Production" } ] }

有关更多信息,请参阅亚马逊 Dynamo DB 开发者指南中的为 DynamoDB 添加标签

以下代码示例演示如何使用 put-item

AWS CLI

示例 1:向表中添加项

以下put-item示例向MusicCollection表中添加了一个新项目。

aws dynamodb put-item \ --table-name MusicCollection \ --item file://item.json \ --return-consumed-capacity TOTAL \ --return-item-collection-metrics SIZE

item.json 的内容:

{ "Artist": {"S": "No One You Know"}, "SongTitle": {"S": "Call Me Today"}, "AlbumTitle": {"S": "Greatest Hits"} }

输出:

{ "ConsumedCapacity": { "TableName": "MusicCollection", "CapacityUnits": 1.0 }, "ItemCollectionMetrics": { "ItemCollectionKey": { "Artist": { "S": "No One You Know" } }, "SizeEstimateRangeGB": [ 0.0, 1.0 ] } }

有关更多信息,请参阅《Amazon DynamoDB 开发人员指南》中的写入项

示例 2:有条件地覆盖表中的项

仅当 MusicCollection 表中的现有项具有值为 Greatest HitsAlbumTitle 属性时,以下 put-item 示例才会覆盖该项。该命令将返回该项先前的值。

aws dynamodb put-item \ --table-name MusicCollection \ --item file://item.json \ --condition-expression "#A = :A" \ --expression-attribute-names file://names.json \ --expression-attribute-values file://values.json \ --return-values ALL_OLD

item.json 的内容:

{ "Artist": {"S": "No One You Know"}, "SongTitle": {"S": "Call Me Today"}, "AlbumTitle": {"S": "Somewhat Famous"} }

names.json 的内容:

{ "#A": "AlbumTitle" }

values.json 的内容:

{ ":A": {"S": "Greatest Hits"} }

输出:

{ "Attributes": { "AlbumTitle": { "S": "Greatest Hits" }, "Artist": { "S": "No One You Know" }, "SongTitle": { "S": "Call Me Today" } } }

如果键已存在,您应看到以下输出:

A client error (ConditionalCheckFailedException) occurred when calling the PutItem operation: The conditional request failed.

有关更多信息,请参阅《Amazon DynamoDB 开发人员指南》中的写入项

以下代码示例演示如何使用 query

AWS CLI

示例 1:查询表

以下 query 示例将查询 MusicCollection 表中的项。该表具有 hash-and-range主键(ArtistSongTitle),但此查询仅指定哈希键值。它返回名为“No One You Know”的艺术家的歌名。

aws dynamodb query \ --table-name MusicCollection \ --projection-expression "SongTitle" \ --key-condition-expression "Artist = :v1" \ --expression-attribute-values file://expression-attributes.json \ --return-consumed-capacity TOTAL

expression-attributes.json 的内容:

{ ":v1": {"S": "No One You Know"} }

输出:

{ "Items": [ { "SongTitle": { "S": "Call Me Today" }, "SongTitle": { "S": "Scared of My Shadow" } } ], "Count": 2, "ScannedCount": 2, "ConsumedCapacity": { "TableName": "MusicCollection", "CapacityUnits": 0.5 } }

有关更多信息,请参阅《Amazon DynamoDB 开发人员指南》中的使用 DynamoDB 中的查询

示例 2:使用强一致性读取查询表并按降序遍历索引

以下示例将执行与第一个示例相同的查询,但返回结果的顺序相反,并且使用强一致性读取。

aws dynamodb query \ --table-name MusicCollection \ --projection-expression "SongTitle" \ --key-condition-expression "Artist = :v1" \ --expression-attribute-values file://expression-attributes.json \ --consistent-read \ --no-scan-index-forward \ --return-consumed-capacity TOTAL

expression-attributes.json 的内容:

{ ":v1": {"S": "No One You Know"} }

输出:

{ "Items": [ { "SongTitle": { "S": "Scared of My Shadow" } }, { "SongTitle": { "S": "Call Me Today" } } ], "Count": 2, "ScannedCount": 2, "ConsumedCapacity": { "TableName": "MusicCollection", "CapacityUnits": 1.0 } }

有关更多信息,请参阅《Amazon DynamoDB 开发人员指南》中的使用 DynamoDB 中的查询

示例 3:筛选出特定结果

以下示例将查询 MusicCollection,但不包括 AlbumTitle 属性中含特定值的结果。请注意,这不会影响 ScannedCountConsumedCapacity,因为筛选器在读取项之后应用。

aws dynamodb query \ --table-name MusicCollection \ --key-condition-expression "#n1 = :v1" \ --filter-expression "NOT (#n2 IN (:v2, :v3))" \ --expression-attribute-names file://names.json \ --expression-attribute-values file://values.json \ --return-consumed-capacity TOTAL

values.json 的内容:

{ ":v1": {"S": "No One You Know"}, ":v2": {"S": "Blue Sky Blues"}, ":v3": {"S": "Greatest Hits"} }

names.json 的内容:

{ "#n1": "Artist", "#n2": "AlbumTitle" }

输出:

{ "Items": [ { "AlbumTitle": { "S": "Somewhat Famous" }, "Artist": { "S": "No One You Know" }, "SongTitle": { "S": "Call Me Today" } } ], "Count": 1, "ScannedCount": 2, "ConsumedCapacity": { "TableName": "MusicCollection", "CapacityUnits": 0.5 } }

有关更多信息,请参阅《Amazon DynamoDB 开发人员指南》中的使用 DynamoDB 中的查询

示例 4:仅检索项计数

以下示例将检索与查询匹配的项计数,但不检索任何项本身。

aws dynamodb query \ --table-name MusicCollection \ --select COUNT \ --key-condition-expression "Artist = :v1" \ --expression-attribute-values file://expression-attributes.json

expression-attributes.json 的内容:

{ ":v1": {"S": "No One You Know"} }

输出:

{ "Count": 2, "ScannedCount": 2, "ConsumedCapacity": null }

有关更多信息,请参阅《Amazon DynamoDB 开发人员指南》中的使用 DynamoDB 中的查询

示例 5:查询索引

以下示例将查询本地二级索引 AlbumTitleIndex。该查询返回基表中已投影到本地二级索引的所有属性。请注意,查询本地二级索引或全局二级索引时,您还必须使用 table-name 参数提供基表的名称。

aws dynamodb query \ --table-name MusicCollection \ --index-name AlbumTitleIndex \ --key-condition-expression "Artist = :v1" \ --expression-attribute-values file://expression-attributes.json \ --select ALL_PROJECTED_ATTRIBUTES \ --return-consumed-capacity INDEXES

expression-attributes.json 的内容:

{ ":v1": {"S": "No One You Know"} }

输出:

{ "Items": [ { "AlbumTitle": { "S": "Blue Sky Blues" }, "Artist": { "S": "No One You Know" }, "SongTitle": { "S": "Scared of My Shadow" } }, { "AlbumTitle": { "S": "Somewhat Famous" }, "Artist": { "S": "No One You Know" }, "SongTitle": { "S": "Call Me Today" } } ], "Count": 2, "ScannedCount": 2, "ConsumedCapacity": { "TableName": "MusicCollection", "CapacityUnits": 0.5, "Table": { "CapacityUnits": 0.0 }, "LocalSecondaryIndexes": { "AlbumTitleIndex": { "CapacityUnits": 0.5 } } } }

有关更多信息,请参阅《Amazon DynamoDB 开发人员指南》中的使用 DynamoDB 中的查询

  • 有关API详细信息,请参阅 “AWS CLI 命令参考” 中的 “查询”。

以下代码示例演示如何使用 restore-table-from-backup

AWS CLI

从现有备份中恢复 DynamoDB 表

以下restore-table-from-backup示例从现有备份中恢复指定的表。

aws dynamodb restore-table-from-backup \ --target-table-name MusicCollection \ --backup-arnarn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection/backup/01576616366715-b4e58d3a

输出:

{ "TableDescription": { "AttributeDefinitions": [ { "AttributeName": "Artist", "AttributeType": "S" }, { "AttributeName": "SongTitle", "AttributeType": "S" } ], "TableName": "MusicCollection2", "KeySchema": [ { "AttributeName": "Artist", "KeyType": "HASH" }, { "AttributeName": "SongTitle", "KeyType": "RANGE" } ], "TableStatus": "CREATING", "CreationDateTime": 1576618274.326, "ProvisionedThroughput": { "NumberOfDecreasesToday": 0, "ReadCapacityUnits": 5, "WriteCapacityUnits": 5 }, "TableSizeBytes": 0, "ItemCount": 0, "TableArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection2", "TableId": "114865c9-5ef3-496c-b4d1-c4cbdd2d44fb", "BillingModeSummary": { "BillingMode": "PROVISIONED" }, "RestoreSummary": { "SourceBackupArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection/backup/01576616366715-b4e58d3a", "SourceTableArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection", "RestoreDateTime": 1576616366.715, "RestoreInProgress": true } } }

有关更多信息,请参阅《亚马逊 DynamoDB 开发者指南》中的 DynamoDB 按需备份和恢复

以下代码示例演示如何使用 restore-table-to-point-in-time

AWS CLI

将 DynamoDB 表还原到某个时间点

以下restore-table-to-point-in-time示例将MusicCollection表恢复到指定的时间点。

aws dynamodb restore-table-to-point-in-time \ --source-table-name MusicCollection \ --target-table-name MusicCollectionRestore \ --restore-date-time 1576622404.0

输出:

{ "TableDescription": { "AttributeDefinitions": [ { "AttributeName": "Artist", "AttributeType": "S" }, { "AttributeName": "SongTitle", "AttributeType": "S" } ], "TableName": "MusicCollectionRestore", "KeySchema": [ { "AttributeName": "Artist", "KeyType": "HASH" }, { "AttributeName": "SongTitle", "KeyType": "RANGE" } ], "TableStatus": "CREATING", "CreationDateTime": 1576623311.86, "ProvisionedThroughput": { "NumberOfDecreasesToday": 0, "ReadCapacityUnits": 5, "WriteCapacityUnits": 5 }, "TableSizeBytes": 0, "ItemCount": 0, "TableArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollectionRestore", "TableId": "befd9e0e-1843-4dc6-a147-d6d00e85cb1f", "BillingModeSummary": { "BillingMode": "PROVISIONED" }, "RestoreSummary": { "SourceTableArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection", "RestoreDateTime": 1576622404.0, "RestoreInProgress": true } } }

有关更多信息,请参阅《亚马逊 DynamoDB 开发者指南》中的 Dyn amoD B Point-in-Time 恢复

以下代码示例演示如何使用 scan

AWS CLI

扫描表

以下 scan 示例将扫描整个 MusicCollection 表,然后将结果范围缩小到艺术家“No One You Know”的歌曲。对于每个项,仅返回专辑名称和歌曲名称。

aws dynamodb scan \ --table-name MusicCollection \ --filter-expression "Artist = :a" \ --projection-expression "#ST, #AT" \ --expression-attribute-names file://expression-attribute-names.json \ --expression-attribute-values file://expression-attribute-values.json

expression-attribute-names.json 的内容:

{ "#ST": "SongTitle", "#AT":"AlbumTitle" }

expression-attribute-values.json 的内容:

{ ":a": {"S": "No One You Know"} }

输出:

{ "Count": 2, "Items": [ { "SongTitle": { "S": "Call Me Today" }, "AlbumTitle": { "S": "Somewhat Famous" } }, { "SongTitle": { "S": "Scared of My Shadow" }, "AlbumTitle": { "S": "Blue Sky Blues" } } ], "ScannedCount": 3, "ConsumedCapacity": null }

有关更多信息,请参阅《Amazon DynamoDB 开发人员指南》中的使用 DynamoDB 中的扫描

  • 有关API详细信息,请参阅《AWS CLI 命令参考》中的 “扫描”。

以下代码示例演示如何使用 tag-resource

AWS CLI

向 DynamoDB 资源添加标签

以下tag-resource示例向表中添加了标签键/值对。MusicCollection

aws dynamodb tag-resource \ --resource-arn arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection \ --tags Key=Owner,Value=blueTeam

此命令不生成任何输出。

有关更多信息,请参阅亚马逊 Dynamo DB 开发者指南中的为 DynamoDB 添加标签

以下代码示例演示如何使用 transact-get-items

AWS CLI

从一个或多个表中以原子方式检索多个项目

以下transact-get-items示例以原子方式检索多个项目。

aws dynamodb transact-get-items \ --transact-items file://transact-items.json \ --return-consumed-capacity TOTAL

transact-items.json 的内容:

[ { "Get": { "Key": { "Artist": {"S": "Acme Band"}, "SongTitle": {"S": "Happy Day"} }, "TableName": "MusicCollection" } }, { "Get": { "Key": { "Artist": {"S": "No One You Know"}, "SongTitle": {"S": "Call Me Today"} }, "TableName": "MusicCollection" } } ]

输出:

{ "ConsumedCapacity": [ { "TableName": "MusicCollection", "CapacityUnits": 4.0, "ReadCapacityUnits": 4.0 } ], "Responses": [ { "Item": { "AlbumTitle": { "S": "Songs About Life" }, "Artist": { "S": "Acme Band" }, "SongTitle": { "S": "Happy Day" } } }, { "Item": { "AlbumTitle": { "S": "Somewhat Famous" }, "Artist": { "S": "No One You Know" }, "SongTitle": { "S": "Call Me Today" } } } ] }

有关更多信息,请参阅亚马逊 DynamoDB 开发者指南中的使用 DynamoDB 事务管理复杂的工作流程

以下代码示例演示如何使用 transact-write-items

AWS CLI

示例 1:以原子方式将项目写入一个或多个表

以下transact-write-items示例更新一个项目并删除另一个项目。如果任一操作失败,或者任一项目包含Rating属性,则操作将失败。

aws dynamodb transact-write-items \ --transact-items file://transact-items.json \ --return-consumed-capacity TOTAL \ --return-item-collection-metrics SIZE

transact-items.json文件内容:

[ { "Update": { "Key": { "Artist": {"S": "Acme Band"}, "SongTitle": {"S": "Happy Day"} }, "UpdateExpression": "SET AlbumTitle = :newval", "ExpressionAttributeValues": { ":newval": {"S": "Updated Album Title"} }, "TableName": "MusicCollection", "ConditionExpression": "attribute_not_exists(Rating)" } }, { "Delete": { "Key": { "Artist": {"S": "No One You Know"}, "SongTitle": {"S": "Call Me Today"} }, "TableName": "MusicCollection", "ConditionExpression": "attribute_not_exists(Rating)" } } ]

输出:

{ "ConsumedCapacity": [ { "TableName": "MusicCollection", "CapacityUnits": 10.0, "WriteCapacityUnits": 10.0 } ], "ItemCollectionMetrics": { "MusicCollection": [ { "ItemCollectionKey": { "Artist": { "S": "No One You Know" } }, "SizeEstimateRangeGB": [ 0.0, 1.0 ] }, { "ItemCollectionKey": { "Artist": { "S": "Acme Band" } }, "SizeEstimateRangeGB": [ 0.0, 1.0 ] } ] } }

有关更多信息,请参阅亚马逊 DynamoDB 开发者指南中的使用 DynamoDB 事务管理复杂的工作流程

示例 2:使用客户端请求令牌以原子方式写入项目

以下命令使用客户端请求令牌来调用 transact-write-items idempotent,这意味着多个调用与单个调用具有相同的效果。

aws dynamodb transact-write-items \ --transact-items file://transact-items.json \ --client-request-token abc123

transact-items.json文件内容:

[ { "Update": { "Key": { "Artist": {"S": "Acme Band"}, "SongTitle": {"S": "Happy Day"} }, "UpdateExpression": "SET AlbumTitle = :newval", "ExpressionAttributeValues": { ":newval": {"S": "Updated Album Title"} }, "TableName": "MusicCollection", "ConditionExpression": "attribute_not_exists(Rating)" } }, { "Delete": { "Key": { "Artist": {"S": "No One You Know"}, "SongTitle": {"S": "Call Me Today"} }, "TableName": "MusicCollection", "ConditionExpression": "attribute_not_exists(Rating)" } } ]

此命令不生成任何输出。

有关更多信息,请参阅亚马逊 DynamoDB 开发者指南中的使用 DynamoDB 事务管理复杂的工作流程

以下代码示例演示如何使用 untag-resource

AWS CLI

从 DynamoDB 资源中移除标签

以下untag-resource示例OwnerMusicCollection表中删除带有密钥的标签。

aws dynamodb untag-resource \ --resource-arn arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection \ --tag-keys Owner

此命令不生成任何输出。

有关更多信息,请参阅亚马逊 Dynamo DB 开发者指南中的为 DynamoDB 添加标签

以下代码示例演示如何使用 update-continuous-backups

AWS CLI

更新 DynamoDB 表的连续备份设置

以下update-continuous-backups示例启用了MusicCollection表的 point-in-time恢复。

aws dynamodb update-continuous-backups \ --table-name MusicCollection \ --point-in-time-recovery-specification PointInTimeRecoveryEnabled=true

输出:

{ "ContinuousBackupsDescription": { "ContinuousBackupsStatus": "ENABLED", "PointInTimeRecoveryDescription": { "PointInTimeRecoveryStatus": "ENABLED", "EarliestRestorableDateTime": 1576622404.0, "LatestRestorableDateTime": 1576622404.0 } } }

有关更多信息,请参阅《亚马逊 DynamoDB 开发者指南》中的 Dyn amoD B Point-in-Time 恢复

以下代码示例演示如何使用 update-contributor-insights

AWS CLI

在表格上启用 “贡献者见解”

以下update-contributor-insights示例在MusicCollection表格和AlbumTitle-index全局二级索引上启用 “贡献者见解”。

aws dynamodb update-contributor-insights \ --table-name MusicCollection \ --index-name AlbumTitle-index \ --contributor-insights-action ENABLE

输出:

{ "TableName": "MusicCollection", "IndexName": "AlbumTitle-index", "ContributorInsightsStatus": "ENABLING" }

有关更多信息,请参阅《亚马逊 DynamoDB 开发者指南》中的 “使用 CloudWatch 贡献者洞察分析 DynamoDB 的数据访问权限”。

以下代码示例演示如何使用 update-global-table-settings

AWS CLI

更新 DynamoDB 全局表上的预配置写入容量设置

以下update-global-table-settings示例将MusicCollection全局表的预配置写入容量设置为 15。

aws dynamodb update-global-table-settings \ --global-table-name MusicCollection \ --global-table-provisioned-write-capacity-units 15

输出:

{ "GlobalTableName": "MusicCollection", "ReplicaSettings": [ { "RegionName": "eu-west-1", "ReplicaStatus": "UPDATING", "ReplicaProvisionedReadCapacityUnits": 10, "ReplicaProvisionedReadCapacityAutoScalingSettings": { "AutoScalingDisabled": true }, "ReplicaProvisionedWriteCapacityUnits": 10, "ReplicaProvisionedWriteCapacityAutoScalingSettings": { "AutoScalingDisabled": true } }, { "RegionName": "us-east-1", "ReplicaStatus": "UPDATING", "ReplicaProvisionedReadCapacityUnits": 10, "ReplicaProvisionedReadCapacityAutoScalingSettings": { "AutoScalingDisabled": true }, "ReplicaProvisionedWriteCapacityUnits": 10, "ReplicaProvisionedWriteCapacityAutoScalingSettings": { "AutoScalingDisabled": true } }, { "RegionName": "us-east-2", "ReplicaStatus": "UPDATING", "ReplicaProvisionedReadCapacityUnits": 10, "ReplicaProvisionedReadCapacityAutoScalingSettings": { "AutoScalingDisabled": true }, "ReplicaProvisionedWriteCapacityUnits": 10, "ReplicaProvisionedWriteCapacityAutoScalingSettings": { "AutoScalingDisabled": true } } ] }

有关更多信息,请参阅亚马逊 DynamoDB 开发者指南中的 Dyn amoD B 全局表。

以下代码示例演示如何使用 update-global-table

AWS CLI

更新 DynamoDB 全局表

以下update-global-table示例将指定区域中的副本添加到MusicCollection全局表。

aws dynamodb update-global-table \ --global-table-name MusicCollection \ --replica-updates Create={RegionName=eu-west-1}

输出:

{ "GlobalTableDescription": { "ReplicationGroup": [ { "RegionName": "eu-west-1" }, { "RegionName": "us-east-2" }, { "RegionName": "us-east-1" } ], "GlobalTableArn": "arn:aws:dynamodb::123456789012:global-table/MusicCollection", "CreationDateTime": 1576625818.532, "GlobalTableStatus": "ACTIVE", "GlobalTableName": "MusicCollection" } }

有关更多信息,请参阅亚马逊 DynamoDB 开发者指南中的 Dyn amoD B 全局表。

以下代码示例演示如何使用 update-item

AWS CLI

示例 1:更新表中的项

下面的 update-item 示例更新 MusicCollection 表的项目。它会添加一个新属性(Year)并修改 AlbumTitle 属性。响应中会返回更新后显示的项中的所有属性。

aws dynamodb update-item \ --table-name MusicCollection \ --key file://key.json \ --update-expression "SET #Y = :y, #AT = :t" \ --expression-attribute-names file://expression-attribute-names.json \ --expression-attribute-values file://expression-attribute-values.json \ --return-values ALL_NEW \ --return-consumed-capacity TOTAL \ --return-item-collection-metrics SIZE

key.json 的内容:

{ "Artist": {"S": "Acme Band"}, "SongTitle": {"S": "Happy Day"} }

expression-attribute-names.json 的内容:

{ "#Y":"Year", "#AT":"AlbumTitle" }

expression-attribute-values.json 的内容:

{ ":y":{"N": "2015"}, ":t":{"S": "Louder Than Ever"} }

输出:

{ "Attributes": { "AlbumTitle": { "S": "Louder Than Ever" }, "Awards": { "N": "10" }, "Artist": { "S": "Acme Band" }, "Year": { "N": "2015" }, "SongTitle": { "S": "Happy Day" } }, "ConsumedCapacity": { "TableName": "MusicCollection", "CapacityUnits": 3.0 }, "ItemCollectionMetrics": { "ItemCollectionKey": { "Artist": { "S": "Acme Band" } }, "SizeEstimateRangeGB": [ 0.0, 1.0 ] } }

有关更多信息,请参阅《Amazon DynamoDB 开发人员指南》中的写入项

示例 2:有条件地更新项

以下示例将更新 MusicCollection 表中的项,但前提是现有项还没有 Year 属性。

aws dynamodb update-item \ --table-name MusicCollection \ --key file://key.json \ --update-expression "SET #Y = :y, #AT = :t" \ --expression-attribute-names file://expression-attribute-names.json \ --expression-attribute-values file://expression-attribute-values.json \ --condition-expression "attribute_not_exists(#Y)"

key.json 的内容:

{ "Artist": {"S": "Acme Band"}, "SongTitle": {"S": "Happy Day"} }

expression-attribute-names.json 的内容:

{ "#Y":"Year", "#AT":"AlbumTitle" }

expression-attribute-values.json 的内容:

{ ":y":{"N": "2015"}, ":t":{"S": "Louder Than Ever"} }

如果该项已有 Year 属性,DynamoDB 会返回以下输出。

An error occurred (ConditionalCheckFailedException) when calling the UpdateItem operation: The conditional request failed

有关更多信息,请参阅《Amazon DynamoDB 开发人员指南》中的写入项

以下代码示例演示如何使用 update-table-replica-auto-scaling

AWS CLI

更新跨全局表副本的 auto 缩放设置

以下update-table-replica-auto-scaling示例更新了指定全局表副本间的写入容量 auto Scaling 设置。

aws dynamodb update-table-replica-auto-scaling \ --table-name MusicCollection \ --provisioned-write-capacity-auto-scaling-update file://auto-scaling-policy.json

auto-scaling-policy.json 的内容:

{ "MinimumUnits": 10, "MaximumUnits": 100, "AutoScalingDisabled": false, "ScalingPolicyUpdate": { "PolicyName": "DynamoDBWriteCapacityUtilization:table/MusicCollection", "TargetTrackingScalingPolicyConfiguration": { "TargetValue": 80 } } }

输出:

{ "TableAutoScalingDescription": { "TableName": "MusicCollection", "TableStatus": "ACTIVE", "Replicas": [ { "RegionName": "eu-central-1", "GlobalSecondaryIndexes": [], "ReplicaProvisionedReadCapacityAutoScalingSettings": { "MinimumUnits": 5, "MaximumUnits": 40000, "AutoScalingRoleArn": "arn:aws:iam::123456789012:role/aws-service-role/dynamodb.application-autoscaling.amazonaws.com/AWSServiceRoleForApplicationAutoScaling_DynamoDBTable", "ScalingPolicies": [ { "PolicyName": "DynamoDBReadCapacityUtilization:table/MusicCollection", "TargetTrackingScalingPolicyConfiguration": { "TargetValue": 70.0 } } ] }, "ReplicaProvisionedWriteCapacityAutoScalingSettings": { "MinimumUnits": 10, "MaximumUnits": 100, "AutoScalingRoleArn": "arn:aws:iam::123456789012:role/aws-service-role/dynamodb.application-autoscaling.amazonaws.com/AWSServiceRoleForApplicationAutoScaling_DynamoDBTable", "ScalingPolicies": [ { "PolicyName": "DynamoDBWriteCapacityUtilization:table/MusicCollection", "TargetTrackingScalingPolicyConfiguration": { "TargetValue": 80.0 } } ] }, "ReplicaStatus": "ACTIVE" }, { "RegionName": "us-east-1", "GlobalSecondaryIndexes": [], "ReplicaProvisionedReadCapacityAutoScalingSettings": { "MinimumUnits": 5, "MaximumUnits": 40000, "AutoScalingRoleArn": "arn:aws:iam::123456789012:role/aws-service-role/dynamodb.application-autoscaling.amazonaws.com/AWSServiceRoleForApplicationAutoScaling_DynamoDBTable", "ScalingPolicies": [ { "PolicyName": "DynamoDBReadCapacityUtilization:table/MusicCollection", "TargetTrackingScalingPolicyConfiguration": { "TargetValue": 70.0 } } ] }, "ReplicaProvisionedWriteCapacityAutoScalingSettings": { "MinimumUnits": 10, "MaximumUnits": 100, "AutoScalingRoleArn": "arn:aws:iam::123456789012:role/aws-service-role/dynamodb.application-autoscaling.amazonaws.com/AWSServiceRoleForApplicationAutoScaling_DynamoDBTable", "ScalingPolicies": [ { "PolicyName": "DynamoDBWriteCapacityUtilization:table/MusicCollection", "TargetTrackingScalingPolicyConfiguration": { "TargetValue": 80.0 } } ] }, "ReplicaStatus": "ACTIVE" }, { "RegionName": "us-east-2", "GlobalSecondaryIndexes": [], "ReplicaProvisionedReadCapacityAutoScalingSettings": { "MinimumUnits": 5, "MaximumUnits": 40000, "AutoScalingRoleArn": "arn:aws:iam::123456789012:role/aws-service-role/dynamodb.application-autoscaling.amazonaws.com/AWSServiceRoleForApplicationAutoScaling_DynamoDBTable", "ScalingPolicies": [ { "PolicyName": "DynamoDBReadCapacityUtilization:table/MusicCollection", "TargetTrackingScalingPolicyConfiguration": { "TargetValue": 70.0 } } ] }, "ReplicaProvisionedWriteCapacityAutoScalingSettings": { "MinimumUnits": 10, "MaximumUnits": 100, "AutoScalingRoleArn": "arn:aws:iam::123456789012:role/aws-service-role/dynamodb.application-autoscaling.amazonaws.com/AWSServiceRoleForApplicationAutoScaling_DynamoDBTable", "ScalingPolicies": [ { "PolicyName": "DynamoDBWriteCapacityUtilization:table/MusicCollection", "TargetTrackingScalingPolicyConfiguration": { "TargetValue": 80.0 } } ] }, "ReplicaStatus": "ACTIVE" } ] } }

有关更多信息,请参阅亚马逊 DynamoDB 开发者指南中的 Dyn amoD B 全局表。

以下代码示例演示如何使用 update-table

AWS CLI

示例 1:修改表的计费模式

以下 update-table 示例增加了 MusicCollection 表上的预置读取和写入容量。

aws dynamodb update-table \ --table-name MusicCollection \ --billing-mode PROVISIONED \ --provisioned-throughput ReadCapacityUnits=15,WriteCapacityUnits=10

输出:

{ "TableDescription": { "AttributeDefinitions": [ { "AttributeName": "AlbumTitle", "AttributeType": "S" }, { "AttributeName": "Artist", "AttributeType": "S" }, { "AttributeName": "SongTitle", "AttributeType": "S" } ], "TableName": "MusicCollection", "KeySchema": [ { "AttributeName": "Artist", "KeyType": "HASH" }, { "AttributeName": "SongTitle", "KeyType": "RANGE" } ], "TableStatus": "UPDATING", "CreationDateTime": "2020-05-26T15:59:49.473000-07:00", "ProvisionedThroughput": { "LastIncreaseDateTime": "2020-07-28T13:18:18.921000-07:00", "NumberOfDecreasesToday": 0, "ReadCapacityUnits": 15, "WriteCapacityUnits": 10 }, "TableSizeBytes": 182, "ItemCount": 2, "TableArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection", "TableId": "abcd0123-01ab-23cd-0123-abcdef123456", "BillingModeSummary": { "BillingMode": "PROVISIONED", "LastUpdateToPayPerRequestDateTime": "2020-07-28T13:14:48.366000-07:00" } } }

有关更多信息,请参阅《Amazon DynamoDB 开发人员指南》中的更新表

示例 2:创建全局二级索引

下面的示例在 MusicCollection 表上添加了全局二级索引。

aws dynamodb update-table \ --table-name MusicCollection \ --attribute-definitions AttributeName=AlbumTitle,AttributeType=S \ --global-secondary-index-updates file://gsi-updates.json

gsi-updates.json 的内容:

[ { "Create": { "IndexName": "AlbumTitle-index", "KeySchema": [ { "AttributeName": "AlbumTitle", "KeyType": "HASH" } ], "ProvisionedThroughput": { "ReadCapacityUnits": 10, "WriteCapacityUnits": 10 }, "Projection": { "ProjectionType": "ALL" } } } ]

输出:

{ "TableDescription": { "AttributeDefinitions": [ { "AttributeName": "AlbumTitle", "AttributeType": "S" }, { "AttributeName": "Artist", "AttributeType": "S" }, { "AttributeName": "SongTitle", "AttributeType": "S" } ], "TableName": "MusicCollection", "KeySchema": [ { "AttributeName": "Artist", "KeyType": "HASH" }, { "AttributeName": "SongTitle", "KeyType": "RANGE" } ], "TableStatus": "UPDATING", "CreationDateTime": "2020-05-26T15:59:49.473000-07:00", "ProvisionedThroughput": { "LastIncreaseDateTime": "2020-07-28T12:59:17.537000-07:00", "NumberOfDecreasesToday": 0, "ReadCapacityUnits": 15, "WriteCapacityUnits": 10 }, "TableSizeBytes": 182, "ItemCount": 2, "TableArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection", "TableId": "abcd0123-01ab-23cd-0123-abcdef123456", "BillingModeSummary": { "BillingMode": "PROVISIONED", "LastUpdateToPayPerRequestDateTime": "2020-07-28T13:14:48.366000-07:00" }, "GlobalSecondaryIndexes": [ { "IndexName": "AlbumTitle-index", "KeySchema": [ { "AttributeName": "AlbumTitle", "KeyType": "HASH" } ], "Projection": { "ProjectionType": "ALL" }, "IndexStatus": "CREATING", "Backfilling": false, "ProvisionedThroughput": { "NumberOfDecreasesToday": 0, "ReadCapacityUnits": 10, "WriteCapacityUnits": 10 }, "IndexSizeBytes": 0, "ItemCount": 0, "IndexArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection/index/AlbumTitle-index" } ] } }

有关更多信息,请参阅《Amazon DynamoDB 开发人员指南》中的更新表

示例 3:在表上启用 DynamoDB Streams

以下命令在 MusicCollection 表上启用 DynamoDB Streams。

aws dynamodb update-table \ --table-name MusicCollection \ --stream-specification StreamEnabled=true,StreamViewType=NEW_IMAGE

输出:

{ "TableDescription": { "AttributeDefinitions": [ { "AttributeName": "AlbumTitle", "AttributeType": "S" }, { "AttributeName": "Artist", "AttributeType": "S" }, { "AttributeName": "SongTitle", "AttributeType": "S" } ], "TableName": "MusicCollection", "KeySchema": [ { "AttributeName": "Artist", "KeyType": "HASH" }, { "AttributeName": "SongTitle", "KeyType": "RANGE" } ], "TableStatus": "UPDATING", "CreationDateTime": "2020-05-26T15:59:49.473000-07:00", "ProvisionedThroughput": { "LastIncreaseDateTime": "2020-07-28T12:59:17.537000-07:00", "NumberOfDecreasesToday": 0, "ReadCapacityUnits": 15, "WriteCapacityUnits": 10 }, "TableSizeBytes": 182, "ItemCount": 2, "TableArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection", "TableId": "abcd0123-01ab-23cd-0123-abcdef123456", "BillingModeSummary": { "BillingMode": "PROVISIONED", "LastUpdateToPayPerRequestDateTime": "2020-07-28T13:14:48.366000-07:00" }, "LocalSecondaryIndexes": [ { "IndexName": "AlbumTitleIndex", "KeySchema": [ { "AttributeName": "Artist", "KeyType": "HASH" }, { "AttributeName": "AlbumTitle", "KeyType": "RANGE" } ], "Projection": { "ProjectionType": "INCLUDE", "NonKeyAttributes": [ "Year", "Genre" ] }, "IndexSizeBytes": 139, "ItemCount": 2, "IndexArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection/index/AlbumTitleIndex" } ], "GlobalSecondaryIndexes": [ { "IndexName": "AlbumTitle-index", "KeySchema": [ { "AttributeName": "AlbumTitle", "KeyType": "HASH" } ], "Projection": { "ProjectionType": "ALL" }, "IndexStatus": "ACTIVE", "ProvisionedThroughput": { "NumberOfDecreasesToday": 0, "ReadCapacityUnits": 10, "WriteCapacityUnits": 10 }, "IndexSizeBytes": 0, "ItemCount": 0, "IndexArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection/index/AlbumTitle-index" } ], "StreamSpecification": { "StreamEnabled": true, "StreamViewType": "NEW_IMAGE" }, "LatestStreamLabel": "2020-07-28T21:53:39.112", "LatestStreamArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection/stream/2020-07-28T21:53:39.112" } }

有关更多信息,请参阅《Amazon DynamoDB 开发人员指南》中的更新表

示例 4:启用服务器端加密

以下示例在 MusicCollection 表上启用服务器端加密。

aws dynamodb update-table \ --table-name MusicCollection \ --sse-specification Enabled=true,SSEType=KMS

输出:

{ "TableDescription": { "AttributeDefinitions": [ { "AttributeName": "AlbumTitle", "AttributeType": "S" }, { "AttributeName": "Artist", "AttributeType": "S" }, { "AttributeName": "SongTitle", "AttributeType": "S" } ], "TableName": "MusicCollection", "KeySchema": [ { "AttributeName": "Artist", "KeyType": "HASH" }, { "AttributeName": "SongTitle", "KeyType": "RANGE" } ], "TableStatus": "ACTIVE", "CreationDateTime": "2020-05-26T15:59:49.473000-07:00", "ProvisionedThroughput": { "LastIncreaseDateTime": "2020-07-28T12:59:17.537000-07:00", "NumberOfDecreasesToday": 0, "ReadCapacityUnits": 15, "WriteCapacityUnits": 10 }, "TableSizeBytes": 182, "ItemCount": 2, "TableArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection", "TableId": "abcd0123-01ab-23cd-0123-abcdef123456", "BillingModeSummary": { "BillingMode": "PROVISIONED", "LastUpdateToPayPerRequestDateTime": "2020-07-28T13:14:48.366000-07:00" }, "LocalSecondaryIndexes": [ { "IndexName": "AlbumTitleIndex", "KeySchema": [ { "AttributeName": "Artist", "KeyType": "HASH" }, { "AttributeName": "AlbumTitle", "KeyType": "RANGE" } ], "Projection": { "ProjectionType": "INCLUDE", "NonKeyAttributes": [ "Year", "Genre" ] }, "IndexSizeBytes": 139, "ItemCount": 2, "IndexArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection/index/AlbumTitleIndex" } ], "GlobalSecondaryIndexes": [ { "IndexName": "AlbumTitle-index", "KeySchema": [ { "AttributeName": "AlbumTitle", "KeyType": "HASH" } ], "Projection": { "ProjectionType": "ALL" }, "IndexStatus": "ACTIVE", "ProvisionedThroughput": { "NumberOfDecreasesToday": 0, "ReadCapacityUnits": 10, "WriteCapacityUnits": 10 }, "IndexSizeBytes": 0, "ItemCount": 0, "IndexArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection/index/AlbumTitle-index" } ], "StreamSpecification": { "StreamEnabled": true, "StreamViewType": "NEW_IMAGE" }, "LatestStreamLabel": "2020-07-28T21:53:39.112", "LatestStreamArn": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection/stream/2020-07-28T21:53:39.112", "SSEDescription": { "Status": "UPDATING" } } }

有关更多信息,请参阅《Amazon DynamoDB 开发人员指南》中的更新表

以下代码示例演示如何使用 update-time-to-live

AWS CLI

更新表的生存时间设置

以下 update-time-to-live 示例在指定表上启用生存时间设置。

aws dynamodb update-time-to-live \ --table-name MusicCollection \ --time-to-live-specification Enabled=true,AttributeName=ttl

输出:

{ "TimeToLiveSpecification": { "Enabled": true, "AttributeName": "ttl" } }

有关更多信息,请参阅《Amazon DynamoDB 开发人员指南》中的生存时间