@Test
public void batchWriteItemWithDeletionsTest() throws Exception{
this.vanillaBatchWriteItemTest();
BatchWriteItemRequest batchWriteItemRequest = new BatchWriteItemRequest();
BatchWriteItemResult result;
// Create a map for the requests in the batch
Map<String, List<WriteRequest>> requestItems = new HashMap<String, List<WriteRequest>>();
// Test: delete some items from database
List<WriteRequest> forumList = new ArrayList<WriteRequest>();
//Test case: Delete Requests
Map<String, AttributeValue> forumKey3c =
createItemKey(
hashKeyName1, new AttributeValue().withN("3"),
"range", new AttributeValue().withS("c"));
Map<String, AttributeValue> forumKey5e =
createItemKey(
hashKeyName1, new AttributeValue().withN("5"),
"range", new AttributeValue().withS("e"));
Map<String, AttributeValue> forumKey6f =
createItemKey(
hashKeyName1, new AttributeValue().withN("6"),
"range", new AttributeValue().withS("f"));
forumList.add(new WriteRequest().withDeleteRequest(new DeleteRequest().withKey(forumKey3c)));
forumList.add(new WriteRequest().withDeleteRequest(new DeleteRequest().withKey(forumKey5e)));
forumList.add(new WriteRequest().withDeleteRequest(new DeleteRequest().withKey(forumKey6f)));
//Test on Table 2
List<WriteRequest> forumListT2 = new ArrayList<WriteRequest>();
Map<String, AttributeValue> forumKeyT2 =
createItemKey(
hashKeyName2, new AttributeValue().withN("1"),
"range", new AttributeValue().withS("a"));
forumListT2.add(new WriteRequest().withDeleteRequest(new DeleteRequest().withKey(forumKeyT2)));
requestItems.put(tableName1, forumList);
requestItems.put(tableName2, forumListT2);
do {
System.out.println("Making the request.");
batchWriteItemRequest.withRequestItems(requestItems);
result = getClient().batchWriteItem(batchWriteItemRequest);
// Print consumed capacity units
for(ConsumedCapacity entry : result.getConsumedCapacity()) {
String tableName1 = entry.getTableName();
Double consumedCapacityUnits = entry.getCapacityUnits();
System.out.println("Consumed capacity units for table " + tableName1 + ": " + consumedCapacityUnits);
}
// Check for unprocessed keys which could happen if you exceed provisioned throughput
System.out.println("Unprocessed Put and Delete requests: \n" + result.getUnprocessedItems());
requestItems = result.getUnprocessedItems();
} while (result.getUnprocessedItems().size() > 0);
}