でのテーブルの操作 DynamoDB - AWS SDK for Java 2.x

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

でのテーブルの操作 DynamoDB

テーブルは、 DynamoDB データベース内のすべての項目のコンテナです。からデータを追加または削除する前に DynamoDB、テーブルを作成する必要があります。

テーブルごとに、以下を定義する必要があります。

  • アカウントとリージョンに固有のテーブル

  • プライマリキー。すべての値は一意でなければならず、テーブル内のどの 2 つの項目も同じプライマリキー値を持つことはできません。

    プライマリキーは、単一のパーティション (ハッシュ) キーで構成されるシンプルなプライマリキーにするか、パーティションとソート (範囲) キーで構成される複合プライマリキーにすることができます。

    各キー値には、 ScalarAttributeType クラスによって列挙されたデータ型 が関連付けられています。キー値は、バイナリ (B)、数値 (N)、または文字列 (S) になります。詳細については、「 Amazon DynamoDB デベロッパーガイド」の「命名規則とデータ型」を参照してください。

  • テーブル用に予約された読み込み/書き込みキャパシティーユニットの数を定義するプロビジョンドスループットの値。

    注記

    Amazon DynamoDB 料金はテーブルに設定したプロビジョニングされたスループット値に基づいているため、テーブルに必要な容量だけを予約してください。

    テーブルのプロビジョンドスループットはいつでも変更できるため、必要に応じてキャパシティーを調整できます。

テーブルを作成する

DynamoDbClient’s createTable メソッドを使用して新しい DynamoDB テーブルを作成します。テーブルのプライマリキーを識別するために使用する、テーブル属性とテーブルスキーマを構築する必要があります。また、最初のプロビジョニングされたスループット値およびテーブル名を指定する必要があります。

注記

選択した名前のテーブルが既に存在する場合は、 DynamoDbException がスローされます。

シンプルプライマリキーを使用してテーブルを作成する

このコードは、テーブルのシンプルなプライマリキーである属性を 1 つ持つテーブルを作成します。この例では、 に AttributeDefinitionおよび KeySchemaElement オブジェクトを使用していますCreateTableRequest

インポート

import software.amazon.awssdk.core.waiters.WaiterResponse; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.dynamodb.model.CreateTableRequest; import software.amazon.awssdk.services.dynamodb.model.AttributeDefinition; import software.amazon.awssdk.services.dynamodb.model.ScalarAttributeType; import software.amazon.awssdk.services.dynamodb.model.ScalarAttributeType; import software.amazon.awssdk.services.dynamodb.model.KeySchemaElement; import software.amazon.awssdk.services.dynamodb.model.ProvisionedThroughput; import software.amazon.awssdk.services.dynamodb.model.KeyType; import software.amazon.awssdk.services.dynamodb.model.CreateTableResponse; import software.amazon.awssdk.services.dynamodb.model.DescribeTableRequest; import software.amazon.awssdk.services.dynamodb.model.DescribeTableResponse; import software.amazon.awssdk.services.dynamodb.model.DynamoDbException; import software.amazon.awssdk.services.dynamodb.DynamoDbClient; import software.amazon.awssdk.services.dynamodb.waiters.DynamoDbWaiter;

Code

