There are more AWS SDK examples available in the AWS Doc SDK Examples
Create a DynamoDB table with warm throughput setting using an AWS SDK
The following code examples show how to create a table with warm throughput enabled.
- SDK for Java 2.x
-
Create DynamoDB table with warm throughput setting.
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); }
-
For API details, see CreateTable in AWS SDK for Java 2.x API Reference.
-