/** * Sends an update request to the service and returns true if the request is successful. */ public boolean sendUpdateRequest(Map<String, AttributeValue> primaryKey, Map<String, AttributeValueUpdate> updateItems, Map<String, ExpectedAttributeValue> expectedItems) throws Exception { if (updateItems.isEmpty()) { return false; // No update, return false } UpdateItemRequest updateItemRequest = new UpdateItemRequest().withTableName(tableName).withKey(primaryKey).withReturnValues(ReturnValue.UPDATED_NEW) .withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL).withAttributeUpdates(updateItems); if (expectedItems != null) { updateItemRequest.withExpected(expectedItems); } UpdateItemResult result = dynamoDBClient.updateItem(updateItemRequest); if(!isRunningOnDDBLocal) { // DDB Local does not support rate limiting tableWriteRateLimiter.adjustRateWithConsumedCapacity(result.getConsumedCapacity()); } return true; }
private static void deleteItem() { Table table = dynamoDB.getTable(tableName); try { DeleteItemSpec deleteItemSpec = new DeleteItemSpec() .withPrimaryKey("Id", 120) .withConditionExpression("#ip = :val") .withNameMap(new NameMap() .with("#ip", "InPublication")) .withValueMap(new ValueMap() .withBoolean(":val", false)) .withReturnValues(ReturnValue.ALL_OLD); DeleteItemOutcome outcome = table.deleteItem(deleteItemSpec); // Check the response. System.out.println("Printing item that was deleted..."); System.out.println(outcome.getItem().toJSONPretty()); } catch (Exception e) { System.err.println("Error deleting item in " + tableName); System.err.println(e.getMessage()); } }
@Override public T putItem(T item) { PutItemSpec req = new PutItemSpec() .withItem(_encryption.encrypt(toItem(item))) .withReturnValues(ReturnValue.ALL_OLD); return fromItem( _encryption.decrypt( maybeBackoff(false, () -> _putItem.putItem(req)).getItem()), _clazz); }
@Override public Collection<MutateWorker> createMutationWorkers(final Map<StaticBuffer, KCVMutation> mutationMap, final DynamoDbStoreTransaction txh) { final List<MutateWorker> workers = Lists.newLinkedList(); for (Map.Entry<StaticBuffer, KCVMutation> entry : mutationMap.entrySet()) { final StaticBuffer hashKey = entry.getKey(); final KCVMutation mutation = entry.getValue(); final Map<String, AttributeValue> key = new ItemBuilder().hashKey(hashKey) .build(); // Using ExpectedAttributeValue map to handle large mutations in a single request // Large mutations would require multiple requests using expressions final Map<String, ExpectedAttributeValue> expected = new SingleExpectedAttributeValueBuilder(this, txh, hashKey).build(mutation); final Map<String, AttributeValueUpdate> attributeValueUpdates = new SingleUpdateBuilder().deletions(mutation.getDeletions()) .additions(mutation.getAdditions()) .build(); final UpdateItemRequest request = super.createUpdateItemRequest() .withKey(key) .withReturnValues(ReturnValue.ALL_NEW) .withAttributeUpdates(attributeValueUpdates) .withExpected(expected); final MutateWorker worker; if (mutation.hasDeletions() && !mutation.hasAdditions()) { worker = new SingleUpdateWithCleanupWorker(request, client.getDelegate()); } else { worker = new UpdateItemWorker(request, client.getDelegate()); } workers.add(worker); } return workers; }
@Override public UpdateItemResult updateItem(String tableName, HashMap<String, AttributeValue> primaryKey, Map<String, AttributeValueUpdate> updateItems) { Map<String, AttributeValueUpdate> attributeValueUpdates = new HashMap<String, AttributeValueUpdate>(); attributeValueUpdates.putAll(updateItems); UpdateItemRequest updateItemRequest = new UpdateItemRequest() .withTableName(tableName) .withKey(primaryKey).withReturnValues(ReturnValue.UPDATED_NEW) .withAttributeUpdates(updateItems); UpdateItemResult updateItemResult = dynamoDBClient.updateItem(updateItemRequest); LOG.info("Successful by updating item from " + tableName + ": " + updateItemResult); return updateItemResult; }
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()); } }
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 } }
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); } }
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); } }
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()); } }
@Override public Map<String, AttributeValue> put( final Map<String, AttributeValueUpdate> attrs) throws IOException { final AmazonDynamoDB aws = this.credentials.aws(); final Attributes expected = this.attributes.only(this.keys); try { final UpdateItemRequest request = new UpdateItemRequest() .withTableName(this.name) .withExpected(expected.asKeys()) .withKey(expected) .withAttributeUpdates(attrs) .withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL) .withReturnValues(ReturnValue.UPDATED_NEW); final long start = System.currentTimeMillis(); final UpdateItemResult result = aws.updateItem(request); Logger.info( this, "#put('%s'): updated item to DynamoDB, %s, in %[ms]s", attrs, new PrintableConsumedCapacity( result.getConsumedCapacity() ).print(), System.currentTimeMillis() - start ); return result.getAttributes(); } catch (final AmazonClientException ex) { throw new IOException( String.format( "failed to put %s into \"%s\" with %s", attrs, this.name, this.keys ), ex ); } finally { aws.shutdown(); } }
@Override public Item put(final Map<String, AttributeValue> attributes) throws IOException { final AmazonDynamoDB aws = this.credentials.aws(); try { final PutItemRequest request = new PutItemRequest(); request.setTableName(this.self); request.setItem(attributes); request.setReturnValues(ReturnValue.NONE); request.setReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL); final PutItemResult result = aws.putItem(request); final long start = System.currentTimeMillis(); Logger.info( this, "#put('%[text]s'): created item in '%s', %s, in %[ms]s", attributes, this.self, new PrintableConsumedCapacity( result.getConsumedCapacity() ).print(), System.currentTimeMillis() - start ); return new AwsItem( this.credentials, this.frame(), this.self, new Attributes(attributes).only(this.keys()), new Array<String>(this.keys()) ); } catch (final AmazonClientException ex) { throw new IOException( String.format( "failed to put into \"%s\" with %s", this.self, attributes ), ex ); } finally { aws.shutdown(); } }
@Override public void delete(final Map<String, AttributeValue> attributes) throws IOException { final AmazonDynamoDB aws = this.credentials.aws(); try { final DeleteItemRequest request = new DeleteItemRequest(); request.setTableName(this.self); request.setKey(attributes); request.setReturnValues(ReturnValue.NONE); request.setReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL); final DeleteItemResult result = aws.deleteItem(request); final long start = System.currentTimeMillis(); Logger.info( this, "#delete('%[text]s'): deleted item in '%s', %s, in %[ms]s", attributes, this.self, new PrintableConsumedCapacity( result.getConsumedCapacity() ).print(), System.currentTimeMillis() - start ); } catch (final AmazonClientException ex) { throw new IOException( String.format( "failed to delete at \"%s\" by keys %s", this.self, attributes ), ex ); } finally { aws.shutdown(); } }
@Override public UpdateItemBuilder<T> returnAllNew() { _spec.withReturnValues(ReturnValue.ALL_NEW); return this; }
@Override public UpdateItemBuilder<T> returnUpdatedNew() { _spec.withReturnValues(ReturnValue.UPDATED_NEW); return this; }
public UpdateItemResult updateConditionalValue(final AmazonDynamoDB dynamoClient, final String tableName, final UpdateKey key, final String attribute, final AggregateAttributeModification update) throws Exception { Map<String, AttributeValue> updateKey = StreamAggregatorUtils.getTableKey(key); UpdateItemResult result; final ReturnValue returnValue = ReturnValue.UPDATED_NEW; final String setAttribute = StreamAggregatorUtils.methodToColumn(attribute); // create the update that we want to write final Map<String, AttributeValueUpdate> thisCalcUpdate = new HashMap<String, AttributeValueUpdate>() { { put(setAttribute, new AttributeValueUpdate().withAction(AttributeAction.PUT).withValue( new AttributeValue().withN("" + update.getFinalValue()))); } }; // create the request UpdateItemRequest req = new UpdateItemRequest().withTableName(tableName).withKey(updateKey).withReturnValues( returnValue).withAttributeUpdates(thisCalcUpdate); Map<String, ExpectedAttributeValue> expected = new HashMap<>(); final SummaryCalculation calc = update.getCalculationApplied(); // try an update to PUT the value if NOT EXISTS, to establish if we // are the first writer for this key expected = new HashMap<String, ExpectedAttributeValue>() { { put(setAttribute, new ExpectedAttributeValue().withExists(false)); } }; req.setExpected(expected); try { result = DynamoUtils.updateWithRetries(dynamoClient, req); // yay - we were the first writer, so our value was written return result; } catch (ConditionalCheckFailedException e1) { // set the expected to the comparison contained in the update // calculation expected.clear(); expected.put( setAttribute, new ExpectedAttributeValue().withComparisonOperator( calc.getDynamoComparisonOperator()).withValue( new AttributeValue().withN("" + update.getFinalValue()))); req.setExpected(expected); // do the conditional update on the summary // calculation. this may result in no update being // applied because the new value is greater than the // current minimum for MIN, or less than the current // maximum for MAX. try { result = DynamoUtils.updateWithRetries(dynamoClient, req); return result; } catch (ConditionalCheckFailedException e2) { // no worries - we just weren't the min or max! return null; } } }
/** * Update an existing memo in the database * @param memo the memo to save */ public void update(Document memo) { Document document = dbTable.updateItem(memo, new UpdateItemOperationConfig().withReturnValues(ReturnValue.ALL_NEW)); }