使用 AWS Command Line Interface v2 處理 DynamoDB 全域資料表和多區域複寫 - Amazon DynamoDB

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

使用 AWS Command Line Interface v2 處理 DynamoDB 全域資料表和多區域複寫

下列程式碼範例示範如何使用多區域複寫來管理 DynamoDB 全域資料表。

  • 建立具有多區域複寫的資料表。

  • 放置並從複本資料表取得項目。

  • 移除複本。

Bash
AWS CLI 搭配 Bash 指令碼

建立具有多區域複寫的資料表。

# Step 1: Create a new table in us-west-2 aws dynamodb create-table \ --table-name MusicTable \ --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 \ --region us-west-2 # Step 2: Create replicas in us-east-1 and us-east-2 aws dynamodb update-table \ --table-name MusicTable \ --replica-updates '[{"Create": {"RegionName": "us-east-1"}}, {"Create": {"RegionName": "us-east-2"}}]' \ --multi-region-consistency STRONG \ --region us-west-2

描述多區域資料表。

# Describe the base table aws dynamodb describe-table --table-name MusicTable --region us-west-2

將項目放在複本資料表中。

# Write a single item to one of the replica tables. aws dynamodb put-item \ --table-name MusicTable \ --item '{"Artist": {"S":"item_1"},"SongTitle": {"S":"Song Value 1"}}' \ --region us-east-2

從複本資料表取得項目。

# Get item from the other two replicas aws dynamodb get-item \ --table-name MusicTable \ --key '{"Artist": {"S":"item_1"},"SongTitle": {"S":"Song Value 1"}}' \ --consistent-read \ --region us-east-1 aws dynamodb get-item \ --table-name MusicTable \ --key '{"Artist": {"S":"item_1"},"SongTitle": {"S":"Song Value 1"}}' \ --consistent-read \ --region us-west-2

移除複本。

# Remove the replica tables. aws dynamodb update-table \ --table-name MusicTable \ --replica-updates '[{"Delete": {"RegionName": "us-east-2"}}, {"Delete": {"RegionName": "us-east-1"}}]' \ --region us-west-2

如需 AWS SDK 開發人員指南和程式碼範例的完整清單,請參閱 搭配 AWS SDK 使用 DynamoDB 。此主題也包含有關入門的資訊和舊版 SDK 的詳細資訊。