public static String createTable(DynamoDbClient ddb, String tableName, String key) { DynamoDbWaiter dbWaiter = ddb.waiter(); CreateTableRequest request = CreateTableRequest.builder() .attributeDefinitions(AttributeDefinition.builder() .attributeName(key) .attributeType(ScalarAttributeType.S) .build()) .keySchema(KeySchemaElement.builder() .attributeName(key) .keyType(KeyType.HASH) .build()) .provisionedThroughput(ProvisionedThroughput.builder() .readCapacityUnits(new Long(10)) .writeCapacityUnits(new Long(10)) .build()) .tableName(tableName) .build(); String newTable =""; try { CreateTableResponse response = ddb.createTable(request); DescribeTableRequest tableRequest = DescribeTableRequest.builder() .tableName(tableName) .build(); // Wait until the Amazon DynamoDB table is created WaiterResponse<DescribeTableResponse> waiterResponse = dbWaiter.waitUntilTableExists(tableRequest); waiterResponse.matched().response().ifPresent(System.out::println); newTable = response.tableDescription().tableName(); return newTable; } catch (DynamoDbException e) { System.err.println(e.getMessage()); System.exit(1); } return ""; }

「」で完全な例を参照してください GitHub。

複合プライマリキーを使用してテーブルを作成する

次の例では、2 つの属性を持つテーブルを作成します。どちらの属性も複合プライマリキーに使用されます。

インポート

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.dynamodb.model.DynamoDbException; import software.amazon.awssdk.services.dynamodb.model.AttributeDefinition; import software.amazon.awssdk.services.dynamodb.model.CreateTableRequest; import software.amazon.awssdk.services.dynamodb.model.CreateTableResponse; import software.amazon.awssdk.services.dynamodb.model.KeySchemaElement; import software.amazon.awssdk.services.dynamodb.model.KeyType; import software.amazon.awssdk.services.dynamodb.model.ProvisionedThroughput; import software.amazon.awssdk.services.dynamodb.model.ScalarAttributeType; import software.amazon.awssdk.services.dynamodb.DynamoDbClient;

Code

public static String createTableComKey(DynamoDbClient ddb, String tableName) { CreateTableRequest request = CreateTableRequest.builder() .attributeDefinitions( AttributeDefinition.builder() .attributeName("Language") .attributeType(ScalarAttributeType.S) .build(), AttributeDefinition.builder() .attributeName("Greeting") .attributeType(ScalarAttributeType.S) .build()) .keySchema( KeySchemaElement.builder() .attributeName("Language") .keyType(KeyType.HASH) .build(), KeySchemaElement.builder() .attributeName("Greeting") .keyType(KeyType.RANGE) .build()) .provisionedThroughput( ProvisionedThroughput.builder() .readCapacityUnits(new Long(10)) .writeCapacityUnits(new Long(10)).build()) .tableName(tableName) .build(); String tableId = ""; try { CreateTableResponse result = ddb.createTable(request); tableId = result.tableDescription().tableId(); return tableId; } catch (DynamoDbException e) { System.err.println(e.getMessage()); System.exit(1); } return ""; }

「」で完全な例を参照してください GitHub。

テーブルの一覧表示

特定のリージョンのテーブルを一覧表示するには、 DynamoDbClient’slistTablesメソッドを呼び出します。

注記

アカウントとリージョンの名前付きテーブルが存在しない場合は、 ResourceNotFoundException がスローされます。

インポート

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.dynamodb.model.DynamoDbException; import software.amazon.awssdk.services.dynamodb.model.ListTablesResponse; import software.amazon.awssdk.services.dynamodb.model.ListTablesRequest; import software.amazon.awssdk.services.dynamodb.DynamoDbClient; import java.util.List;

Code

public static void listAllTables(DynamoDbClient ddb){ boolean moreTables = true; String lastName = null; while(moreTables) { try { ListTablesResponse response = null; if (lastName == null) { ListTablesRequest request = ListTablesRequest.builder().build(); response = ddb.listTables(request); } else { ListTablesRequest request = ListTablesRequest.builder() .exclusiveStartTableName(lastName).build(); response = ddb.listTables(request); } List<String> tableNames = response.tableNames(); if (tableNames.size() > 0) { for (String curName : tableNames) { System.out.format("* %s\n", curName); } } else { System.out.println("No tables found!"); System.exit(0); } lastName = response.lastEvaluatedTableName(); if (lastName == null) { moreTables = false; } } catch (DynamoDbException e) { System.err.println(e.getMessage()); System.exit(1); } } System.out.println("\nDone!"); }

デフォルトでは、呼び出しごとに最大 100 個のテーブルが返されます。評価された最後のテーブルを取得するには、返されたListTablesResponseオブジェクトlastEvaluatedTableNameで を使用します。この値を使用して、前回の一覧表示で返された最後の値以降から、一覧表示を開始できます。

「」で完全な例を参照してください GitHub。

テーブルの説明 (テーブルに関する情報の取得)

DynamoDbClient’s describeTable メソッドを使用して、テーブルに関する情報を取得します。

注記

アカウントとリージョンの名前付きテーブルが存在しない場合は、 ResourceNotFoundException がスローされます。

インポート

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.dynamodb.model.DynamoDbException; import software.amazon.awssdk.services.dynamodb.DynamoDbClient; import software.amazon.awssdk.services.dynamodb.model.AttributeDefinition; import software.amazon.awssdk.services.dynamodb.model.DescribeTableRequest; import software.amazon.awssdk.services.dynamodb.model.ProvisionedThroughputDescription; import software.amazon.awssdk.services.dynamodb.model.TableDescription; import java.util.List;

Code

public static void describeDymamoDBTable(DynamoDbClient ddb,String tableName ) { DescribeTableRequest request = DescribeTableRequest.builder() .tableName(tableName) .build(); try { TableDescription tableInfo = ddb.describeTable(request).table(); if (tableInfo != null) { System.out.format("Table name : %s\n", tableInfo.tableName()); System.out.format("Table ARN : %s\n", tableInfo.tableArn()); System.out.format("Status : %s\n", tableInfo.tableStatus()); System.out.format("Item count : %d\n", tableInfo.itemCount().longValue()); System.out.format("Size (bytes): %d\n", tableInfo.tableSizeBytes().longValue()); ProvisionedThroughputDescription throughputInfo = tableInfo.provisionedThroughput(); System.out.println("Throughput"); System.out.format(" Read Capacity : %d\n", throughputInfo.readCapacityUnits().longValue()); System.out.format(" Write Capacity: %d\n", throughputInfo.writeCapacityUnits().longValue()); List<AttributeDefinition> attributes = tableInfo.attributeDefinitions(); System.out.println("Attributes"); for (AttributeDefinition a : attributes) { System.out.format(" %s (%s)\n", a.attributeName(), a.attributeType()); } } } catch (DynamoDbException e) { System.err.println(e.getMessage()); System.exit(1); } System.out.println("\nDone!"); }

「」で完全な例を参照してください GitHub。

テーブルの変更 (更新)

テーブルのプロビジョンドスループット値は、DynamoDbClient’s updateTable メソッドを呼び出すことで随時変更できます。

注記

アカウントとリージョンの名前付きテーブルが存在しない場合は、 ResourceNotFoundException がスローされます。

インポート

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.dynamodb.model.ProvisionedThroughput; import software.amazon.awssdk.services.dynamodb.DynamoDbClient; import software.amazon.awssdk.services.dynamodb.model.UpdateTableRequest; import software.amazon.awssdk.services.dynamodb.model.DynamoDbException;

Code

public static void updateDynamoDBTable(DynamoDbClient ddb, String tableName, Long readCapacity, Long writeCapacity) { System.out.format( "Updating %s with new provisioned throughput values\n", tableName); System.out.format("Read capacity : %d\n", readCapacity); System.out.format("Write capacity : %d\n", writeCapacity); ProvisionedThroughput tableThroughput = ProvisionedThroughput.builder() .readCapacityUnits(readCapacity) .writeCapacityUnits(writeCapacity) .build(); UpdateTableRequest request = UpdateTableRequest.builder() .provisionedThroughput(tableThroughput) .tableName(tableName) .build(); try { ddb.updateTable(request); } catch (DynamoDbException e) { System.err.println(e.getMessage()); System.exit(1); } System.out.println("Done!"); }

「」で完全な例を参照してください GitHub。

テーブルを削除する

テーブルを削除するには、DynamoDbClient’s deleteTable メソッドを呼び出してテーブルの名前を指定します。

注記

アカウントとリージョンの名前付きテーブルが存在しない場合は、 ResourceNotFoundException がスローされます。

インポート

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.dynamodb.model.DynamoDbException; import software.amazon.awssdk.services.dynamodb.DynamoDbClient; import software.amazon.awssdk.services.dynamodb.model.DeleteTableRequest;

Code

public static void deleteDynamoDBTable(DynamoDbClient ddb, String tableName) { DeleteTableRequest request = DeleteTableRequest.builder() .tableName(tableName) .build(); try { ddb.deleteTable(request); } catch (DynamoDbException e) { System.err.println(e.getMessage()); System.exit(1); } System.out.println(tableName +" was successfully deleted!"); }

「」で完全な例を参照してください GitHub。

詳細情報