/** * Returns a string representation of this object. This is useful for testing and debugging. Sensitive data will be * redacted from this string using a placeholder value. * * @return A string representation of this object. * * @see java.lang.Object#toString() */ @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("{"); if (getAttributes() != null) sb.append("Attributes: ").append(getAttributes()).append(","); if (getConsumedCapacity() != null) sb.append("ConsumedCapacity: ").append(getConsumedCapacity()).append(","); if (getItemCollectionMetrics() != null) sb.append("ItemCollectionMetrics: ").append(getItemCollectionMetrics()); sb.append("}"); return sb.toString(); }
/** * Returns all the returned attributes as a (non-null) {@link Item}. */ public Item getItem() { Map<String, Object> attributes = InternalUtils.toSimpleMapValue(result.getAttributes()); Item item = Item.fromMap(attributes); return item; }
@Override public int hashCode() { final int prime = 31; int hashCode = 1; hashCode = prime * hashCode + ((getAttributes() == null) ? 0 : getAttributes().hashCode()); hashCode = prime * hashCode + ((getConsumedCapacity() == null) ? 0 : getConsumedCapacity().hashCode()); hashCode = prime * hashCode + ((getItemCollectionMetrics() == null) ? 0 : getItemCollectionMetrics().hashCode()); return hashCode; }
@Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (obj instanceof UpdateItemResult == false) return false; UpdateItemResult other = (UpdateItemResult) obj; if (other.getAttributes() == null ^ this.getAttributes() == null) return false; if (other.getAttributes() != null && other.getAttributes().equals(this.getAttributes()) == false) return false; if (other.getConsumedCapacity() == null ^ this.getConsumedCapacity() == null) return false; if (other.getConsumedCapacity() != null && other.getConsumedCapacity().equals(this.getConsumedCapacity()) == false) return false; if (other.getItemCollectionMetrics() == null ^ this.getItemCollectionMetrics() == null) return false; if (other.getItemCollectionMetrics() != null && other.getItemCollectionMetrics().equals(this.getItemCollectionMetrics()) == false) return false; return true; }
@Override protected void executeLowLevelRequest() { UpdateItemResult updateItemResult = doUpdateItem(); // The UpdateItem request is specified to return ALL_NEW // attributes of the affected item. So if the returned // UpdateItemResult does not include any ReturnedAttributes, // it indicates the UpdateItem failed silently (e.g. the // key-only-put nightmare - // https://forums.aws.amazon.com/thread.jspa?threadID=86798&tstart=25), // in which case we should re-send a PutItem // request instead. if (updateItemResult.getAttributes() == null || updateItemResult.getAttributes().isEmpty()) { // Before we proceed with PutItem, we need to put all // the key attributes (prepared for the // UpdateItemRequest) into the AttributeValueUpdates // collection. for (String keyAttributeName : getPrimaryKeyAttributeValues().keySet()) { getAttributeValueUpdates().put(keyAttributeName, new AttributeValueUpdate() .withValue(getPrimaryKeyAttributeValues().get(keyAttributeName)) .withAction("PUT")); } doPutItem(); } } };
/** * Returns a string representation of this object; useful for testing and * debugging. * * @return A string representation of this object. * @see java.lang.Object#toString() */ @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("{"); if (getAttributes() != null) sb.append("Attributes: " + getAttributes() + ","); if (getConsumedCapacity() != null) sb.append("ConsumedCapacity: " + getConsumedCapacity() + ","); if (getItemCollectionMetrics() != null) sb.append("ItemCollectionMetrics: " + getItemCollectionMetrics()); sb.append("}"); return sb.toString(); }
@Override public int hashCode() { final int prime = 31; int hashCode = 1; hashCode = prime * hashCode + ((getAttributes() == null) ? 0 : getAttributes().hashCode()); hashCode = prime * hashCode + ((getConsumedCapacity() == null) ? 0 : getConsumedCapacity().hashCode()); hashCode = prime * hashCode + ((getItemCollectionMetrics() == null) ? 0 : getItemCollectionMetrics().hashCode()); return hashCode; }
@Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (obj instanceof UpdateItemResult == false) return false; UpdateItemResult other = (UpdateItemResult) obj; if (other.getAttributes() == null ^ this.getAttributes() == null) return false; if (other.getAttributes() != null && other.getAttributes().equals(this.getAttributes()) == false) return false; if (other.getConsumedCapacity() == null ^ this.getConsumedCapacity() == null) return false; if (other.getConsumedCapacity() != null && other.getConsumedCapacity().equals(this.getConsumedCapacity()) == false) return false; if (other.getItemCollectionMetrics() == null ^ this.getItemCollectionMetrics() == null) return false; if (other.getItemCollectionMetrics() != null && other.getItemCollectionMetrics().equals(this.getItemCollectionMetrics()) == false) return false; return true; } }
@Override protected void executeLowLevelRequest() { final UpdateItemResult updateItemResult = doUpdateItem(); // The UpdateItem request is specified to return ALL_NEW // attributes of the affected item. So if the returned // UpdateItemResult does not include any ReturnedAttributes, // it indicates the UpdateItem failed silently (e.g. the // key-only-put nightmare - // https://forums.aws.amazon.com/thread.jspa?threadID=86798&tstart=25), // in which case we should re-send a PutItem // request instead. if (updateItemResult.getAttributes() == null || updateItemResult.getAttributes().isEmpty()) { // Before we proceed with PutItem, we need to put all // the key attributes (prepared for the // UpdateItemRequest) into the AttributeValueUpdates // collection. for (final String keyAttributeName : getKeyAttributeValues().keySet()) { getAttributeValueUpdates().put( keyAttributeName, new AttributeValueUpdate() .withValue( getKeyAttributeValues().get(keyAttributeName)) .withAction("PUT")); } doPutItem(); } } };
private Document updateItem(Document doc, Key key, UpdateItemOperationConfig config) { final boolean updateChangedAttributes = !this.haveKeysChanged(doc); final Map<String, AttributeValueUpdate> attributeUpdateValues = this.toAttributeUpdateMap( doc, updateChangedAttributes); for (final String keyName : this.keys.keySet()) { attributeUpdateValues.remove(keyName); } final UpdateItemRequest updateRequest = new UpdateItemRequest(); updateRequest.setTableName(this.tableName); updateRequest.setKey(key); updateRequest.setAttributeUpdates( attributeUpdateValues.size() == 0 ? null : attributeUpdateValues); updateRequest.setReturnValues(config.getReturnValue()); Table.appendDynamoDBDocumentUserAgentString(updateRequest); final UpdateItemResult result = this.client.updateItem(updateRequest); doc.commit(); if (config.getReturnValue() != ReturnValue.NONE) { return this.fromAttributeMap(result.getAttributes()); } return null; }
/** * Returns a string representation of this object. This is useful for testing and debugging. Sensitive data will be * redacted from this string using a placeholder value. * * @return A string representation of this object. * * @see java.lang.Object#toString() */ @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("{"); if (getAttributes() != null) sb.append("Attributes: ").append(getAttributes()).append(","); if (getConsumedCapacity() != null) sb.append("ConsumedCapacity: ").append(getConsumedCapacity()).append(","); if (getItemCollectionMetrics() != null) sb.append("ItemCollectionMetrics: ").append(getItemCollectionMetrics()); sb.append("}"); return sb.toString(); }
/** * Returns all the returned attributes as a (non-null) {@link Item}. */ public Item getItem() { Map<String, Object> attributes = InternalUtils.toSimpleMapValue(result.getAttributes()); Item item = Item.fromMap(attributes); return item; }
@Override public int hashCode() { final int prime = 31; int hashCode = 1; hashCode = prime * hashCode + ((getAttributes() == null) ? 0 : getAttributes().hashCode()); hashCode = prime * hashCode + ((getConsumedCapacity() == null) ? 0 : getConsumedCapacity().hashCode()); hashCode = prime * hashCode + ((getItemCollectionMetrics() == null) ? 0 : getItemCollectionMetrics().hashCode()); return hashCode; }
@Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (obj instanceof UpdateItemResult == false) return false; UpdateItemResult other = (UpdateItemResult) obj; if (other.getAttributes() == null ^ this.getAttributes() == null) return false; if (other.getAttributes() != null && other.getAttributes().equals(this.getAttributes()) == false) return false; if (other.getConsumedCapacity() == null ^ this.getConsumedCapacity() == null) return false; if (other.getConsumedCapacity() != null && other.getConsumedCapacity().equals(this.getConsumedCapacity()) == false) return false; if (other.getItemCollectionMetrics() == null ^ this.getItemCollectionMetrics() == null) return false; if (other.getItemCollectionMetrics() != null && other.getItemCollectionMetrics().equals(this.getItemCollectionMetrics()) == false) return false; return true; }
@Override protected void executeLowLevelRequest() { UpdateItemResult updateItemResult = doUpdateItem(); // The UpdateItem request is specified to return ALL_NEW // attributes of the affected item. So if the returned // UpdateItemResult does not include any ReturnedAttributes, // it indicates the UpdateItem failed silently (e.g. the // key-only-put nightmare - // https://forums.aws.amazon.com/thread.jspa?threadID=86798&tstart=25), // in which case we should re-send a PutItem // request instead. if (updateItemResult.getAttributes() == null || updateItemResult.getAttributes().isEmpty()) { // Before we proceed with PutItem, we need to put all // the key attributes (prepared for the // UpdateItemRequest) into the AttributeValueUpdates // collection. for (String keyAttributeName : getPrimaryKeyAttributeValues().keySet()) { getAttributeValueUpdates().put(keyAttributeName, new AttributeValueUpdate() .withValue(getPrimaryKeyAttributeValues().get(keyAttributeName)) .withAction("PUT")); } doPutItem(); } } };
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 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()); } }
printItem(result.getAttributes()); } catch (ConditionalCheckFailedException cse) {
log.info("Item updated : {}", result.getAttributes()); } catch (final Exception e) { log.warn("Error while updating item in the table!", e);
@Override public Void call() throws BackendException { final UpdateItem updateBackoff = new UpdateItem(updateItemRequest, dynamoDbDelegate); final UpdateItemResult result = updateBackoff.runWithBackoff(); final Map<String, AttributeValue> item = result.getAttributes(); if (item == null) { // bail return null; } // If the record has no Titan columns left after deletions occur, then just delete the record if (item.containsKey(Constants.JANUSGRAPH_HASH_KEY) && item.size() == ATTRIBUTES_IN_EMPTY_SINGLE_ITEM) { final DeleteItem deleteBackoff = new DeleteItem(new DeleteItemRequest().withTableName(updateItemRequest.getTableName()) .withKey(updateItemRequest.getKey()), dynamoDbDelegate); deleteBackoff.runWithBackoff(); } // void return null; } }