使用 的 DynamoDB 範例 AWS CLI - AWS SDK 程式碼範例

文件 AWS SDK AWS 範例 SDK 儲存庫中有更多可用的 GitHub 範例。

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

使用 的 DynamoDB 範例 AWS CLI

下列程式碼範例示範如何搭配 AWS Command Line Interface DynamoDB 使用 來執行動作和實作常見案例。

Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然 動作會示範如何呼叫個別服務函數,但您可以在其相關案例中查看內容中的動作。

每個範例都包含完整原始程式碼的連結,您可以在其中找到如何在內容中設定和執行程式碼的指示。

主題

動作

下列程式碼範例示範如何使用 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 開發人員指南中的批次操作

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 BatchGetItem

下列程式碼範例示範如何使用 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 開發人員指南中的批次操作

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 BatchWriteItem

下列程式碼範例示範如何使用 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 } }

如需詳細資訊,請參閱 Amazon DynamoDB 開發人員指南中的 DynamoDB 隨需備份和還原 DynamoDB

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 CreateBackup

下列程式碼範例示範如何使用 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" } }

如需詳細資訊,請參閱 Amazon DynamoDB 開發人員指南中的 DynamoDB Global Tables DynamoDB

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 CreateGlobalTable

下列程式碼範例示範如何使用 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其中具有名為 的 Local Secondary IndexAlbumTitleIndex

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其 Global Secondary Index 名為 GameTitleIndex。基礎資料表具有 的分割區索引鍵UserId和 的排序索引鍵GameTitle,可讓您有效率地尋找個別使用者在特定遊戲的最佳分數,而 GSI 具有 的分割區索引鍵GameTitle和 的排序索引鍵TopScore,可讓您快速尋找特定遊戲的整體最高分數。

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:建立已啟用串流的資料表

下列範例會建立名為 的資料表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:建立已啟用僅限金鑰串流的資料表

下列範例會建立名為 的資料表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 串流的資料擷取 DynamoDB

範例 9:使用標準不常存取類別建立資料表

下列範例會建立名為 的資料表,GameScores並指派 Standard-Infrequent Access (DynamoDB Standard-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:建立已啟用 Delete Protection 的資料表

下列範例會建立名為 的資料表,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 開發人員指南中的使用刪除保護

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 CreateTable

下列程式碼範例示範如何使用 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": {} } }

如需詳細資訊,請參閱 Amazon DynamoDB 開發人員指南中的 DynamoDB 隨需備份和還原 DynamoDB

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 DeleteBackup

下列程式碼範例示範如何使用 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 開發人員指南中的撰寫項目

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 DeleteItem

下列程式碼範例示範如何使用 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 開發人員指南中的刪除資料表

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 DeleteTable

下列程式碼範例示範如何使用 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": {} } }

如需詳細資訊,請參閱 Amazon DynamoDB 開發人員指南中的 DynamoDB 隨需備份和還原 DynamoDB

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 DescribeBackup

下列程式碼範例示範如何使用 describe-continuous-backups

AWS CLI

取得 DynamoDB 資料表的持續備份相關資訊

下列describe-continuous-backups範例顯示MusicCollection資料表連續備份設定的詳細資訊。

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

輸出:

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

如需詳細資訊,請參閱 Amazon DynamoDB 開發人員指南中的 DynamoDB Point-in-Time Recovery DynamoDB

下列程式碼範例示範如何使用 describe-contributor-insights

AWS CLI

檢視 DynamoDB 資料表的 Contributor Insights 設定

下列describe-contributor-insights範例顯示MusicCollection資料表和AlbumTitle-index全域次要索引的 Contributor Insights 設定。

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 }

如需詳細資訊,請參閱 Amazon DynamoDB 開發人員指南中的使用 CloudWatch Contributor Insights for DynamoDB 分析資料存取 DynamoDB

下列程式碼範例示範如何使用 describe-endpoints

AWS CLI

檢視區域端點資訊

下列describe-endpoints範例顯示目前 AWS 區域端點的詳細資訊。

aws dynamodb describe-endpoints

輸出:

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

如需詳細資訊,請參閱 AWS 一般參考中的 Amazon DynamoDB 端點和配額

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 DescribeEndpoints

下列程式碼範例示範如何使用 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 } } ] }

如需詳細資訊,請參閱 Amazon DynamoDB 開發人員指南中的 DynamoDB Global Tables DynamoDB

下列程式碼範例示範如何使用 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" } }

如需詳細資訊,請參閱 Amazon DynamoDB 開發人員指南中的 DynamoDB Global Tables DynamoDB

下列程式碼範例示範如何使用 describe-limits

AWS CLI

檢視佈建容量限制

下列describe-limits範例顯示目前 AWS 區域中您帳戶的佈建容量限制。

aws dynamodb describe-limits

輸出:

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

如需詳細資訊,請參閱 Amazon DynamoDB 開發人員指南中的 DynamoDB 中的限制 DynamoDB

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 DescribeLimits

下列程式碼範例示範如何使用 describe-table-replica-auto-scaling

AWS CLI

檢視全域資料表複本之間的自動擴展設定

下列describe-table-replica-auto-scaling範例顯示MusicCollection全域資料表複本之間的自動擴展設定。

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

如需詳細資訊,請參閱 Amazon DynamoDB 開發人員指南中的 DynamoDB Global Tables DynamoDB

下列程式碼範例示範如何使用 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 開發人員指南中的描述資料表

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 DescribeTable

下列程式碼範例示範如何使用 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資料表擷取項目。資料表具有 a 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 開發人員指南中的讀取項目

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 GetItem

下列程式碼範例示範如何使用 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 } ] }

如需詳細資訊,請參閱 Amazon DynamoDB 開發人員指南中的 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 } ] }

