private static void saveItem(List<EventTracker> items) {
List<WriteRequest> wrList = new ArrayList<>();
try {
for (EventTracker item : items) {
WriteRequest wreqItem;
wreqItem = getWriteRequest(item);
wrList.add(wreqItem);
}
try {
BatchWriteItemResult batchWriteItemResult = new BatchWriteItemResult();
do {
BatchWriteItemRequest batchWriteItemRequest = new BatchWriteItemRequest();
batchWriteItemRequest.addRequestItemsEntry(forumTableName, wrList);// setRequestItems(writeRequestitems);
batchWriteItemResult = amazonDynamoDB.batchWriteItem(batchWriteItemRequest);
// Check for unprocessed keys which could happen if you
// exceed
// provisioned throughput
Map<String, List<WriteRequest>> unprocessedItems = batchWriteItemResult.getUnprocessedItems();
if (unprocessedItems.size() == 0) {
System.out.println("No unprocessed items found");
} else {
System.out.println("Sleeping for: " + ThreadLocalRandom.current().nextInt(500, 999 + 1));
Thread.sleep(ThreadLocalRandom.current().nextInt(500, 999 + 1));
wrList = unprocessedItems.get(forumTableName);
System.out.println("Retrieving the unprocessed items");
}
} while (batchWriteItemResult.getUnprocessedItems().size() > 0);
} catch (Exception e) {
System.err.println("Failed to retrieve items: ");
e.printStackTrace(System.err);
}
} catch (Exception e) {
}
}
This is what we need to know if we want to make use of batch operations to put data to a dynamo db table. Lets see the steps that need to be followed to accomplish this. In this example, we are trying to persist a list of EventTracker data, which is my POJO.
The below code will show how we create write requests.