本文件 AWS CLI 僅適用於 的第 1 版。如需與 第 2 版相關的文件 AWS CLI,請參閱 第 2 版使用者指南 。
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用 的 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-capacityTOTAL
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詳細資訊,請參閱 命令參考 BatchGetItem
中的 。 AWS CLI
-
下列程式碼範例示範如何使用 batch-write-item
。
- AWS CLI
-
將多個項目新增至資料表
下列
batch-write-item
範例使用一批三個PutItem
請求,將三個新項目新增至MusicCollection
資料表。它還請求有關操作消耗的寫入容量單位數量以及操作修改的任何項目集合的資訊。aws dynamodb batch-write-item \ --request-items
file://request-items.json
\ --return-consumed-capacityINDEXES
\ --return-item-collection-metricsSIZE
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詳細資訊,請參閱 命令參考 BatchWriteItem
中的 。 AWS CLI
-
下列程式碼範例示範如何使用 create-backup
。
- AWS CLI
-
為現有的 DynamoDB 資料表建立備份
下列
create-backup
範例會建立MusicCollection
資料表的備份。aws dynamodb create-backup \ --table-name
MusicCollection
\ --backup-nameMusicCollectionBackup
輸出:
{ "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詳細資訊,請參閱 命令參考 CreateBackup
中的 。 AWS CLI
-
下列程式碼範例示範如何使用 create-global-table
。
- AWS CLI
-
建立全域資料表
下列
create-global-table
範例會從指定、個別 AWS 區域中的兩個相同資料表建立全域資料表。aws dynamodb create-global-table \ --global-table-name
MusicCollection
\ --replication-groupRegionName=us-east-2
RegionName=us-east-1
\ --regionus-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詳細資訊,請參閱 命令參考 CreateGlobalTable
中的 。 AWS CLI
-
下列程式碼範例示範如何使用 create-table
。
- AWS CLI
-
範例 1:使用標籤建立資料表
下列
create-table
範例使用指定的屬性和金鑰結構描述來建立名為 的資料表MusicCollection
。此資料表使用佈建的輸送量,並使用預設 AWS 擁有的 進行靜態加密CMK。命令也會將標籤套用至資料表,索引鍵為Owner
,值為blueTeam
。aws dynamodb create-table \ --table-name
MusicCollection
\ --attribute-definitionsAttributeName=Artist,AttributeType=S
AttributeName=SongTitle,AttributeType=S
\ --key-schemaAttributeName=Artist,KeyType=HASH
AttributeName=SongTitle,KeyType=RANGE
\ --provisioned-throughputReadCapacityUnits=5,WriteCapacityUnits=5
\ --tagsKey=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-definitionsAttributeName=Artist,AttributeType=S
AttributeName=SongTitle,AttributeType=S
\ --key-schemaAttributeName=Artist,KeyType=HASH
AttributeName=SongTitle,KeyType=RANGE
\ --billing-modePAY_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:建立資料表並使用 Customer Managed 加密 CMK
下列範例會建立名為 的資料表,
MusicCollection
並使用客戶受管 對其進行加密CMK。aws dynamodb create-table \ --table-name
MusicCollection
\ --attribute-definitionsAttributeName=Artist,AttributeType=S
AttributeName=SongTitle,AttributeType=S
\ --key-schemaAttributeName=Artist,KeyType=HASH
AttributeName=SongTitle,KeyType=RANGE
\ --provisioned-throughputReadCapacityUnits=5,WriteCapacityUnits=5
\ --sse-specificationEnabled=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:使用 Local Secondary Index 建立資料表
下列範例使用指定的屬性和金鑰結構描述來建立名為 的資料表,
MusicCollection
其名稱為 Local Secondary Index,名為AlbumTitleIndex
。aws dynamodb create-table \ --table-name
MusicCollection
\ --attribute-definitionsAttributeName=Artist,AttributeType=S
AttributeName=SongTitle,AttributeType=S
AttributeName=AlbumTitle,AttributeType=S
\ --key-schemaAttributeName=Artist,KeyType=HASH
AttributeName=SongTitle,KeyType=RANGE
\ --provisioned-throughputReadCapacityUnits=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-definitionsAttributeName=UserId,AttributeType=S
AttributeName=GameTitle,AttributeType=S
AttributeName=TopScore,AttributeType=N
\ --key-schemaAttributeName=UserId,KeyType=HASH
\AttributeName=GameTitle,KeyType=RANGE
\ --provisioned-throughputReadCapacityUnits=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-definitionsAttributeName=UserId,AttributeType=S
AttributeName=GameTitle,AttributeType=S
AttributeName=TopScore,AttributeType=N
AttributeName=Date,AttributeType=S
\ --key-schemaAttributeName=UserId,KeyType=HASH
AttributeName=GameTitle,KeyType=RANGE
\ --provisioned-throughputReadCapacityUnits=10,WriteCapacityUnits=5
\ --global-secondary-indexesfile://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-definitionsAttributeName=UserId,AttributeType=S
AttributeName=GameTitle,AttributeType=S
\ --key-schemaAttributeName=UserId,KeyType=HASH
AttributeName=GameTitle,KeyType=RANGE
\ --provisioned-throughputReadCapacityUnits=10,WriteCapacityUnits=5
\ --stream-specificationStreamEnabled=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-definitionsAttributeName=UserId,AttributeType=S
AttributeName=GameTitle,AttributeType=S
\ --key-schemaAttributeName=UserId,KeyType=HASH
AttributeName=GameTitle,KeyType=RANGE
\ --provisioned-throughputReadCapacityUnits=10,WriteCapacityUnits=5
\ --stream-specificationStreamEnabled=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-definitionsAttributeName=UserId,AttributeType=S
AttributeName=GameTitle,AttributeType=S
\ --key-schemaAttributeName=UserId,KeyType=HASH
AttributeName=GameTitle,KeyType=RANGE
\ --provisioned-throughputReadCapacityUnits=10,WriteCapacityUnits=5
\ --table-classSTANDARD_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-definitionsAttributeName=UserId,AttributeType=S
AttributeName=GameTitle,AttributeType=S
\ --key-schemaAttributeName=UserId,KeyType=HASH
AttributeName=GameTitle,KeyType=RANGE
\ --provisioned-throughputReadCapacityUnits=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詳細資訊,請參閱 命令參考 CreateTable
中的 。 AWS CLI
-
下列程式碼範例示範如何使用 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詳細資訊,請參閱 命令參考 DeleteBackup
中的 。 AWS CLI
-
下列程式碼範例示範如何使用 delete-item
。
- AWS CLI
-
範例 1:刪除項目
下列
delete-item
範例會從MusicCollection
資料表中刪除項目,並請求有關已刪除項目的詳細資訊,以及請求使用的容量。aws dynamodb delete-item \ --table-name
MusicCollection
\ --keyfile://key.json
\ --return-valuesALL_OLD
\ --return-consumed-capacityTOTAL
\ --return-item-collection-metricsSIZE
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:有條件刪除項目
只有當項目
ProductCategory
為Sporting Goods
或Gardening 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-namesfile://names.json
\ --expression-attribute-valuesfile://values.json
\ --return-valuesALL_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詳細資訊,請參閱 命令參考 DeleteItem
中的 。 AWS CLI
-
下列程式碼範例示範如何使用 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詳細資訊,請參閱 命令參考 DeleteTable
中的 。 AWS CLI
-
下列程式碼範例示範如何使用 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詳細資訊,請參閱 命令參考 DescribeBackup
中的 。 AWS CLI
-
下列程式碼範例示範如何使用 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 Point-in-Time DynamoDB 開發人員指南 中的 DynamoDB 復原。 DynamoDB
-
如需API詳細資訊,請參閱 命令參考 DescribeContinuousBackups
中的 。 AWS CLI
-
下列程式碼範例示範如何使用 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-nameAlbumTitle-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
-
如需API詳細資訊,請參閱 命令參考 DescribeContributorInsights
中的 。 AWS CLI
-
下列程式碼範例示範如何使用 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詳細資訊,請參閱 命令參考 DescribeEndpoints
中的 。 AWS CLI
-
下列程式碼範例示範如何使用 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
-
如需API詳細資訊,請參閱 命令參考 DescribeGlobalTableSettings
中的 。 AWS CLI
-
下列程式碼範例示範如何使用 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
-
如需API詳細資訊,請參閱 命令參考 DescribeGlobalTable
中的 。 AWS CLI
-
下列程式碼範例示範如何使用 describe-limits
。
- AWS CLI
-
檢視佈建容量限制
下列
describe-limits
範例顯示目前 AWS 區域中您帳戶的佈建容量限制。aws dynamodb describe-limits
輸出:
{ "AccountMaxReadCapacityUnits": 80000, "AccountMaxWriteCapacityUnits": 80000, "TableMaxReadCapacityUnits": 40000, "TableMaxWriteCapacityUnits": 40000 }
如需詳細資訊,請參閱 Amazon DynamoDB 開發人員指南 中的 DynamoDB 中的限制。 DynamoDB
-
如需API詳細資訊,請參閱 命令參考 DescribeLimits
中的 。 AWS CLI
-
下列程式碼範例示範如何使用 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
-
如需API詳細資訊,請參閱 命令參考 DescribeTableReplicaAutoScaling
中的 。 AWS CLI
-
下列程式碼範例示範如何使用 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詳細資訊,請參閱 命令參考 DescribeTable
中的 。 AWS CLI
-
下列程式碼範例示範如何使用 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 開發人員指南 中的存留時間。
-
如需API詳細資訊,請參閱 命令參考 DescribeTimeToLive
中的 。 AWS CLI
-
下列程式碼範例示範如何使用 get-item
。
- AWS CLI
-
範例 1:讀取資料表中的項目
下列
get-item
範例會從MusicCollection
資料表擷取項目。資料表具有 hash-and-range主索引鍵 (Artist
和SongTitle
),因此您必須指定這兩個屬性。命令也會請求有關 操作所耗用讀取容量的資訊。aws dynamodb get-item \ --table-name
MusicCollection
\ --keyfile://key.json
\ --return-consumed-capacityTOTAL
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
\ --keyfile://key.json
\ --consistent-read \ --return-consumed-capacityTOTAL
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-namesfile://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詳細資訊,請參閱 命令參考 GetItem
中的 。 AWS CLI
-
下列程式碼範例示範如何使用 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-bound1577836800
\ --time-range-upper-bound1583020800
\ --backup-typeUSER
輸出:
{ "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詳細資訊,請參閱 命令參考 ListBackups
中的 。 AWS CLI
-
下列程式碼範例示範如何使用 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-tokenabCDeFGhiJKlmnOPqrSTuvwxYZ1aBCdEFghijK7LM51nOpqRSTuv3WxY3ZabC5dEFGhI2Jk3LmnoPQ6RST9
輸出:
{ "ContributorInsightsSummaries": [ { "TableName": "Reply", "ContributorInsightsStatus": "ENABLED" }, { "TableName": "Thread", "ContributorInsightsStatus": "ENABLED" } ] }
如需詳細資訊,請參閱 Amazon DynamoDB 開發人員指南 中的使用 CloudWatch Contributor Insights for DynamoDB 分析資料存取。 DynamoDB
-
如需API詳細資訊,請參閱 命令參考 ListContributorInsights
中的 。 AWS CLI
-
下列程式碼範例示範如何使用 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詳細資訊,請參閱 命令參考 ListGlobalTables
中的 。 AWS CLI
-
下列程式碼範例示範如何使用 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詳細資訊,請參閱 命令參考 ListTables
中的 。 AWS CLI
-
下列程式碼範例示範如何使用 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-items1
輸出:
{ "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-tokenabCDeFGhiJKlmnOPqrSTuvwxYZ1aBCdEFghijK7LM51nOpqRSTuv3WxY3ZabC5dEFGhI2Jk3LmnoPQ6RST9
輸出:
{ "Tags": [ { "Key": "Environment", "Value": "Production" } ] }
如需詳細資訊,請參閱 Amazon DynamoDB 開發人員指南 中的 DynamoDB 標記。 DynamoDB
-
如需API詳細資訊,請參閱 命令參考 ListTagsOfResource
中的 。 AWS CLI
-
下列程式碼範例示範如何使用 put-item
。
- AWS CLI
-
範例 1:將項目新增至資料表
下列
put-item
範例會將新項目新增至MusicCollection資料表。aws dynamodb put-item \ --table-name
MusicCollection
\ --itemfile://item.json
\ --return-consumed-capacityTOTAL
\ --return-item-collection-metricsSIZE
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
\ --itemfile://item.json
\ --condition-expression"#A = :A"
\ --expression-attribute-namesfile://names.json
\ --expression-attribute-valuesfile://values.json
\ --return-valuesALL_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詳細資訊,請參閱 命令參考 PutItem
中的 。 AWS CLI
-
下列程式碼範例示範如何使用 query
。
- AWS CLI
-
範例 1:查詢資料表
下列
query
範例會查詢MusicCollection
資料表中的項目。資料表具有 hash-and-range主索引鍵 (Artist
和SongTitle
),但此查詢只會指定雜湊索引鍵值。它傳回名為 "No One You Know" 的藝術家的歌曲標題。aws dynamodb query \ --table-name
MusicCollection
\ --projection-expression"SongTitle"
\ --key-condition-expression"Artist = :v1"
\ --expression-attribute-valuesfile://expression-attributes.json
\ --return-consumed-capacityTOTAL
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-valuesfile://expression-attributes.json
\ --consistent-read \ --no-scan-index-forward \ --return-consumed-capacityTOTAL
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
屬性中具有特定值的結果。請注意,這不會影響ScannedCount
或ConsumedCapacity
,因為篩選條件會在項目讀取後套用。aws dynamodb query \ --table-name
MusicCollection
\ --key-condition-expression"#n1 = :v1"
\ --filter-expression"NOT (#n2 IN (:v2, :v3))"
\ --expression-attribute-namesfile://names.json
\ --expression-attribute-valuesfile://values.json
\ --return-consumed-capacityTOTAL
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
\ --selectCOUNT
\ --key-condition-expression"Artist = :v1"
\ --expression-attribute-valuesfile://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-nameAlbumTitleIndex
\ --key-condition-expression"Artist = :v1"
\ --expression-attribute-valuesfile://expression-attributes.json
\ --selectALL_PROJECTED_ATTRIBUTES
\ --return-consumed-capacityINDEXES
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
-
如需API詳細資訊,請參閱 命令參考 RestoreTableFromBackup
中的 。 AWS CLI
-
下列程式碼範例示範如何使用 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-nameMusicCollectionRestore
\ --restore-date-time1576622404.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 Point-in-Time DynamoDB 開發人員指南 中的 DynamoDB 復原。 DynamoDB
-
如需API詳細資訊,請參閱 命令參考 RestoreTableToPointInTime
中的 。 AWS CLI
-
下列程式碼範例示範如何使用 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-namesfile://expression-attribute-names.json
\ --expression-attribute-valuesfile://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
\ --tagsKey=Owner,Value=blueTeam
此命令不會產生輸出。
如需詳細資訊,請參閱 Amazon DynamoDB 開發人員指南 中的 DynamoDB 標記。 DynamoDB
-
如需API詳細資訊,請參閱 命令參考 TagResource
中的 。 AWS CLI
-
下列程式碼範例示範如何使用 transact-get-items
。
- AWS CLI
-
從一或多個資料表以原子方式擷取多個項目
下列
transact-get-items
範例會以原子方式擷取多個項目。aws dynamodb transact-get-items \ --transact-items
file://transact-items.json
\ --return-consumed-capacityTOTAL
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詳細資訊,請參閱 命令參考 TransactGetItems
中的 。 AWS CLI
-
下列程式碼範例示範如何使用 transact-write-items
。
- AWS CLI
-
範例 1:以原子方式將項目寫入一或多個資料表
下列
transact-write-items
範例會更新一個項目並刪除另一個項目。如果任一操作失敗,或任一項目包含Rating
屬性,則操作會失敗。aws dynamodb transact-write-items \ --transact-items
file://transact-items.json
\ --return-consumed-capacityTOTAL
\ --return-item-collection-metricsSIZE
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-tokenabc123
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
-
如需API詳細資訊,請參閱 命令參考 TransactWriteItems
中的 。 AWS CLI
-
下列程式碼範例示範如何使用 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-keysOwner
此命令不會產生輸出。
如需詳細資訊,請參閱 Amazon DynamoDB 開發人員指南 中的 DynamoDB 標記。 DynamoDB
-
如需API詳細資訊,請參閱 命令參考 UntagResource
中的 。 AWS CLI
-
下列程式碼範例示範如何使用 update-continuous-backups
。
- AWS CLI
-
更新 DynamoDB 資料表的連續備份設定
下列
update-continuous-backups
範例可 point-in-time復原MusicCollection
資料表。aws dynamodb update-continuous-backups \ --table-name
MusicCollection
\ --point-in-time-recovery-specificationPointInTimeRecoveryEnabled=true
輸出:
{ "ContinuousBackupsDescription": { "ContinuousBackupsStatus": "ENABLED", "PointInTimeRecoveryDescription": { "PointInTimeRecoveryStatus": "ENABLED", "EarliestRestorableDateTime": 1576622404.0, "LatestRestorableDateTime": 1576622404.0 } } }
如需詳細資訊,請參閱 Amazon Point-in-Time DynamoDB 開發人員指南 中的 DynamoDB 復原。 DynamoDB
-
如需API詳細資訊,請參閱 命令參考 UpdateContinuousBackups
中的 。 AWS CLI
-
下列程式碼範例示範如何使用 update-contributor-insights
。
- AWS CLI
-
在資料表上啟用貢獻者洞察
下列
update-contributor-insights
範例會在MusicCollection
資料表和AlbumTitle-index
全域次要索引上啟用貢獻者洞察。aws dynamodb update-contributor-insights \ --table-name
MusicCollection
\ --index-nameAlbumTitle-index
\ --contributor-insights-actionENABLE
輸出:
{ "TableName": "MusicCollection", "IndexName": "AlbumTitle-index", "ContributorInsightsStatus": "ENABLING" }
如需詳細資訊,請參閱 Amazon DynamoDB 開發人員指南 中的使用 CloudWatch Contributor Insights for DynamoDB 分析資料存取。 DynamoDB
-
如需API詳細資訊,請參閱 命令參考 UpdateContributorInsights
中的 。 AWS CLI
-
下列程式碼範例示範如何使用 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-units15
輸出:
{ "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
-
如需API詳細資訊,請參閱 命令參考 UpdateGlobalTableSettings
中的 。 AWS CLI
-
下列程式碼範例示範如何使用 update-global-table
。
- AWS CLI
-
若要更新 DynamoDB 全域資料表
下列
update-global-table
範例會將指定區域中的複本新增至MusicCollection
全域資料表。aws dynamodb update-global-table \ --global-table-name
MusicCollection
\ --replica-updatesCreate={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詳細資訊,請參閱 命令參考 UpdateGlobalTable
中的 。 AWS CLI
-
下列程式碼範例示範如何使用 update-item
。
- AWS CLI
-
範例 1:更新資料表中的項目
下列
update-item
範例會更新MusicCollection
資料表中的項目。它會新增屬性 (Year
) 並修改AlbumTitle
屬性。項目中的所有屬性,如更新後顯示,都會傳回回應中。aws dynamodb update-item \ --table-name
MusicCollection
\ --keyfile://key.json
\ --update-expression"SET #Y = :y, #AT = :t"
\ --expression-attribute-namesfile://expression-attribute-names.json
\ --expression-attribute-valuesfile://expression-attribute-values.json
\ --return-valuesALL_NEW
\ --return-consumed-capacityTOTAL
\ --return-item-collection-metricsSIZE
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
\ --keyfile://key.json
\ --update-expression"SET #Y = :y, #AT = :t"
\ --expression-attribute-namesfile://expression-attribute-names.json
\ --expression-attribute-valuesfile://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詳細資訊,請參閱 命令參考 UpdateItem
中的 。 AWS CLI
-
下列程式碼範例示範如何使用 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-updatefile://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
-
如需API詳細資訊,請參閱 命令參考 UpdateTableReplicaAutoScaling
中的 。 AWS CLI
-
下列程式碼範例示範如何使用 update-table
。
- AWS CLI
-
範例 1:修改資料表的計費模式
下列
update-table
範例會增加MusicCollection
資料表上的佈建讀取和寫入容量。aws dynamodb update-table \ --table-name
MusicCollection
\ --billing-modePROVISIONED
\ --provisioned-throughputReadCapacityUnits=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-definitionsAttributeName=AlbumTitle,AttributeType=S
\ --global-secondary-index-updatesfile://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-specificationStreamEnabled=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-specificationEnabled=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詳細資訊,請參閱 命令參考 UpdateTable
中的 。 AWS CLI
-
下列程式碼範例示範如何使用 update-time-to-live
。
- AWS CLI
-
更新資料表上的存留時間設定
下列
update-time-to-live
範例會啟用指定資料表上的存留時間。aws dynamodb update-time-to-live \ --table-name
MusicCollection
\ --time-to-live-specificationEnabled=true,AttributeName=ttl
輸出:
{ "TimeToLiveSpecification": { "Enabled": true, "AttributeName": "ttl" } }
如需詳細資訊,請參閱 Amazon DynamoDB 開發人員指南 中的存留時間。
-
如需API詳細資訊,請參閱 命令參考 UpdateTimeToLive
中的 。 AWS CLI
-