Search in sources :

Example 6 with DynamoDbException

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

the class EnhancedQueryRecords method queryTable.

// snippet-start:[dynamodb.java2.mapping.query.main]
public static String queryTable(DynamoDbEnhancedClient enhancedClient) {
    try {
        DynamoDbTable<Customer> mappedTable = enhancedClient.table("Customer", TableSchema.fromBean(Customer.class));
        QueryConditional queryConditional = QueryConditional.keyEqualTo(Key.builder().partitionValue("id120").build());
        // Get items in the table and write out the ID value
        Iterator<Customer> results = mappedTable.query(queryConditional).items().iterator();
        String result = "";
        while (results.hasNext()) {
            Customer rec = results.next();
            result = rec.getId();
            System.out.println("The record id is " + result);
        }
        return result;
    } catch (DynamoDbException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
    return "";
}
Also used : QueryConditional(software.amazon.awssdk.enhanced.dynamodb.model.QueryConditional) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException)

Example 7 with DynamoDbException

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

the class EnhancedQueryRecordsWithFilter method queryTableFilter.

// snippet-start:[dynamodb.java2.mapping.queryfilter.main]
public static void queryTableFilter(DynamoDbEnhancedClient enhancedClient) {
    try {
        DynamoDbTable<Customer> mappedTable = enhancedClient.table("Customer", TableSchema.fromBean(Customer.class));
        AttributeValue att = AttributeValue.builder().s("sblue@noserver.com").build();
        Map<String, AttributeValue> expressionValues = new HashMap<>();
        expressionValues.put(":value", att);
        Expression expression = Expression.builder().expression("email = :value").expressionValues(expressionValues).build();
        // Create a QueryConditional object that is used in the query operation.
        QueryConditional queryConditional = QueryConditional.keyEqualTo(Key.builder().partitionValue("id103").build());
        // Get items in the Customer table and write out the ID value.
        Iterator<Customer> results = mappedTable.query(r -> r.queryConditional(queryConditional).filterExpression(expression)).items().iterator();
        while (results.hasNext()) {
            Customer rec = results.next();
            System.out.println("The record id is " + rec.getId());
        }
    } catch (DynamoDbException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
    System.out.println("Done");
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) HashMap(java.util.HashMap) Expression(software.amazon.awssdk.enhanced.dynamodb.Expression) QueryConditional(software.amazon.awssdk.enhanced.dynamodb.model.QueryConditional) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException)

Example 8 with DynamoDbException

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

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

the class DeleteTable method deleteDynamoDBTable.

// snippet-start:[dynamodb.java2.delete_table.main]
public static void deleteDynamoDBTable(DynamoDbClient ddb, String tableName) {
    DeleteTableRequest request = DeleteTableRequest.builder().tableName(tableName).build();
    try {
        ddb.deleteTable(request);
    } catch (DynamoDbException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
    System.out.println(tableName + " was successfully deleted!");
}
Also used : DeleteTableRequest(software.amazon.awssdk.services.dynamodb.model.DeleteTableRequest) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException)

Example 10 with DynamoDbException

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

the class EnhancedGetItem method getItem.

// snippet-start:[dynamodb.java2.mapping.getitem.main]
public static String getItem(DynamoDbEnhancedClient enhancedClient) {
    try {
        // Create a DynamoDbTable object
        DynamoDbTable<Customer> mappedTable = enhancedClient.table("Customer", TableSchema.fromBean(Customer.class));
        // Create a KEY object
        Key key = Key.builder().partitionValue("id146").build();
        // Get the item by using the key
        Customer result = mappedTable.getItem(r -> r.key(key));
        return "The email value is " + result.getEmail();
    } catch (DynamoDbException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
    return "";
}
Also used : DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) DynamoDbSortKey(software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbSortKey) Key(software.amazon.awssdk.enhanced.dynamodb.Key) DynamoDbPartitionKey(software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbPartitionKey)

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