Java Code Examples for software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest#Builder
The following examples show how to use
software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest#Builder .
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: UpdateItemOperationTest.java From aws-sdk-java-v2 with Apache License 2.0 | 5 votes |
@Test public void generateRequest_nullValuesNotIgnoredByDefault() { FakeItemWithSort item = createUniqueFakeItemWithSort(); item.setOtherAttribute1("value-1"); UpdateItemOperation<FakeItemWithSort> updateItemOperation = UpdateItemOperation.create( UpdateItemEnhancedRequest.builder(FakeItemWithSort.class).item(item).build()); Map<String, AttributeValue> expectedKey = new HashMap<>(); expectedKey.put("id", AttributeValue.builder().s(item.getId()).build()); expectedKey.put("sort", AttributeValue.builder().s(item.getSort()).build()); Map<String, AttributeValue> expectedValues = new HashMap<>(); expectedValues.put(OTHER_ATTRIBUTE_1_VALUE, AttributeValue.builder().s("value-1").build()); Map<String, String> expectedNames = new HashMap<>(); expectedNames.put(OTHER_ATTRIBUTE_1_NAME, "other_attribute_1"); expectedNames.put(OTHER_ATTRIBUTE_2_NAME, "other_attribute_2"); UpdateItemRequest.Builder baseExpectedRequest = UpdateItemRequest.builder() .tableName(TABLE_NAME) .expressionAttributeValues(expectedValues) .expressionAttributeNames(expectedNames) .key(expectedKey) .returnValues(ReturnValue.ALL_NEW); UpdateItemRequest expectedRequest = baseExpectedRequest.updateExpression("SET " + OTHER_ATTRIBUTE_1_NAME + " = " + OTHER_ATTRIBUTE_1_VALUE + " REMOVE " + OTHER_ATTRIBUTE_2_NAME) .build(); UpdateItemRequest request = updateItemOperation.generateRequest(FakeItemWithSort.getTableSchema(), PRIMARY_CONTEXT, null); assertThat(request, is(expectedRequest)); }
Example 2
Source File: UpdateItemOperationTest.java From aws-sdk-java-v2 with Apache License 2.0 | 5 votes |
@Test public void generateRequest_withConditionExpression() { FakeItemWithSort item = createUniqueFakeItemWithSort(); item.setOtherAttribute1("value-1"); UpdateItemOperation<FakeItemWithSort> updateItemOperation = UpdateItemOperation.create(UpdateItemEnhancedRequest.builder(FakeItemWithSort.class) .item(item) .conditionExpression(CONDITION_EXPRESSION) .build()); Map<String, AttributeValue> expectedKey = new HashMap<>(); expectedKey.put("id", AttributeValue.builder().s(item.getId()).build()); expectedKey.put("sort", AttributeValue.builder().s(item.getSort()).build()); Map<String, AttributeValue> expectedValues = new HashMap<>(CONDITION_EXPRESSION.expressionValues()); expectedValues.put(OTHER_ATTRIBUTE_1_VALUE, AttributeValue.builder().s("value-1").build()); Map<String, String> expectedNames = new HashMap<>(CONDITION_EXPRESSION.expressionNames()); expectedNames.put(OTHER_ATTRIBUTE_1_NAME, "other_attribute_1"); expectedNames.put(OTHER_ATTRIBUTE_2_NAME, "other_attribute_2"); UpdateItemRequest.Builder baseExpectedRequest = UpdateItemRequest.builder() .tableName(TABLE_NAME) .expressionAttributeValues(expectedValues) .expressionAttributeNames(expectedNames) .conditionExpression(CONDITION_EXPRESSION.expression()) .key(expectedKey) .returnValues(ReturnValue.ALL_NEW); UpdateItemRequest expectedRequest = baseExpectedRequest.updateExpression("SET " + OTHER_ATTRIBUTE_1_NAME + " = " + OTHER_ATTRIBUTE_1_VALUE + " REMOVE " + OTHER_ATTRIBUTE_2_NAME) .build(); UpdateItemRequest request = updateItemOperation.generateRequest(FakeItemWithSort.getTableSchema(), PRIMARY_CONTEXT, null); assertThat(request, is(expectedRequest)); }
Example 3
Source File: UpdateItemOperationTest.java From aws-sdk-java-v2 with Apache License 2.0 | 5 votes |
@Test public void generateRequest_explicitlyUnsetIgnoreNulls() { FakeItemWithSort item = createUniqueFakeItemWithSort(); item.setOtherAttribute1("value-1"); UpdateItemOperation<FakeItemWithSort> updateItemOperation = UpdateItemOperation.create(UpdateItemEnhancedRequest.builder(FakeItemWithSort.class) .item(item) .ignoreNulls(false) .build()); Map<String, AttributeValue> expectedKey = new HashMap<>(); expectedKey.put("id", AttributeValue.builder().s(item.getId()).build()); expectedKey.put("sort", AttributeValue.builder().s(item.getSort()).build()); Map<String, AttributeValue> expectedValues = new HashMap<>(); expectedValues.put(OTHER_ATTRIBUTE_1_VALUE, AttributeValue.builder().s("value-1").build()); Map<String, String> expectedNames = new HashMap<>(); expectedNames.put(OTHER_ATTRIBUTE_1_NAME, "other_attribute_1"); expectedNames.put(OTHER_ATTRIBUTE_2_NAME, "other_attribute_2"); UpdateItemRequest.Builder baseExpectedRequest = UpdateItemRequest.builder() .tableName(TABLE_NAME) .expressionAttributeValues(expectedValues) .expressionAttributeNames(expectedNames) .key(expectedKey) .returnValues(ReturnValue.ALL_NEW); UpdateItemRequest expectedRequest = baseExpectedRequest.updateExpression("SET " + OTHER_ATTRIBUTE_1_NAME + " = " + OTHER_ATTRIBUTE_1_VALUE + " REMOVE " + OTHER_ATTRIBUTE_2_NAME) .build(); UpdateItemRequest request = updateItemOperation.generateRequest(FakeItemWithSort.getTableSchema(), PRIMARY_CONTEXT, null); assertThat(request, is(expectedRequest)); }
Example 4
Source File: UpdateItemOperationTest.java From aws-sdk-java-v2 with Apache License 2.0 | 5 votes |
@Test public void generateRequest_multipleSetters() { FakeItemWithSort item = createUniqueFakeItemWithSort(); item.setOtherAttribute1("value-1"); item.setOtherAttribute2("value-2"); UpdateItemOperation<FakeItemWithSort> updateItemOperation = UpdateItemOperation.create(UpdateItemEnhancedRequest.builder(FakeItemWithSort.class) .item(item) .ignoreNulls(false) .build()); Map<String, AttributeValue> expectedKey = new HashMap<>(); expectedKey.put("id", AttributeValue.builder().s(item.getId()).build()); expectedKey.put("sort", AttributeValue.builder().s(item.getSort()).build()); Map<String, AttributeValue> expectedValues = new HashMap<>(); expectedValues.put(OTHER_ATTRIBUTE_1_VALUE, AttributeValue.builder().s("value-1").build()); expectedValues.put(OTHER_ATTRIBUTE_2_VALUE, AttributeValue.builder().s("value-2").build()); Map<String, String> expectedNames = new HashMap<>(); expectedNames.put(OTHER_ATTRIBUTE_1_NAME, "other_attribute_1"); expectedNames.put(OTHER_ATTRIBUTE_2_NAME, "other_attribute_2"); UpdateItemRequest.Builder baseExpectedRequest = UpdateItemRequest.builder() .tableName(TABLE_NAME) .expressionAttributeValues(expectedValues) .expressionAttributeNames(expectedNames) .key(expectedKey) .returnValues(ReturnValue.ALL_NEW); UpdateItemRequest expectedRequest1 = baseExpectedRequest.updateExpression("SET " + OTHER_ATTRIBUTE_1_NAME + " = " + OTHER_ATTRIBUTE_1_VALUE + ", " + OTHER_ATTRIBUTE_2_NAME + " = " + OTHER_ATTRIBUTE_2_VALUE) .build(); UpdateItemRequest expectedRequest2 = baseExpectedRequest.updateExpression("SET " + OTHER_ATTRIBUTE_2_NAME + " = " + OTHER_ATTRIBUTE_2_VALUE + ", " + OTHER_ATTRIBUTE_1_NAME + " = " + OTHER_ATTRIBUTE_1_VALUE) .build(); UpdateItemRequest request = updateItemOperation.generateRequest(FakeItemWithSort.getTableSchema(), PRIMARY_CONTEXT, null); assertThat(request, either(is(expectedRequest1)).or(is(expectedRequest2))); }
Example 5
Source File: UpdateItemOperationTest.java From aws-sdk-java-v2 with Apache License 2.0 | 5 votes |
@Test public void generateRequest_multipleDeletes() { FakeItemWithSort item = createUniqueFakeItemWithSort(); UpdateItemOperation<FakeItemWithSort> updateItemOperation = UpdateItemOperation.create(UpdateItemEnhancedRequest.builder(FakeItemWithSort.class) .item(item) .ignoreNulls(false) .build()); Map<String, AttributeValue> expectedKey = new HashMap<>(); expectedKey.put("id", AttributeValue.builder().s(item.getId()).build()); expectedKey.put("sort", AttributeValue.builder().s(item.getSort()).build()); Map<String, String> expectedNames = new HashMap<>(); expectedNames.put(OTHER_ATTRIBUTE_1_NAME, "other_attribute_1"); expectedNames.put(OTHER_ATTRIBUTE_2_NAME, "other_attribute_2"); UpdateItemRequest.Builder baseExpectedRequest = UpdateItemRequest.builder() .tableName(TABLE_NAME) .expressionAttributeNames(expectedNames) .key(expectedKey) .returnValues(ReturnValue.ALL_NEW); UpdateItemRequest expectedRequest1 = baseExpectedRequest.updateExpression("REMOVE " + OTHER_ATTRIBUTE_1_NAME + ", " + OTHER_ATTRIBUTE_2_NAME) .build(); UpdateItemRequest expectedRequest2 = baseExpectedRequest.updateExpression("REMOVE " + OTHER_ATTRIBUTE_2_NAME + ", " + OTHER_ATTRIBUTE_1_NAME) .build(); UpdateItemRequest request = updateItemOperation.generateRequest(FakeItemWithSort.getTableSchema(), PRIMARY_CONTEXT, null); assertThat(request,either(is(expectedRequest1)).or(is(expectedRequest2))); }
Example 6
Source File: AWSDynamoDAO.java From para with Apache License 2.0 | 5 votes |
private boolean updateRow(String key, String appid, Map<String, AttributeValue> row) { if (StringUtils.isBlank(key) || StringUtils.isBlank(appid) || row == null || row.isEmpty()) { return false; } String table = getTableNameForAppid(appid); try { UpdateItemRequest.Builder updateRequest = UpdateItemRequest.builder(); StringBuilder updateExpression = new StringBuilder("SET "); Map<String, String> names = new HashMap<>(row.size() + 1); Map<String, AttributeValue> values = new HashMap<>(row.size() + 1); boolean isLockingEnabledForRow = false; AttributeValue version = row.remove(Config._VERSION); // ignore the version field here if (version == null || version.n() == null) { version = AttributeValue.builder().n("0").build(); } if (Long.parseLong(version.n()) > 0L) { isLockingEnabledForRow = true; } for (Entry<String, AttributeValue> attr : row.entrySet()) { String name = "#" + attr.getKey(); String value = ":" + attr.getKey(); updateExpression.append(name).append("=").append(value).append(","); names.put(name, attr.getKey()); values.put(value, attr.getValue()); } updateExpression.setLength(updateExpression.length() - 1); // remove comma at the end if (isLockingEnabledForRow) { names.put("#" + Config._VERSION, Config._VERSION); values.put(":" + Config._VERSION, version); values.put(":plusOne", AttributeValue.builder().n("1").build()); updateRequest.conditionExpression("#" + Config._VERSION + " = :" + Config._VERSION); updateExpression.append(" ADD #").append(Config._VERSION).append(" :plusOne"); } updateRequest.tableName(table); updateRequest.key(rowKey(key, appid)); updateRequest.expressionAttributeNames(names); updateRequest.expressionAttributeValues(values); updateRequest.updateExpression(updateExpression.toString()); client().updateItem(updateRequest.build()); return true; } catch (ConditionalCheckFailedException ex) { logger.warn("Item not updated - versions don't match. table={}, appid={}, key={}.", table, appid, key); } catch (Exception e) { logger.error("Could not update row in DB - table={}, appid={}, key={}:", table, appid, key, e); throwIfNecessary(e); } return false; }
Example 7
Source File: UpdateItemOperation.java From aws-sdk-java-v2 with Apache License 2.0 | 4 votes |
@Override public UpdateItemRequest generateRequest(TableSchema<T> tableSchema, OperationContext operationContext, DynamoDbEnhancedClientExtension extension) { if (!TableMetadata.primaryIndexName().equals(operationContext.indexName())) { throw new IllegalArgumentException("UpdateItem cannot be executed against a secondary index."); } Map<String, AttributeValue> itemMap = tableSchema.itemToMap(this.request.item(), Boolean.TRUE.equals(this.request.ignoreNulls())); TableMetadata tableMetadata = tableSchema.tableMetadata(); WriteModification transformation = extension != null ? extension.beforeWrite(DefaultDynamoDbExtensionContext.builder() .items(itemMap) .operationContext(operationContext) .tableMetadata(tableMetadata) .build()) : null; if (transformation != null && transformation.transformedItem() != null) { itemMap = transformation.transformedItem(); } Collection<String> primaryKeys = tableSchema.tableMetadata().primaryKeys(); Map<String, AttributeValue> keyAttributeValues = itemMap.entrySet().stream() .filter(entry -> primaryKeys.contains(entry.getKey())) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); UpdateItemRequest.Builder requestBuilder = UpdateItemRequest.builder() .tableName(operationContext.tableName()) .key(keyAttributeValues) .returnValues(ReturnValue.ALL_NEW); Map<String, AttributeValue> filteredAttributeValues = itemMap.entrySet().stream() .filter(entry -> !primaryKeys.contains(entry.getKey())) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); requestBuilder = addExpressionsIfExist(transformation, filteredAttributeValues, requestBuilder); return requestBuilder.build(); }
Example 8
Source File: UpdateItemOperation.java From aws-sdk-java-v2 with Apache License 2.0 | 4 votes |
private UpdateItemRequest.Builder addExpressionsIfExist(WriteModification transformation, Map<String, AttributeValue> filteredAttributeValues, UpdateItemRequest.Builder requestBuilder) { Map<String, String> expressionNames = null; Map<String, AttributeValue> expressionValues = null; String conditionExpressionString = null; /* Add update expression for transformed non-key attributes if applicable */ if (!filteredAttributeValues.isEmpty()) { Expression fullUpdateExpression = generateUpdateExpression(filteredAttributeValues); expressionNames = fullUpdateExpression.expressionNames(); expressionValues = fullUpdateExpression.expressionValues(); requestBuilder = requestBuilder.updateExpression(fullUpdateExpression.expression()); } /* Merge in conditional expression from extension WriteModification if applicable */ if (transformation != null && transformation.additionalConditionalExpression() != null) { expressionNames = Expression.joinNames(expressionNames, transformation.additionalConditionalExpression().expressionNames()); expressionValues = Expression.joinValues(expressionValues, transformation.additionalConditionalExpression().expressionValues()); conditionExpressionString = transformation.additionalConditionalExpression().expression(); } /* Merge in conditional expression from specified 'conditionExpression' if applicable */ if (this.request.conditionExpression() != null) { expressionNames = Expression.joinNames(expressionNames, this.request.conditionExpression().expressionNames()); expressionValues = Expression.joinValues(expressionValues, this.request.conditionExpression().expressionValues()); conditionExpressionString = Expression.joinExpressions(conditionExpressionString, this.request.conditionExpression().expression(), " AND "); } // Avoiding adding empty collections that the low level SDK will propagate to DynamoDb where it causes error. if (expressionNames != null && !expressionNames.isEmpty()) { requestBuilder = requestBuilder.expressionAttributeNames(expressionNames); } if (expressionValues != null && !expressionValues.isEmpty()) { requestBuilder = requestBuilder.expressionAttributeValues(expressionValues); } return requestBuilder.conditionExpression(conditionExpressionString); }