Search in sources :

Example 1 with AttributeDefinition

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

the class DescribeTable method describeDymamoDBTable.

// snippet-start:[dynamodb.java2.describe_table.main]
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!");
}
Also used : DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) AttributeDefinition(software.amazon.awssdk.services.dynamodb.model.AttributeDefinition) DescribeTableRequest(software.amazon.awssdk.services.dynamodb.model.DescribeTableRequest) ProvisionedThroughputDescription(software.amazon.awssdk.services.dynamodb.model.ProvisionedThroughputDescription) TableDescription(software.amazon.awssdk.services.dynamodb.model.TableDescription)

Example 2 with AttributeDefinition

use of software.amazon.awssdk.services.dynamodb.model.AttributeDefinition 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

AttributeDefinition (software.amazon.awssdk.services.dynamodb.model.AttributeDefinition)2 DescribeTableRequest (software.amazon.awssdk.services.dynamodb.model.DescribeTableRequest)2 DynamoDbException (software.amazon.awssdk.services.dynamodb.model.DynamoDbException)2 ArrayList (java.util.ArrayList)1 CreateTableRequest (software.amazon.awssdk.services.dynamodb.model.CreateTableRequest)1 CreateTableResponse (software.amazon.awssdk.services.dynamodb.model.CreateTableResponse)1 DescribeTableResponse (software.amazon.awssdk.services.dynamodb.model.DescribeTableResponse)1 KeySchemaElement (software.amazon.awssdk.services.dynamodb.model.KeySchemaElement)1 ProvisionedThroughputDescription (software.amazon.awssdk.services.dynamodb.model.ProvisionedThroughputDescription)1 TableDescription (software.amazon.awssdk.services.dynamodb.model.TableDescription)1 DynamoDbWaiter (software.amazon.awssdk.services.dynamodb.waiters.DynamoDbWaiter)1