如需詳細資訊,請參閱 Amazon DynamoDB 開發人員指南中的 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 } ] }

如需詳細資訊,請參閱 Amazon DynamoDB 開發人員指南中的 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" }

如需詳細資訊,請參閱 Amazon DynamoDB 開發人員指南中的 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 } ] }

如需詳細資訊,請參閱 Amazon DynamoDB 開發人員指南中的 DynamoDB 隨需備份和還原 DynamoDB

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 ListBackups

下列程式碼範例示範如何使用 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" } ] }

如需詳細資訊,請參閱 Amazon DynamoDB 開發人員指南中的使用 CloudWatch Contributor Insights for DynamoDB 分析資料存取 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" }

如需詳細資訊,請參閱 Amazon DynamoDB 開發人員指南中的使用 CloudWatch Contributor Insights for DynamoDB 分析資料存取 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" } ] }

如需詳細資訊,請參閱 Amazon DynamoDB 開發人員指南中的使用 CloudWatch Contributor Insights for DynamoDB 分析資料存取 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" } ] } ] }

如需詳細資訊,請參閱 Amazon DynamoDB 開發人員指南中的 DynamoDB Global Tables DynamoDB

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 ListGlobalTables

下列程式碼範例示範如何使用 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 開發人員指南中的列出資料表名稱

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 ListTables

下列程式碼範例示範如何使用 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" } ] }

如需詳細資訊,請參閱 Amazon DynamoDB 開發人員指南中的 DynamoDB 標記 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" }

如需詳細資訊,請參閱 Amazon DynamoDB 開發人員指南中的 DynamoDB 標記 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" } ] }

如需詳細資訊,請參閱 Amazon DynamoDB 開發人員指南中的 DynamoDB 標記 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:有條件覆寫資料表中的項目

只有當現有項目具有值為 的AlbumTitle屬性時,下列put-item範例才會覆寫MusicCollection資料表中的現有項目Greatest Hits。命令會傳回項目的上一個值。

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 開發人員指南中的撰寫項目

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 PutItem

下列程式碼範例示範如何使用 query

AWS CLI

範例 1:查詢資料表

下列query範例會查詢 MusicCollection 資料表中的項目。資料表具有 a 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 中使用查詢 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 中使用查詢 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 中使用查詢 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 中使用查詢 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 中使用查詢 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 } } }

如需詳細資訊,請參閱 Amazon DynamoDB 開發人員指南中的 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 } } }

如需詳細資訊,請參閱 Amazon DynamoDB 開發人員指南中的 DynamoDB Point-in-Time Recovery DynamoDB

下列程式碼範例示範如何使用 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 中使用掃描 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

此命令不會產生輸出。

如需詳細資訊,請參閱 Amazon DynamoDB 開發人員指南中的 DynamoDB 標記 DynamoDB

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 TagResource

下列程式碼範例示範如何使用 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" } } } ] }

如需詳細資訊,請參閱 Amazon DynamoDB 開發人員指南中的使用 DynamoDB 交易管理複雜工作流程 DynamoDB

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 TransactGetItems

下列程式碼範例示範如何使用 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 ] } ] } }

如需詳細資訊,請參閱 Amazon DynamoDB 開發人員指南中的使用 DynamoDB 交易管理複雜工作流程 DynamoDB

範例 2:使用用戶端請求權杖以原子方式寫入項目

下列命令使用用戶端請求權杖對transact-write-items等電位進行呼叫,這表示多個呼叫具有與單一呼叫相同的效果。

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

此命令不會產生輸出。

如需詳細資訊,請參閱 Amazon DynamoDB 開發人員指南中的使用 DynamoDB 交易管理複雜工作流程 DynamoDB

下列程式碼範例示範如何使用 untag-resource

AWS CLI

從 DynamoDB 資源中移除標籤

下列untag-resource範例會從MusicCollection資料表中移除具有 金鑰Owner的標籤。

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

此命令不會產生輸出。

如需詳細資訊,請參閱 Amazon DynamoDB 開發人員指南中的 DynamoDB 標記 DynamoDB

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 UntagResource

下列程式碼範例示範如何使用 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 } } }

如需詳細資訊,請參閱 Amazon DynamoDB 開發人員指南中的 DynamoDB Point-in-Time Recovery DynamoDB

下列程式碼範例示範如何使用 update-contributor-insights

AWS CLI

在資料表上啟用貢獻者洞察

下列update-contributor-insights範例會在MusicCollection資料表和AlbumTitle-index全域次要索引上啟用 Contributor Insights。

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

輸出:

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

如需詳細資訊,請參閱 Amazon DynamoDB 開發人員指南中的使用 CloudWatch Contributor Insights for DynamoDB 分析資料存取 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 } } ] }

如需詳細資訊,請參閱 Amazon DynamoDB 開發人員指南中的 DynamoDB Global Tables DynamoDB

下列程式碼範例示範如何使用 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" } }

如需詳細資訊,請參閱 Amazon DynamoDB 開發人員指南中的 DynamoDB Global Tables DynamoDB

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 UpdateGlobalTable

下列程式碼範例示範如何使用 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 開發人員指南中的撰寫項目

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 UpdateItem

下列程式碼範例示範如何使用 update-table-replica-auto-scaling

AWS CLI

更新全域資料表複本之間的自動擴展設定

下列update-table-replica-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" } ] } }

如需詳細資訊,請參閱 Amazon DynamoDB 開發人員指南中的 DynamoDB Global Tables DynamoDB

下列程式碼範例示範如何使用 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 串流

下列命令會在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 開發人員指南中的更新資料表

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 UpdateTable

下列程式碼範例示範如何使用 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 開發人員指南中的存留時間

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 UpdateTimeToLive