チュートリアル: グローバルテーブルの作成 - Amazon DynamoDB

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

チュートリアル: グローバルテーブルの作成

このセクションでは、Amazon DynamoDB コンソールまたは AWS Command Line Interface (AWS CLI) を使用してグローバルテーブルを作成する方法について説明します。

グローバルテーブルの作成 (コンソール)

コンソールを使用してグローバルテーブルを作成するには、次の手順に従います。以下の例では、レプリカテーブルを持つグローバルテーブルを米国およびヨーロッパに作成します。

  1. DynamoDB コンソール (https://console.aws.amazon.com/dynamodb/home) を開きます。この例では、米国東部 (オハイオ) リージョンを選択します。

  2. コンソールの左側のナビゲーションペインで、[Tables] (テーブル) を選択します。

  3. [テーブルの作成] を選択します。

    1. [テーブル名] に「Music」と入力します。

    2. [パーティションキー] に「Artist」と入力します。[ソートキー] に「SongTitle」と入力します (ArtistSongTitle はいずれも文字列)。

    テーブルを作成するには、[テーブルを作成] を選択します。このテーブルは、新しいグローバルテーブルの最初のレプリカテーブルとして機能します。これは、後で追加する他のレプリカテーブルのプロトタイプです。

  4. [グローバルテーブル] タブを選択後、[レプリカを作成] を選択します。

  5. [利用できるレプリケーションリージョン] ドロップダウンで、米国西部 (オレゴン) を選択します。

    コンソールは、選択したリージョンに同一名のテーブルが存在しないことを確認します。同一名のテーブルが存在する場合は、そのリージョンで新しいレプリカテーブルを作成する前に既存のテーブルを削除する必要があります。

  6. [レプリカを作成] を選択します。これにより、米国西部 (オレゴン) でテーブル作成プロセスが開始されます。

    選択したテーブル (およびその他すべてのレプリカテーブル) の [グローバルテーブル] タブに、そのテーブルが複数のリージョンでレプリケートされたことが示されます。

  7. ここで、別のリージョンを追加して、グローバルテーブルがレプリケートされ、米国およびヨーロッパに同期されるようにします。これを行うには、ステップ 5 を繰り返しますが、今回は米国西部 (オレゴン) の代わりに欧州 (フランクフルト) を指定します。

  8. 引き続き、米国東部 (オハイオ)リージョンで AWS Management Console を使用する必要があります。左のナビゲーションメニューで [項目] を選択し、[音楽] テーブルを選択してから、[項目を作成] を選択します。

    1. [Artist] に「item_1」と入力します。

    2. SongTitle に「Song Value 1」と入力します。

    3. 項目を書き込むには、[項目を作成] を選択します。

  9. 間もなくすると、この項目は、グローバルテーブルの 3 つのすべてのリージョンにレプリケートされます。これを確認するには、コンソールの右上隅にあるリージョンセレクターで、[Europe (Frankfurt) (欧州 (フランクフルト))] を選択します。欧州 (フランクフルト) の Music テーブルに新しい項目が追加されます。

  10. ステップ 9 を繰り返し、米国西部 (オレゴン) を選択して、そのリージョンでのレプリケーションを確認します。

グローバルテーブル (AWS CLI) の作成

Music を使用してグローバルテーブル AWS CLI を作成するには、次の手順に従います。以下の例では、米国およびヨーロッパのレプリカテーブルを使用して、グローバルテーブルを作成します。

  1. DynamoDB Streams を有効にして (Music)、米国東部 (オハイオ) で新規テーブル (NEW_AND_OLD_IMAGES) DynamoDB Streams を作成します。

    aws dynamodb create-table \ --table-name Music \ --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 \ --stream-specification StreamEnabled=true,StreamViewType=NEW_AND_OLD_IMAGES \ --region us-east-2
  2. 米国東部 (バージニア北部) に同じ Music テーブルを作成します。

    aws dynamodb update-table --table-name Music --cli-input-json \ '{ "ReplicaUpdates": [ { "Create": { "RegionName": "us-east-1" } } ] }' \ --region=us-east-2
  3. ステップ 2 を繰り返して、欧州 (アイルランド) (eu-west-1) にテーブルを作成します。

  4. describe-table を使用して、作成されたレプリカのリストを表示できます。

    aws dynamodb describe-table --table-name Music --region us-east-2
  5. レプリケーションが機能していることを確認するには、新しい項目を米国東部 (オハイオ) の Music テーブルに追加します。

    aws dynamodb put-item \ --table-name Music \ --item '{"Artist": {"S":"item_1"},"SongTitle": {"S":"Song Value 1"}}' \ --region us-east-2
  6. 数秒間待ってから、項目が米国東部 (バージニア北部) および欧州 (アイルランド) に正常にレプリケートされたかどうかを確認します。

    aws dynamodb get-item \ --table-name Music \ --key '{"Artist": {"S":"item_1"},"SongTitle": {"S":"Song Value 1"}}' \ --region us-east-1
    aws dynamodb get-item \ --table-name Music \ --key '{"Artist": {"S":"item_1"},"SongTitle": {"S":"Song Value 1"}}' \ --region eu-west-1
  7. 欧州 (アイルランド) リージョンに作成されたレプリカテーブルを削除します。

    aws dynamodb update-table --table-name Music --cli-input-json \ '{ "ReplicaUpdates": [ { "Delete": { "RegionName": "eu-west-1" } } ] }'

グローバルテーブル (Java) の作成

次の Java コード例は、欧州 (アイルランド) リージョンに Music テーブルを作成し、アジアパシフィック (ソウル) リージョンにレプリカを作成します。

package com.amazonaws.codesamples.gtv2 import java.util.logging.Logger; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import com.amazonaws.regions.Regions; import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder; import com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException; import com.amazonaws.services.dynamodbv2.model.AttributeDefinition; import com.amazonaws.services.dynamodbv2.model.BillingMode; import com.amazonaws.services.dynamodbv2.model.CreateReplicationGroupMemberAction; import com.amazonaws.services.dynamodbv2.model.CreateTableRequest; import com.amazonaws.services.dynamodbv2.model.DescribeTableRequest; import com.amazonaws.services.dynamodbv2.model.GlobalSecondaryIndex; import com.amazonaws.services.dynamodbv2.model.KeySchemaElement; import com.amazonaws.services.dynamodbv2.model.KeyType; import com.amazonaws.services.dynamodbv2.model.Projection; import com.amazonaws.services.dynamodbv2.model.ProjectionType; import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput; import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughputOverride; import com.amazonaws.services.dynamodbv2.model.ReplicaGlobalSecondaryIndex; import com.amazonaws.services.dynamodbv2.model.ReplicationGroupUpdate; import com.amazonaws.services.dynamodbv2.model.ScalarAttributeType; import com.amazonaws.services.dynamodbv2.model.StreamSpecification; import com.amazonaws.services.dynamodbv2.model.StreamViewType; import com.amazonaws.services.dynamodbv2.model.UpdateTableRequest; import com.amazonaws.waiters.WaiterParameters; public class App { private final static Logger LOGGER = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME); public static void main( String[] args ) { String tableName = "Music"; String indexName = "index1"; Regions calledRegion = Regions.EU_WEST_1; Regions destRegion = Regions.AP_NORTHEAST_2; AmazonDynamoDB ddbClient = AmazonDynamoDBClientBuilder.standard() .withCredentials(new ProfileCredentialsProvider("default")) .withRegion(calledRegion) .build(); LOGGER.info("Creating a regional table - TableName: " + tableName +", IndexName: " + indexName + " ....."); ddbClient.createTable(new CreateTableRequest() .withTableName(tableName) .withAttributeDefinitions( new AttributeDefinition() .withAttributeName("Artist").withAttributeType(ScalarAttributeType.S), new AttributeDefinition() .withAttributeName("SongTitle").withAttributeType(ScalarAttributeType.S)) .withKeySchema( new KeySchemaElement().withAttributeName("Artist").withKeyType(KeyType.HASH), new KeySchemaElement().withAttributeName("SongTitle").withKeyType(KeyType.RANGE)) .withBillingMode(BillingMode.PAY_PER_REQUEST) .withGlobalSecondaryIndexes(new GlobalSecondaryIndex() .withIndexName(indexName) .withKeySchema(new KeySchemaElement() .withAttributeName("SongTitle") .withKeyType(KeyType.HASH)) .withProjection(new Projection().withProjectionType(ProjectionType.ALL))) .withStreamSpecification(new StreamSpecification() .withStreamEnabled(true) .withStreamViewType(StreamViewType.NEW_AND_OLD_IMAGES))); LOGGER.info("Waiting for ACTIVE table status ....."); ddbClient.waiters().tableExists().run(new WaiterParameters<>(new DescribeTableRequest(tableName))); LOGGER.info("Testing parameters for adding a new Replica in " + destRegion + " ....."); CreateReplicationGroupMemberAction createReplicaAction = new CreateReplicationGroupMemberAction() .withRegionName(destRegion.getName()) .withGlobalSecondaryIndexes(new ReplicaGlobalSecondaryIndex() .withIndexName(indexName) .withProvisionedThroughputOverride(new ProvisionedThroughputOverride() .withReadCapacityUnits(15L))); ddbClient.updateTable(new UpdateTableRequest() .withTableName(tableName) .withReplicaUpdates(new ReplicationGroupUpdate() .withCreate(createReplicaAction.withKMSMasterKeyId(null)))); } }