Search in sources :

Example 1 with ReturnValue

use of com.amazonaws.services.dynamodbv2.model.ReturnValue in project aws-doc-sdk-examples by awsdocs.

the class LowLevelItemCRUDExample method updateMultipleAttributes.

private static void updateMultipleAttributes() {
    try {
        HashMap<String, AttributeValue> key = new HashMap<String, AttributeValue>();
        key.put("Id", new AttributeValue().withN("120"));
        Map<String, AttributeValue> expressionAttributeValues = new HashMap<String, AttributeValue>();
        expressionAttributeValues.put(":val1", new AttributeValue().withSS("Author YY", "Author ZZ"));
        expressionAttributeValues.put(":val2", new AttributeValue().withS("someValue"));
        ReturnValue returnValues = ReturnValue.ALL_NEW;
        UpdateItemRequest updateItemRequest = new UpdateItemRequest().withTableName(tableName).withKey(key).withUpdateExpression("add Authors :val1 set NewAttribute=:val2").withExpressionAttributeValues(expressionAttributeValues).withReturnValues(returnValues);
        UpdateItemResult result = client.updateItem(updateItemRequest);
        // Check the response.
        System.out.println("Printing item after multiple attribute update...");
        printItem(result.getAttributes());
    } catch (AmazonServiceException ase) {
        System.err.println("Failed to update multiple attributes in " + tableName);
        // DELETEME
        System.out.println(ase.getMessage());
        // DELETEME
        System.err.println("Failed to update multiple attributes in " + tableName);
    }
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) UpdateItemRequest(com.amazonaws.services.dynamodbv2.model.UpdateItemRequest) HashMap(java.util.HashMap) AmazonServiceException(com.amazonaws.AmazonServiceException) ReturnValue(com.amazonaws.services.dynamodbv2.model.ReturnValue) UpdateItemResult(com.amazonaws.services.dynamodbv2.model.UpdateItemResult)

Example 2 with ReturnValue

use of com.amazonaws.services.dynamodbv2.model.ReturnValue in project aws-doc-sdk-examples by awsdocs.

the class LowLevelItemCRUDExample method updateExistingAttributeConditionally.

private static void updateExistingAttributeConditionally() {
    try {
        HashMap<String, AttributeValue> key = new HashMap<String, AttributeValue>();
        key.put("Id", new AttributeValue().withN("120"));
        // Specify the desired price (25.00) and also the condition (price =
        // 20.00)
        Map<String, AttributeValue> expressionAttributeValues = new HashMap<String, AttributeValue>();
        expressionAttributeValues.put(":val1", new AttributeValue().withN("25.00"));
        expressionAttributeValues.put(":val2", new AttributeValue().withN("20.00"));
        ReturnValue returnValues = ReturnValue.ALL_NEW;
        UpdateItemRequest updateItemRequest = new UpdateItemRequest().withTableName(tableName).withKey(key).withUpdateExpression("set Price = :val1").withConditionExpression("Price = :val2").withExpressionAttributeValues(expressionAttributeValues).withReturnValues(returnValues);
        UpdateItemResult result = client.updateItem(updateItemRequest);
        // Check the response.
        System.out.println("Printing item after conditional update to new attribute...");
        printItem(result.getAttributes());
    } catch (ConditionalCheckFailedException cse) {
        // Reload object and retry code.
        System.err.println("Conditional check failed in " + tableName);
    } catch (AmazonServiceException ase) {
        System.err.println("Error updating item in " + tableName);
    }
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) UpdateItemRequest(com.amazonaws.services.dynamodbv2.model.UpdateItemRequest) HashMap(java.util.HashMap) AmazonServiceException(com.amazonaws.AmazonServiceException) ReturnValue(com.amazonaws.services.dynamodbv2.model.ReturnValue) UpdateItemResult(com.amazonaws.services.dynamodbv2.model.UpdateItemResult) ConditionalCheckFailedException(com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException)

