使用範例 AWS CLI - AWS Command Line Interface

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

使用範例 AWS CLI

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

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

Scenarios (案例) 是向您展示如何呼叫相同服務中的多個函數來完成特定任務的程式碼範例。

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

主題

動作

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

  • 如需 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 開發人員指南中的 B atch 操作

  • 如需 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 Dynam oDB 開發人員指南中的隨需 Backup 和還原

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

下列程式碼範例會示範如何使用create-global-table

AWS CLI

建立全域表的步驟

下列create-global-table範例會從指定的個別 Region 中的兩個相同資料表建立全 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 全域表

下列程式碼範例會示範如何使用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,可讓您有效率地找到個別使用者在特定遊戲中的最佳分數,而 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 串流的資料擷取

範例 9:若要建立具有標準不常存取類別的資料表

下列範例會建立名為的資料表,GameScores並指派標準不常存取 (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 開發人員指南中的使用刪除保護

  • 如需 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 Dynam oDB 開發人員指南中的隨需 Backup 和還原

  • 如需 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:若要有條件地刪除項目

下列範例只會刪除ProductCatalog表格中的某ProductCategory個物件,Sporting GoodsGardening Supplies且其价格在 500 到 600 之間。它返回有關被刪除的項目的詳細信息。

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 Dynam oDB 開發人員指南中的隨需 Backup 和還原

  • 如需 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 的時間點復原

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

如需詳細資訊,請參閱 Amazon DynamoDB DynamoDB 開發 CloudWatch 人員指南中的使用 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 端點和配額

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

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

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

AWS CLI

若要檢視佈建容量限制

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

aws dynamodb describe-limits

輸出:

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

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

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

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

AWS CLI

若要檢視全域表格複本之間的 auto 調整設定

下列describe-table-replica-auto-scaling範例會顯示MusicCollection全域表格複本之間的 auto 調整設定。

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 全域表

下列程式碼範例會示範如何使用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表擷取項目。該表具有 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 Dynam oDB 開發人員指南中的隨需 Backup 和還原

範例 2:列出特定時間範圍內的使用者建立的備份

下列範例僅列出建立日期介於 2020 年 1 月 1 日至 2020 年 3 月 1 日之間的使用者 (而非 DynamoDB 自動建立的MusicCollection資料表) 所建立的表格備份。

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 Dynam oDB 開發人員指南中的隨需 Backup 和還原

範例 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 Dynam oDB 開發人員指南中的隨需 Backup 和還原

範例 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 Dynam oDB 開發人員指南中的隨需 Backup 和還原

範例 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 Dynam oDB 開發人員指南中的隨需 Backup 和還原

  • 如需 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 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" }

如需詳細資訊,請參閱 Amazon DynamoDB 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" } ] }

如需詳細資訊,請參閱 Amazon DynamoDB 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" } ] } ] }

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

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

下列程式碼範例會示範如何使用list-tables

AWS CLI

範例 1:列出表格

下列list-tables範例會列出與目前 AWS 帳戶和 Region 相關聯的所有表格。

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 記。

範例 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 記。

範例 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 記。

下列程式碼範例會示範如何使用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:若要有條件地覆寫資料表中的項目

下列put-item範例只有在現有項目具有值為的AlbumTitle屬性時,才會覆寫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資料表中的項目。該表有一個 hash-and-range 主鍵(ArtistSongTitle),但這個查詢只指定哈希鍵值。它返回名為「沒有人你認識」的藝術家的歌曲標題。

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 命令參考》中的 Query

下列程式碼範例會示範如何使用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 Dynam oDB 開發人員指南中的隨需 Backup 和還原

下列程式碼範例會示範如何使用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 的時間點復原

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

AWS CLI

掃描表格

下列scan範例會掃描整個MusicCollection表格,然後將結果縮小為演出者「沒有您認識的人」的歌曲。對於每個項目,僅返回專輯標題和歌曲標題。

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 開發人員指南中的在 Dynamo DB 中使用掃描

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

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

  • 如需 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 交易管理複雜的工作流程

  • 如需 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 交易管理複雜的工作流程

示例 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 交易管理複雜的工作流程

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

  • 如需 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 的時間點復原

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

如需詳細資訊,請參閱 Amazon DynamoDB 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 } } ] }

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

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

若要更新全域表格複本之間的 auto 調整設定

下列update-table-replica-auto-scaling範例會更新指定全域資料表之間複本的寫入容量 auto 調整設定。

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 全域表

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

下列命令會在資料表上啟用 DynamoDB Streams。MusicCollection

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中的。