Java Code Examples for com.amazonaws.services.dynamodbv2.model.ReturnValue#ALL_NEW

The following examples show how to use com.amazonaws.services.dynamodbv2.model.ReturnValue#ALL_NEW . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LowLevelItemCRUDExample.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
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());
    }
}
 
Example 2
Source File: LowLevelItemCRUDExample.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
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);
        System.out.println(ase.getMessage()); // DELETEME
        System.err.println("Failed to update multiple attributes in " + tableName); // DELETEME
    }
}
 
Example 3
Source File: LowLevelItemCRUDExample.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 4
Source File: LowLevelItemCRUDExample.java    From aws-dynamodb-examples with Apache License 2.0 5 votes vote down vote up
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());
    }        
}
 
Example 5
Source File: LowLevelItemCRUDExample.java    From aws-dynamodb-examples with Apache License 2.0 5 votes vote down vote up
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);
        System.out.println(ase.getMessage());  //DELETEME
        System.err.println("Failed to update multiple attributes in " + tableName); //DELETEME
    }
}
 
Example 6
Source File: LowLevelItemCRUDExample.java    From aws-dynamodb-examples with Apache License 2.0 5 votes vote down vote up
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);
    }        
}