use of software.amazon.awssdk.services.dynamodb.model.UpdateItemResponse in project aws-sdk-java-v2 by aws.
the class DynamoServiceIntegrationTest method testServiceOperations.
/**
* Tests that we can call each service operation to create and describe
* tables, put, update and delete data, and query.
*/
@Test
public void testServiceOperations() throws Exception {
// Describe all tables
ListTablesResponse describeTablesResult = dynamo.listTables(ListTablesRequest.builder().build());
// Describe our new table
DescribeTableRequest describeTablesRequest = DescribeTableRequest.builder().tableName(tableName).build();
TableDescription tableDescription = dynamo.describeTable(describeTablesRequest).table();
assertEquals(tableName, tableDescription.tableName());
assertNotNull(tableDescription.tableStatus());
assertEquals(HASH_KEY_NAME, tableDescription.keySchema().get(0).attributeName());
assertEquals(KeyType.HASH, tableDescription.keySchema().get(0).keyType());
assertNotNull(tableDescription.provisionedThroughput().numberOfDecreasesToday());
assertEquals(READ_CAPACITY, tableDescription.provisionedThroughput().readCapacityUnits());
assertEquals(WRITE_CAPACITY, tableDescription.provisionedThroughput().writeCapacityUnits());
// Add some data
int contentLength = 1 * 1024;
Set<SdkBytes> byteBufferSet = new HashSet<SdkBytes>();
byteBufferSet.add(SdkBytes.fromByteArray(generateByteArray(contentLength)));
byteBufferSet.add(SdkBytes.fromByteArray(generateByteArray(contentLength + 1)));
Map<String, AttributeValue> item = new HashMap<String, AttributeValue>();
item.put(HASH_KEY_NAME, AttributeValue.builder().s("bar").build());
item.put("age", AttributeValue.builder().n("30").build());
item.put("bar", AttributeValue.builder().s("" + System.currentTimeMillis()).build());
item.put("foos", AttributeValue.builder().ss("bleh", "blah").build());
item.put("S", AttributeValue.builder().ss("ONE", "TWO").build());
item.put("blob", AttributeValue.builder().b(SdkBytes.fromByteArray(generateByteArray(contentLength))).build());
item.put("blobs", AttributeValue.builder().bs(SdkBytes.fromByteArray(generateByteArray(contentLength)), SdkBytes.fromByteArray(generateByteArray(contentLength + 1))).build());
item.put("BS", AttributeValue.builder().bs(byteBufferSet).build());
PutItemRequest putItemRequest = PutItemRequest.builder().tableName(tableName).item(item).returnValues(ReturnValue.ALL_OLD.toString()).build();
PutItemResponse putItemResult = dynamo.putItem(putItemRequest);
// Get our new item
GetItemResponse itemResult = dynamo.getItem(GetItemRequest.builder().tableName(tableName).key(mapKey(HASH_KEY_NAME, AttributeValue.builder().s("bar").build())).consistentRead(true).build());
assertNotNull(itemResult.item().get("S").ss());
assertEquals(2, itemResult.item().get("S").ss().size());
assertTrue(itemResult.item().get("S").ss().contains("ONE"));
assertTrue(itemResult.item().get("S").ss().contains("TWO"));
assertEquals("30", itemResult.item().get("age").n());
assertNotNull(itemResult.item().get("bar").s());
assertNotNull(itemResult.item().get("blob").b());
assertTrue(itemResult.item().get("blob").b().equals(SdkBytes.fromByteArray(generateByteArray(contentLength))));
assertNotNull(itemResult.item().get("blobs").bs());
assertEquals(2, itemResult.item().get("blobs").bs().size());
assertTrue(itemResult.item().get("blobs").bs().contains(SdkBytes.fromByteArray(generateByteArray(contentLength))));
assertTrue(itemResult.item().get("blobs").bs().contains(SdkBytes.fromByteArray(generateByteArray(contentLength + 1))));
assertNotNull(itemResult.item().get("BS").bs());
assertEquals(2, itemResult.item().get("BS").bs().size());
assertTrue(itemResult.item().get("BS").bs().contains(SdkBytes.fromByteArray(generateByteArray(contentLength))));
assertTrue(itemResult.item().get("BS").bs().contains(SdkBytes.fromByteArray(generateByteArray(contentLength + 1))));
// Pause to try and deal with ProvisionedThroughputExceededExceptions
Thread.sleep(1000 * 5);
// Add some data into the table with binary hash key
ByteBuffer byteBuffer = ByteBuffer.allocate(contentLength * 2);
byteBuffer.put(generateByteArray(contentLength));
byteBuffer.flip();
item = new HashMap<String, AttributeValue>();
item.put(HASH_KEY_NAME, AttributeValue.builder().b(SdkBytes.fromByteBuffer(byteBuffer)).build());
// Reuse the byteBuffer
item.put("blob", AttributeValue.builder().b(SdkBytes.fromByteBuffer(byteBuffer)).build());
item.put("blobs", AttributeValue.builder().bs(SdkBytes.fromByteArray(generateByteArray(contentLength)), SdkBytes.fromByteArray(generateByteArray(contentLength + 1))).build());
// Reuse the byteBufferSet
item.put("BS", AttributeValue.builder().bs(byteBufferSet).build());
putItemRequest = PutItemRequest.builder().tableName(binaryKeyTableName).item(item).returnValues(ReturnValue.ALL_OLD.toString()).build();
dynamo.putItem(putItemRequest);
// Get our new item
itemResult = dynamo.getItem(GetItemRequest.builder().tableName(binaryKeyTableName).key(mapKey(HASH_KEY_NAME, AttributeValue.builder().b(SdkBytes.fromByteBuffer(byteBuffer)).build())).consistentRead(true).build());
assertNotNull(itemResult.item().get("blob").b());
assertEquals(itemResult.item().get("blob").b(), SdkBytes.fromByteArray(generateByteArray(contentLength)));
assertNotNull(itemResult.item().get("blobs").bs());
assertEquals(2, itemResult.item().get("blobs").bs().size());
assertTrue(itemResult.item().get("blobs").bs().contains(SdkBytes.fromByteArray(generateByteArray(contentLength))));
assertTrue(itemResult.item().get("blobs").bs().contains(SdkBytes.fromByteArray(generateByteArray(contentLength + 1))));
assertNotNull(itemResult.item().get("BS").bs());
assertEquals(2, itemResult.item().get("BS").bs().size());
assertTrue(itemResult.item().get("BS").bs().contains(SdkBytes.fromByteArray(generateByteArray(contentLength))));
assertTrue(itemResult.item().get("BS").bs().contains(SdkBytes.fromByteArray(generateByteArray(contentLength + 1))));
// Pause to try and deal with ProvisionedThroughputExceededExceptions
Thread.sleep(1000 * 5);
// Load some random data
System.out.println("Loading data...");
Random random = new Random();
for (int i = 0; i < 50; i++) {
item = new HashMap<String, AttributeValue>();
item.put(HASH_KEY_NAME, AttributeValue.builder().s("bar-" + System.currentTimeMillis()).build());
item.put("age", AttributeValue.builder().n(Integer.toString(random.nextInt(100) + 30)).build());
item.put("bar", AttributeValue.builder().s("" + System.currentTimeMillis()).build());
item.put("foos", AttributeValue.builder().ss("bleh", "blah").build());
dynamo.putItem(PutItemRequest.builder().tableName(tableName).item(item).returnValues(ReturnValue.ALL_OLD.toString()).build());
}
// Update an item
Map<String, AttributeValueUpdate> itemUpdates = new HashMap<String, AttributeValueUpdate>();
itemUpdates.put("1", AttributeValueUpdate.builder().value(AttributeValue.builder().s("¢").build()).action(AttributeAction.PUT.toString()).build());
itemUpdates.put("foos", AttributeValueUpdate.builder().value(AttributeValue.builder().ss("foo").build()).action(AttributeAction.PUT.toString()).build());
itemUpdates.put("S", AttributeValueUpdate.builder().value(AttributeValue.builder().ss("THREE").build()).action(AttributeAction.ADD.toString()).build());
itemUpdates.put("age", AttributeValueUpdate.builder().value(AttributeValue.builder().n("10").build()).action(AttributeAction.ADD.toString()).build());
itemUpdates.put("blob", AttributeValueUpdate.builder().value(AttributeValue.builder().b(SdkBytes.fromByteArray(generateByteArray(contentLength + 1))).build()).action(AttributeAction.PUT.toString()).build());
itemUpdates.put("blobs", AttributeValueUpdate.builder().value(AttributeValue.builder().bs(SdkBytes.fromByteArray(generateByteArray(contentLength))).build()).action(AttributeAction.PUT.toString()).build());
UpdateItemRequest updateItemRequest = UpdateItemRequest.builder().tableName(tableName).key(mapKey(HASH_KEY_NAME, AttributeValue.builder().s("bar").build())).attributeUpdates(itemUpdates).returnValues("ALL_NEW").build();
UpdateItemResponse updateItemResult = dynamo.updateItem(updateItemRequest);
assertEquals("¢", updateItemResult.attributes().get("1").s());
assertEquals(1, updateItemResult.attributes().get("foos").ss().size());
assertTrue(updateItemResult.attributes().get("foos").ss().contains("foo"));
assertEquals(3, updateItemResult.attributes().get("S").ss().size());
assertTrue(updateItemResult.attributes().get("S").ss().contains("ONE"));
assertTrue(updateItemResult.attributes().get("S").ss().contains("TWO"));
assertTrue(updateItemResult.attributes().get("S").ss().contains("THREE"));
assertEquals(Integer.toString(30 + 10), updateItemResult.attributes().get("age").n());
assertEquals(updateItemResult.attributes().get("blob").b(), SdkBytes.fromByteArray(generateByteArray(contentLength + 1)));
assertEquals(1, updateItemResult.attributes().get("blobs").bs().size());
assertTrue(updateItemResult.attributes().get("blobs").bs().contains(SdkBytes.fromByteArray(generateByteArray(contentLength))));
itemUpdates.clear();
itemUpdates.put("age", AttributeValueUpdate.builder().value(AttributeValue.builder().n("30").build()).action(AttributeAction.PUT.toString()).build());
itemUpdates.put("blobs", AttributeValueUpdate.builder().value(AttributeValue.builder().bs(SdkBytes.fromByteArray(generateByteArray(contentLength + 1))).build()).action(AttributeAction.ADD.toString()).build());
updateItemRequest = UpdateItemRequest.builder().tableName(tableName).key(mapKey(HASH_KEY_NAME, AttributeValue.builder().s("bar").build())).attributeUpdates(itemUpdates).returnValues("ALL_NEW").build();
updateItemResult = dynamo.updateItem(updateItemRequest);
assertEquals("30", updateItemResult.attributes().get("age").n());
assertEquals(2, updateItemResult.attributes().get("blobs").bs().size());
assertTrue(updateItemResult.attributes().get("blobs").bs().contains(SdkBytes.fromByteArray(generateByteArray(contentLength))));
assertTrue(updateItemResult.attributes().get("blobs").bs().contains(SdkBytes.fromByteArray(generateByteArray(contentLength + 1))));
// Get an item that doesn't exist.
GetItemRequest itemsRequest = GetItemRequest.builder().tableName(tableName).key(mapKey(HASH_KEY_NAME, AttributeValue.builder().s("3").build())).consistentRead(true).build();
GetItemResponse itemsResult = dynamo.getItem(itemsRequest);
assertTrue(itemsResult.item() instanceof SdkAutoConstructMap);
// Get an item that doesn't have any attributes,
itemsRequest = GetItemRequest.builder().tableName(tableName).key(mapKey(HASH_KEY_NAME, AttributeValue.builder().s("bar").build())).consistentRead(true).attributesToGet("non-existent-attribute").build();
itemsResult = dynamo.getItem(itemsRequest);
assertEquals(0, itemsResult.item().size());
// Scan data
ScanRequest scanRequest = ScanRequest.builder().tableName(tableName).attributesToGet(HASH_KEY_NAME).build();
ScanResponse scanResult = dynamo.scan(scanRequest);
assertTrue(scanResult.count() > 0);
assertTrue(scanResult.scannedCount() > 0);
// Try a more advanced Scan query and run it a few times for performance metrics
System.out.println("Testing Scan...");
for (int i = 0; i < 10; i++) {
HashMap<String, Condition> scanFilter = new HashMap<String, Condition>();
scanFilter.put("age", Condition.builder().attributeValueList(AttributeValue.builder().n("40").build()).comparisonOperator(ComparisonOperator.GT.toString()).build());
scanRequest = ScanRequest.builder().tableName(tableName).scanFilter(scanFilter).build();
scanResult = dynamo.scan(scanRequest);
}
// Batch write
HashMap<String, List<WriteRequest>> requestItems = new HashMap<String, List<WriteRequest>>();
List<WriteRequest> writeRequests = new ArrayList<WriteRequest>();
HashMap<String, AttributeValue> writeAttributes = new HashMap<String, AttributeValue>();
writeAttributes.put(HASH_KEY_NAME, AttributeValue.builder().s("" + System.currentTimeMillis()).build());
writeAttributes.put("bar", AttributeValue.builder().s("" + System.currentTimeMillis()).build());
writeRequests.add(WriteRequest.builder().putRequest(PutRequest.builder().item(writeAttributes).build()).build());
writeRequests.add(WriteRequest.builder().deleteRequest(DeleteRequest.builder().key(mapKey(HASH_KEY_NAME, AttributeValue.builder().s("toDelete").build())).build()).build());
requestItems.put(tableName, writeRequests);
BatchWriteItemResponse batchWriteItem = dynamo.batchWriteItem(BatchWriteItemRequest.builder().requestItems(requestItems).build());
// assertNotNull(batchWriteItem.itemCollectionMetrics());
// assertEquals(1, batchWriteItem.itemCollectionMetrics().size());
// assertEquals(tableName, batchWriteItem.itemCollectionMetrics().entrySet().iterator().next().get);
// assertNotNull(tableName, batchWriteItem.getResponses().iterator().next().getCapacityUnits());
assertNotNull(batchWriteItem.unprocessedItems());
assertTrue(batchWriteItem.unprocessedItems().isEmpty());
// Delete some data
DeleteItemRequest deleteItemRequest = DeleteItemRequest.builder().tableName(tableName).key(mapKey(HASH_KEY_NAME, AttributeValue.builder().s("jeep").build())).returnValues(ReturnValue.ALL_OLD.toString()).build();
DeleteItemResponse deleteItemResult = dynamo.deleteItem(deleteItemRequest);
// Delete our table
DeleteTableResponse deleteTable = dynamo.deleteTable(DeleteTableRequest.builder().tableName(tableName).build());
}
use of software.amazon.awssdk.services.dynamodb.model.UpdateItemResponse in project aws-sdk-java-v2 by aws.
the class UpdateItemOperationTest method getServiceCall_makesTheRightCallAndReturnsResponse.
@Test
public void getServiceCall_makesTheRightCallAndReturnsResponse() {
FakeItem item = createUniqueFakeItem();
UpdateItemOperation<FakeItem> updateItemOperation = UpdateItemOperation.create(UpdateItemEnhancedRequest.builder(FakeItem.class).item(item).build());
UpdateItemRequest updateItemRequest = UpdateItemRequest.builder().tableName(TABLE_NAME).build();
UpdateItemResponse expectedResponse = UpdateItemResponse.builder().build();
when(mockDynamoDbClient.updateItem(any(UpdateItemRequest.class))).thenReturn(expectedResponse);
UpdateItemResponse response = updateItemOperation.serviceCall(mockDynamoDbClient).apply(updateItemRequest);
assertThat(response, sameInstance(expectedResponse));
verify(mockDynamoDbClient).updateItem(updateItemRequest);
}
use of software.amazon.awssdk.services.dynamodb.model.UpdateItemResponse in project aws-sdk-java-v2 by aws.
the class EmptyBinaryTest method updateEmptyBytesWithCondition.
@Test
public void updateEmptyBytesWithCondition() {
Map<String, AttributeValue> expectedItemMap = new HashMap<>();
expectedItemMap.put("id", AttributeValue.builder().s("id123").build());
expectedItemMap.put("b", EMPTY_BINARY);
TestBean testBean = new TestBean();
testBean.setId("id123");
testBean.setB(EMPTY_BYTES);
UpdateItemResponse response = UpdateItemResponse.builder().attributes(expectedItemMap).build();
when(mockDynamoDbClient.updateItem(any(UpdateItemRequest.class))).thenReturn(response);
Expression conditionExpression = Expression.builder().expression("#attr = :val").expressionNames(singletonMap("#attr", "b")).expressionValues(singletonMap(":val", EMPTY_BINARY)).build();
TestBean result = dynamoDbTable.updateItem(r -> r.item(testBean).conditionExpression(conditionExpression));
Map<String, String> expectedExpressionAttributeNames = new HashMap<>();
expectedExpressionAttributeNames.put("#AMZN_MAPPED_b", "b");
expectedExpressionAttributeNames.put("#attr", "b");
Map<String, AttributeValue> expectedExpressionAttributeValues = new HashMap<>();
expectedExpressionAttributeValues.put(":AMZN_MAPPED_b", EMPTY_BINARY);
expectedExpressionAttributeValues.put(":val", EMPTY_BINARY);
Map<String, AttributeValue> expectedKeyMap = new HashMap<>();
expectedKeyMap.put("id", AttributeValue.builder().s("id123").build());
UpdateItemRequest expectedRequest = UpdateItemRequest.builder().tableName(TABLE_NAME).key(expectedKeyMap).returnValues(ReturnValue.ALL_NEW).updateExpression("SET #AMZN_MAPPED_b = :AMZN_MAPPED_b").conditionExpression("#attr = :val").expressionAttributeNames(expectedExpressionAttributeNames).expressionAttributeValues(expectedExpressionAttributeValues).build();
verify(mockDynamoDbClient).updateItem(expectedRequest);
assertThat(result.getId()).isEqualTo("id123");
assertThat(result.getB()).isEqualTo(EMPTY_BYTES);
}
use of software.amazon.awssdk.services.dynamodb.model.UpdateItemResponse in project aws-sdk-java-v2 by aws.
the class EmptyStringTest method updateEmptyStringWithCondition.
@Test
public void updateEmptyStringWithCondition() {
Map<String, AttributeValue> expectedItemMap = new HashMap<>();
expectedItemMap.put("id", AttributeValue.builder().s("id123").build());
expectedItemMap.put("s", EMPTY_STRING);
TestBean testBean = new TestBean();
testBean.setId("id123");
testBean.setS("");
UpdateItemResponse response = UpdateItemResponse.builder().attributes(expectedItemMap).build();
when(mockDynamoDbClient.updateItem(any(UpdateItemRequest.class))).thenReturn(response);
Expression conditionExpression = Expression.builder().expression("#attr = :val").expressionNames(singletonMap("#attr", "s")).expressionValues(singletonMap(":val", EMPTY_STRING)).build();
TestBean result = dynamoDbTable.updateItem(r -> r.item(testBean).conditionExpression(conditionExpression));
Map<String, String> expectedExpressionAttributeNames = new HashMap<>();
expectedExpressionAttributeNames.put("#AMZN_MAPPED_s", "s");
expectedExpressionAttributeNames.put("#attr", "s");
Map<String, AttributeValue> expectedExpressionAttributeValues = new HashMap<>();
expectedExpressionAttributeValues.put(":AMZN_MAPPED_s", EMPTY_STRING);
expectedExpressionAttributeValues.put(":val", EMPTY_STRING);
Map<String, AttributeValue> expectedKeyMap = new HashMap<>();
expectedKeyMap.put("id", AttributeValue.builder().s("id123").build());
UpdateItemRequest expectedRequest = UpdateItemRequest.builder().tableName(TABLE_NAME).key(expectedKeyMap).returnValues(ReturnValue.ALL_NEW).updateExpression("SET #AMZN_MAPPED_s = :AMZN_MAPPED_s").conditionExpression("#attr = :val").expressionAttributeNames(expectedExpressionAttributeNames).expressionAttributeValues(expectedExpressionAttributeValues).build();
verify(mockDynamoDbClient).updateItem(expectedRequest);
assertThat(result.getId()).isEqualTo("id123");
assertThat(result.getS()).isEmpty();
}
use of software.amazon.awssdk.services.dynamodb.model.UpdateItemResponse in project micronaut-aws-sdk by agorapulse.
the class DefaultUpdateBuilder method update.
@Override
@SuppressWarnings("unchecked")
public R update(DynamoDbTable<T> mapper, DynamoDbClient client, AttributeConversionHelper attributeConversionHelper, ApplicationEventPublisher publisher) {
UpdateItemRequest request = resolveRequest(mapper, attributeConversionHelper);
T keyItem = mapper.tableSchema().mapToItem(request.key());
publisher.publishEvent(DynamoDbEvent.preUpdate(keyItem));
UpdateItemResponse result = client.updateItem(request);
Map<String, AttributeValue> attributes = result.attributes();
if (attributes == null || attributes.isEmpty()) {
return null;
}
if (ReturnValue.NONE.equals(__returnValue)) {
return null;
}
T item = mapper.tableSchema().mapToItem(attributes);
publisher.publishEvent(DynamoDbEvent.postUpdate(item));
return (R) __returnValueMapper.apply(item);
}
Aggregations