Search in sources :

Example 1 with CreateTableRequest

use of software.amazon.awssdk.services.dynamodb.model.CreateTableRequest in project aws-doc-sdk-examples by awsdocs.

the class CreateTableCompositeKey method createTableComKey.

// snippet-start:[dynamodb.java2.create_table_composite_key.main]
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 "";
}
Also used : DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) CreateTableResponse(software.amazon.awssdk.services.dynamodb.model.CreateTableResponse) CreateTableRequest(software.amazon.awssdk.services.dynamodb.model.CreateTableRequest)

Example 2 with CreateTableRequest

use of software.amazon.awssdk.services.dynamodb.model.CreateTableRequest in project aws-doc-sdk-examples by awsdocs.

the class CreateTable method createTable.

// snippet-start:[dynamodb.java2.create_table.main]
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 "";
}
Also used : DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) CreateTableResponse(software.amazon.awssdk.services.dynamodb.model.CreateTableResponse) DescribeTableResponse(software.amazon.awssdk.services.dynamodb.model.DescribeTableResponse) DescribeTableRequest(software.amazon.awssdk.services.dynamodb.model.DescribeTableRequest) DynamoDbWaiter(software.amazon.awssdk.services.dynamodb.waiters.DynamoDbWaiter) CreateTableRequest(software.amazon.awssdk.services.dynamodb.model.CreateTableRequest)

Example 3 with CreateTableRequest

use of software.amazon.awssdk.services.dynamodb.model.CreateTableRequest in project aws-doc-sdk-examples by awsdocs.

the class DynamoDBAsyncCreateTable method createTable.

// snippet-start:[dynamodb.java2.dbasync.table.main]
public static void createTable(DynamoDbAsyncClient client, String tableName, String key) {
    // Create a DynamoDbAsyncWaiter object
    DynamoDbAsyncWaiter asyncWaiter = client.waiter();
    // Create the CreateTableRequest object
    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();
    // Create the table by using the DynamoDbAsyncClient object
    CompletableFuture<CreateTableResponse> response = client.createTable(request);
    // When future is complete (either successfully or in error) handle the response
    response.whenComplete((table, err) -> {
        try {
            if (table != null) {
                // Create a DescribeTableRequest object required for waiter functionality
                DescribeTableRequest tableRequest = DescribeTableRequest.builder().tableName(table.tableDescription().tableName()).build();
                CompletableFuture<WaiterResponse<DescribeTableResponse>> waiterResponse = asyncWaiter.waitUntilTableExists(tableRequest);
                // Fires when the table is ready
                waiterResponse.whenComplete((r, t) -> {
                    // print out the new table's ARN when its ready
                    String tableARN = r.matched().response().get().table().tableArn();
                    System.out.println("The table " + tableARN + " is ready");
                });
                waiterResponse.join();
            } else {
                // Handle error
                err.printStackTrace();
            }
        } finally {
            // Lets the application shut down. Only close the client when you are completely done with it.
            client.close();
        }
    });
    response.join();
}
Also used : WaiterResponse(software.amazon.awssdk.core.waiters.WaiterResponse) CreateTableResponse(software.amazon.awssdk.services.dynamodb.model.CreateTableResponse) DynamoDbAsyncWaiter(software.amazon.awssdk.services.dynamodb.waiters.DynamoDbAsyncWaiter) DescribeTableRequest(software.amazon.awssdk.services.dynamodb.model.DescribeTableRequest) CreateTableRequest(software.amazon.awssdk.services.dynamodb.model.CreateTableRequest)

Example 4 with CreateTableRequest

use of software.amazon.awssdk.services.dynamodb.model.CreateTableRequest in project aws-doc-sdk-examples by awsdocs.

the class Scenario method createTable.

// snippet-start:[dynamodb.java2.scenario.create_table.main]
// Create a table with a Sort key.
public static void createTable(DynamoDbClient ddb, String tableName) {
    DynamoDbWaiter dbWaiter = ddb.waiter();
    ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
    // Define attributes.
    attributeDefinitions.add(AttributeDefinition.builder().attributeName("year").attributeType("N").build());
    attributeDefinitions.add(AttributeDefinition.builder().attributeName("title").attributeType("S").build());
    ArrayList<KeySchemaElement> tableKey = new ArrayList<KeySchemaElement>();
    KeySchemaElement key = KeySchemaElement.builder().attributeName("year").keyType(KeyType.HASH).build();
    KeySchemaElement key2 = KeySchemaElement.builder().attributeName("title").keyType(// Sort
    KeyType.RANGE).build();
    // Add KeySchemaElement objects to the list.
    tableKey.add(key);
    tableKey.add(key2);
    CreateTableRequest request = CreateTableRequest.builder().keySchema(tableKey).provisionedThroughput(ProvisionedThroughput.builder().readCapacityUnits(new Long(10)).writeCapacityUnits(new Long(10)).build()).attributeDefinitions(attributeDefinitions).tableName(tableName).build();
    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);
        String newTable = response.tableDescription().tableName();
        System.out.println("The " + newTable + " was successfully created.");
    } catch (DynamoDbException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}
Also used : ArrayList(java.util.ArrayList) AttributeDefinition(software.amazon.awssdk.services.dynamodb.model.AttributeDefinition) DescribeTableResponse(software.amazon.awssdk.services.dynamodb.model.DescribeTableResponse) DescribeTableRequest(software.amazon.awssdk.services.dynamodb.model.DescribeTableRequest) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) CreateTableResponse(software.amazon.awssdk.services.dynamodb.model.CreateTableResponse) DynamoDbWaiter(software.amazon.awssdk.services.dynamodb.waiters.DynamoDbWaiter) CreateTableRequest(software.amazon.awssdk.services.dynamodb.model.CreateTableRequest) KeySchemaElement(software.amazon.awssdk.services.dynamodb.model.KeySchemaElement)

Aggregations

CreateTableRequest (software.amazon.awssdk.services.dynamodb.model.CreateTableRequest)4 CreateTableResponse (software.amazon.awssdk.services.dynamodb.model.CreateTableResponse)4 DescribeTableRequest (software.amazon.awssdk.services.dynamodb.model.DescribeTableRequest)3 DynamoDbException (software.amazon.awssdk.services.dynamodb.model.DynamoDbException)3 DescribeTableResponse (software.amazon.awssdk.services.dynamodb.model.DescribeTableResponse)2 DynamoDbWaiter (software.amazon.awssdk.services.dynamodb.waiters.DynamoDbWaiter)2 ArrayList (java.util.ArrayList)1 WaiterResponse (software.amazon.awssdk.core.waiters.WaiterResponse)1 AttributeDefinition (software.amazon.awssdk.services.dynamodb.model.AttributeDefinition)1 KeySchemaElement (software.amazon.awssdk.services.dynamodb.model.KeySchemaElement)1 DynamoDbAsyncWaiter (software.amazon.awssdk.services.dynamodb.waiters.DynamoDbAsyncWaiter)1