次のコード例は、ウォームスループットを有効にしたテーブルを作成する方法を示しています。
- SDK for Java 2.x
-
ウォームスループット設定で DynamoDB テーブルを作成します。
import software.amazon.awssdk.services.dynamodb.DynamoDbClient; 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.GlobalSecondaryIndex; import software.amazon.awssdk.services.dynamodb.model.KeySchemaElement; import software.amazon.awssdk.services.dynamodb.model.KeyType; import software.amazon.awssdk.services.dynamodb.model.Projection; import software.amazon.awssdk.services.dynamodb.model.ProjectionType; import software.amazon.awssdk.services.dynamodb.model.ProvisionedThroughput; import software.amazon.awssdk.services.dynamodb.model.ScalarAttributeType; import software.amazon.awssdk.services.dynamodb.model.WarmThroughput; public static WarmThroughput buildWarmThroughput(final Long readUnitsPerSecond, final Long writeUnitsPerSecond) { return WarmThroughput.builder() .readUnitsPerSecond(readUnitsPerSecond) .writeUnitsPerSecond(writeUnitsPerSecond) .build(); } public static ProvisionedThroughput buildProvisionedThroughput(final Long readCapacityUnits, final Long writeCapacityUnits) { return ProvisionedThroughput.builder() .readCapacityUnits(readCapacityUnits) .writeCapacityUnits(writeCapacityUnits) .build(); } private static AttributeDefinition buildAttributeDefinition(final String attributeName, final ScalarAttributeType scalarAttributeType) { return AttributeDefinition.builder() .attributeName(attributeName) .attributeType(scalarAttributeType) .build(); } private static KeySchemaElement buildKeySchemaElement(final String attributeName, final KeyType keyType) { return KeySchemaElement.builder() .attributeName(attributeName) .keyType(keyType) .build(); } public static void createDynamoDBTable(DynamoDbClient ddb, String tableName, String partitionKey, String sortKey, String miscellaneousKeyAttribute, String nonKeyAttribute, Long tableReadCapacityUnits, Long tableWriteCapacityUnits, Long tableWarmReadUnitsPerSecond, Long tableWarmWriteUnitsPerSecond, String globalSecondaryIndexName, Long globalSecondaryIndexReadCapacityUnits, Long globalSecondaryIndexWriteCapacityUnits, Long globalSecondaryIndexWarmReadUnitsPerSecond, Long globalSecondaryIndexWarmWriteUnitsPerSecond) { // Define the table attributes final AttributeDefinition partitionKeyAttribute = buildAttributeDefinition(partitionKey, ScalarAttributeType.S); final AttributeDefinition sortKeyAttribute = buildAttributeDefinition(sortKey, ScalarAttributeType.S); final AttributeDefinition miscellaneousKeyAttributeDefinition = buildAttributeDefinition(miscellaneousKeyAttribute, ScalarAttributeType.N); final AttributeDefinition[] attributeDefinitions = {partitionKeyAttribute, sortKeyAttribute, miscellaneousKeyAttributeDefinition}; // Define the table key schema final KeySchemaElement partitionKeyElement = buildKeySchemaElement(partitionKey, KeyType.HASH); final KeySchemaElement sortKeyElement = buildKeySchemaElement(sortKey, KeyType.RANGE); final KeySchemaElement[] keySchema = {partitionKeyElement, sortKeyElement}; // Define the provisioned throughput for the table final ProvisionedThroughput provisionedThroughput = buildProvisionedThroughput(tableReadCapacityUnits, tableWriteCapacityUnits); // Define the Global Secondary Index (GSI) final KeySchemaElement globalSecondaryIndexPartitionKeyElement = buildKeySchemaElement(sortKey, KeyType.HASH); final KeySchemaElement globalSecondaryIndexSortKeyElement = buildKeySchemaElement(miscellaneousKeyAttribute, KeyType.RANGE); final KeySchemaElement[] gsiKeySchema = {globalSecondaryIndexPartitionKeyElement, globalSecondaryIndexSortKeyElement}; final Projection gsiProjection = Projection.builder() .projectionType(String.valueOf(ProjectionType.INCLUDE)) .nonKeyAttributes(nonKeyAttribute) .build(); final ProvisionedThroughput gsiProvisionedThroughput = buildProvisionedThroughput(globalSecondaryIndexReadCapacityUnits, globalSecondaryIndexWriteCapacityUnits); // Define the warm throughput for the Global Secondary Index (GSI) final WarmThroughput gsiWarmThroughput = buildWarmThroughput(globalSecondaryIndexWarmReadUnitsPerSecond, globalSecondaryIndexWarmWriteUnitsPerSecond); final GlobalSecondaryIndex globalSecondaryIndex = GlobalSecondaryIndex.builder() .indexName(globalSecondaryIndexName) .keySchema(gsiKeySchema) .projection(gsiProjection) .provisionedThroughput(gsiProvisionedThroughput) .warmThroughput(gsiWarmThroughput) .build(); // Define the warm throughput for the table final WarmThroughput tableWarmThroughput = buildWarmThroughput(tableWarmReadUnitsPerSecond, tableWarmWriteUnitsPerSecond); final CreateTableRequest request = CreateTableRequest.builder() .tableName(tableName) .attributeDefinitions(attributeDefinitions) .keySchema(keySchema) .provisionedThroughput(provisionedThroughput) .globalSecondaryIndexes(globalSecondaryIndex) .warmThroughput(tableWarmThroughput) .build(); CreateTableResponse response = ddb.createTable(request); System.out.println(response); }
-
API の詳細については、「AWS SDK for Java 2.x API リファレンス」の「CreateTable」を参照してください。
-
AWS SDK デベロッパーガイドとコード例の詳細なリストについては、「AWS SDK で DynamoDB を使用する」を参照してください。このトピックには、使用開始方法に関する情報と、以前の SDK バージョンの詳細も含まれています。