Example 3 with ReturnValue

use of com.amazonaws.services.dynamodbv2.model.ReturnValue in project aws-doc-sdk-examples by awsdocs.

the class LowLevelItemCRUDExample method deleteItem.

private static void deleteItem() {
    try {
        HashMap<String, AttributeValue> key = new HashMap<String, AttributeValue>();
        key.put("Id", new AttributeValue().withN("120"));
        Map<String, AttributeValue> expressionAttributeValues = new HashMap<String, AttributeValue>();
        expressionAttributeValues.put(":val", new AttributeValue().withBOOL(false));
        ReturnValue returnValues = ReturnValue.ALL_OLD;
        DeleteItemRequest deleteItemRequest = new DeleteItemRequest().withTableName(tableName).withKey(key).withConditionExpression("InPublication = :val").withExpressionAttributeValues(expressionAttributeValues).withReturnValues(returnValues);
        DeleteItemResult result = client.deleteItem(deleteItemRequest);
        // Check the response.
        System.out.println("Printing item that was deleted...");
        printItem(result.getAttributes());
    } catch (AmazonServiceException ase) {
        System.err.println("Failed to get item after deletion " + tableName);
    }
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) HashMap(java.util.HashMap) DeleteItemRequest(com.amazonaws.services.dynamodbv2.model.DeleteItemRequest) AmazonServiceException(com.amazonaws.AmazonServiceException) ReturnValue(com.amazonaws.services.dynamodbv2.model.ReturnValue) DeleteItemResult(com.amazonaws.services.dynamodbv2.model.DeleteItemResult)

Example 4 with ReturnValue

use of com.amazonaws.services.dynamodbv2.model.ReturnValue in project aws-doc-sdk-examples by awsdocs.

the class LowLevelItemCRUDExample method updateAddNewAttribute.

private static void updateAddNewAttribute() {
    try {
        HashMap<String, AttributeValue> key = new HashMap<String, AttributeValue>();
        key.put("Id", new AttributeValue().withN("121"));
        Map<String, AttributeValue> expressionAttributeValues = new HashMap<String, AttributeValue>();
        expressionAttributeValues.put(":val1", new AttributeValue().withS("Some value"));
        ReturnValue returnValues = ReturnValue.ALL_NEW;
        UpdateItemRequest updateItemRequest = new UpdateItemRequest().withTableName(tableName).withKey(key).withUpdateExpression("set NewAttribute = :val1").withExpressionAttributeValues(expressionAttributeValues).withReturnValues(returnValues);
        UpdateItemResult result = client.updateItem(updateItemRequest);
        // Check the response.
        System.out.println("Printing item after adding new attribute...");
        printItem(result.getAttributes());
    } catch (AmazonServiceException ase) {
        System.err.println("Failed to add new attribute in " + tableName);
        System.err.println(ase.getMessage());
    }
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) UpdateItemRequest(com.amazonaws.services.dynamodbv2.model.UpdateItemRequest) HashMap(java.util.HashMap) AmazonServiceException(com.amazonaws.AmazonServiceException) ReturnValue(com.amazonaws.services.dynamodbv2.model.ReturnValue) UpdateItemResult(com.amazonaws.services.dynamodbv2.model.UpdateItemResult)

Aggregations

AmazonServiceException (com.amazonaws.AmazonServiceException)4 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)4 ReturnValue (com.amazonaws.services.dynamodbv2.model.ReturnValue)4 HashMap (java.util.HashMap)4 UpdateItemRequest (com.amazonaws.services.dynamodbv2.model.UpdateItemRequest)3 UpdateItemResult (com.amazonaws.services.dynamodbv2.model.UpdateItemResult)3 ConditionalCheckFailedException (com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException)1 DeleteItemRequest (com.amazonaws.services.dynamodbv2.model.DeleteItemRequest)1 DeleteItemResult (com.amazonaws.services.dynamodbv2.model.DeleteItemResult)1