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);
}
}
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);
}
}
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);
}
}
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());
}
}
Aggregations