Search in sources :

Example 21 with DynamoDbException

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

the class UpdateTable method updateDynamoDBTable.

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

Example 22 with DynamoDbException

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

the class ListTables method listAllTables.

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

Example 23 with DynamoDbException

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

the class Scenario method putRecord.

public static void putRecord(DynamoDbClient ddb) {
    try {
        // Create a DynamoDbEnhancedClient.
        DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder().dynamoDbClient(ddb).build();
        // Create a DynamoDbTable object.
        DynamoDbTable<Movies> table = enhancedClient.table("Movies", TableSchema.fromBean(Movies.class));
        // Populate the Table.
        Movies record = new Movies();
        record.setYear(2020);
        record.setTitle("My Movie2");
        record.setInfo("no info");
        // Put the data into a DynamoDB table.
        table.putItem(record);
    } catch (DynamoDbException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
    System.out.println("Added a new issue to the table.");
}
Also used : DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) DynamoDbEnhancedClient(software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient)

Example 24 with DynamoDbException

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

Example 25 with DynamoDbException

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

the class Scenario method queryTable.

// snippet-end:[dynamodb.java2.scenario.create_table.main]
// snippet-start:[dynamodb.java2.scenario.query.main]
// Query the table.
public static void queryTable(DynamoDbClient ddb) {
    try {
        DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder().dynamoDbClient(ddb).build();
        DynamoDbTable<Movies> custTable = enhancedClient.table("Movies", TableSchema.fromBean(Movies.class));
        QueryConditional queryConditional = QueryConditional.keyEqualTo(Key.builder().partitionValue(2013).build());
        // Get items in the table and write out the ID value
        Iterator<Movies> results = custTable.query(queryConditional).items().iterator();
        String result = "";
        while (results.hasNext()) {
            Movies rec = results.next();
            System.out.println("The title of the movie is " + rec.getTitle());
            System.out.println("The movie information  is " + rec.getInfo());
        }
    } catch (DynamoDbException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}
Also used : QueryConditional(software.amazon.awssdk.enhanced.dynamodb.model.QueryConditional) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) DynamoDbEnhancedClient(software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient)

Aggregations

DynamoDbException (software.amazon.awssdk.services.dynamodb.model.DynamoDbException)33 AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)13 HashMap (java.util.HashMap)10 DynamoDbEnhancedClient (software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient)8 DynamoDbClient (software.amazon.awssdk.services.dynamodb.DynamoDbClient)5 Instant (java.time.Instant)4 LocalDateTime (java.time.LocalDateTime)4 QueryConditional (software.amazon.awssdk.enhanced.dynamodb.model.QueryConditional)4 Region (software.amazon.awssdk.regions.Region)4 ResourceNotFoundException (software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException)4 LocalDate (java.time.LocalDate)3 DynamoDbPartitionKey (software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbPartitionKey)3 DynamoDbSortKey (software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbSortKey)3 CreateTableRequest (software.amazon.awssdk.services.dynamodb.model.CreateTableRequest)3 CreateTableResponse (software.amazon.awssdk.services.dynamodb.model.CreateTableResponse)3 DescribeTableRequest (software.amazon.awssdk.services.dynamodb.model.DescribeTableRequest)3 GetItemRequest (software.amazon.awssdk.services.dynamodb.model.GetItemRequest)3 ArrayList (java.util.ArrayList)2 Expression (software.amazon.awssdk.enhanced.dynamodb.Expression)2 Key (software.amazon.awssdk.enhanced.dynamodb.Key)2