@Override public void announce(long stateVersion) { Table table = dynamoDB.getTable(tableName); UpdateItemSpec updateItemSpec = new UpdateItemSpec() .withPrimaryKey("namespace", blobNamespace) .withUpdateExpression("set #version = :ver") .withNameMap(new NameMap().with("#version", "version")) .withValueMap(new ValueMap().withNumber(":ver", stateVersion)); table.updateItem(updateItemSpec); }
@Override public UpdateItemBuilder<T> updateItem(Object hk, Object rk) { UpdateItemSpec spec = new UpdateItemSpec(); if ( null != _rkName ) { spec.withPrimaryKey(_hkName, hk, _rkName, rk); } else { spec.withPrimaryKey(_hkName, hk); } return new UpdateItemBuilderImpl<T>(spec, this::updateItem, _clazz, _encryption, _convertValue); }
private boolean updateItem(IndexKey key, String updateExpression, String conditionalExpression, Map<String, String> expressionNames, Map<String, Object> expressionValues) { try { updateItem(new UpdateItemSpec() .withPrimaryKey(toPrimaryKey(key)) .withUpdateExpression(updateExpression) .withConditionExpression(conditionalExpression) .withNameMap(expressionNames) .withValueMap(expressionValues)); return true; } catch ( RollbackException ex ) { return false; } }
private <V> V updateItem(UpdateItemSpec updateItemSpec, Class<V> type) { try { return maybeBackoff(false, () -> fromItem(_encryption.decrypt(_updateItem.updateItem(updateItemSpec).getItem()), type)); } catch ( ConditionalCheckFailedException ex ) { throw new RollbackException(ex); } }
UpdateItemBuilderImpl(UpdateItemSpec spec, UpdateItem updateItem, Class<T> clazz, DDBEncryption encryption, ConvertValue convertValue) { _spec = spec; _updateItem = updateItem; _clazz = clazz; _encryption = encryption; _convertValue = convertValue; }
public static void main(String[] args) { AmazonDynamoDBClient client = new AmazonDynamoDBClient(); client.setEndpoint("http://localhost:8000"); DynamoDB dynamoDB = new DynamoDB(client); Table table = dynamoDB.getTable("Movies"); int year = 2015; String title = "The Big New Movie"; // Conditional update (will fail) UpdateItemSpec updateItemSpec = new UpdateItemSpec() .withPrimaryKey(new PrimaryKey("year", 2015, "title", "The Big New Movie")) .withUpdateExpression("remove info.actors[0]") .withConditionExpression("size(info.actors) > :num") .withValueMap(new ValueMap().withNumber(":num", 3)); System.out.println("Attempting a conditional update..."); try { table.updateItem(updateItemSpec); System.out.println("UpdateItem succeeded: " + table.getItem("year", year, "title", title).toJSONPretty()); } catch (ConditionalCheckFailedException e) { e.printStackTrace(); System.out.println("UpdateItem failed"); } }
public static void main(String[] args) { AmazonDynamoDBClient client = new AmazonDynamoDBClient(); client.setEndpoint("http://localhost:8000"); DynamoDB dynamoDB = new DynamoDB(client); Table table = dynamoDB.getTable("Movies"); int year = 2015; String title = "The Big New Movie"; UpdateItemSpec updateItemSpec = new UpdateItemSpec() .withPrimaryKey("year", year, "title", title) .withUpdateExpression("set info.rating = info.rating + :val") .withValueMap(new ValueMap() .withNumber(":val", 1)); System.out.println("Incrementing an atomic counter..."); try { table.updateItem(updateItemSpec); System.out.println("UpdateItem succeeded: " + table.getItem("year", year, "title", title).toJSONPretty()); } catch (Exception e) { System.out.println("UpdateItem failed"); e.printStackTrace(); } }
public static void main(String[] args) { AmazonDynamoDBClient client = new AmazonDynamoDBClient(); client.setEndpoint("http://localhost:8000"); DynamoDB dynamoDB = new DynamoDB(client); Table table = dynamoDB.getTable("Movies"); int year = 2015; String title = "The Big New Movie"; UpdateItemSpec updateItemSpec = new UpdateItemSpec() .withPrimaryKey("year", year, "title", title) .withUpdateExpression("set info.rating = :r, info.plot=:p, info.actors=:a") .withValueMap(new ValueMap() .withNumber(":r", 5.5) .withString(":p", "Everything happens all at once.") .withList(":a", Arrays.asList("Larry","Moe","Curly"))); System.out.println("Updating the item..."); try { table.updateItem(updateItemSpec); System.out.println("UpdateItem succeeded: " + table.getItem("year", year, "title", title).toJSONPretty()); } catch (Exception e) { System.out.println("UpdateItem failed"); e.printStackTrace(); } }
private static void updateAddNewAttribute() { Table table = dynamoDB.getTable(tableName); try { Map<String, String> expressionAttributeNames = new HashMap<String, String>(); expressionAttributeNames.put("#na", "NewAttribute"); UpdateItemSpec updateItemSpec = new UpdateItemSpec() .withPrimaryKey("Id", 121) .withUpdateExpression("set #na = :val1") .withNameMap(new NameMap() .with("#na", "NewAttribute")) .withValueMap(new ValueMap() .withString(":val1", "Some value")) .withReturnValues(ReturnValue.ALL_NEW); UpdateItemOutcome outcome = table.updateItem(updateItemSpec); // Check the response. System.out.println("Printing item after adding new attribute..."); System.out.println(outcome.getItem().toJSONPretty()); } catch (Exception e) { System.err.println("Failed to add new attribute in " + tableName); System.err.println(e.getMessage()); } }
private static void updateMultipleAttributes() { Table table = dynamoDB.getTable(tableName); try { UpdateItemSpec updateItemSpec = new UpdateItemSpec() .withPrimaryKey("Id", 120) .withUpdateExpression("add #a :val1 set #na=:val2") .withNameMap(new NameMap() .with("#a", "Authors") .with("#na", "NewAttribute")) .withValueMap(new ValueMap() .withStringSet(":val1", "Author YY", "Author ZZ") .withString(":val2", "someValue")) .withReturnValues(ReturnValue.ALL_NEW); UpdateItemOutcome outcome = table.updateItem(updateItemSpec); // Check the response. System.out .println("Printing item after multiple attribute update..."); System.out.println(outcome.getItem().toJSONPretty()); } catch (Exception e) { System.err.println("Failed to update multiple attributes in " + tableName); System.err.println(e.getMessage()); } }
private static void updateExistingAttributeConditionally() { Table table = dynamoDB.getTable(tableName); try { // Specify the desired price (25.00) and also the condition (price = // 20.00) UpdateItemSpec updateItemSpec = new UpdateItemSpec() .withPrimaryKey("Id", 120) .withReturnValues(ReturnValue.ALL_NEW) .withUpdateExpression("set #p = :val1") .withConditionExpression("#p = :val2") .withNameMap(new NameMap() .with("#p", "Price")) .withValueMap(new ValueMap() .withNumber(":val1", 25) .withNumber(":val2", 20)); UpdateItemOutcome outcome = table.updateItem(updateItemSpec); // Check the response. System.out .println("Printing item after conditional update to new attribute..."); System.out.println(outcome.getItem().toJSONPretty()); } catch (Exception e) { System.err.println("Error updating item in " + tableName); System.err.println(e.getMessage()); } }
private T updateItem(UpdateItemSpec updateItemSpec) { return updateItem(updateItemSpec, _clazz); }
public <V> V updateItem(UpdateItemSpec spec, Class<V> type);