Source:
public int batchInsert(final List<DeviceData> deviceDataList) { final Map<String, WriteRequest> writeRequestMap = Maps.newHashMap(); for (final DeviceData data: deviceDataList) { final HashMap<String, AttributeValue> item = deviceDataToAttributeMap(data); final String hashAndRangeKey = item.get(AttributeNames.DEVICE_ID).getN() + item.get(RANGE_KEY_NAME).getS(); final WriteRequest request = new WriteRequest().withPutRequest(new PutRequest().withItem(item)); writeRequestMap.put(hashAndRangeKey, request); } Map<String, List<WriteRequest>> requestItems = Maps.newHashMapWithExpectedSize(1); requestItems.put(this.tableName, Lists.newArrayList(writeRequestMap.values())); int numAttempts = 0; do { numAttempts++; final BatchWriteItemRequest batchWriteItemRequest = new BatchWriteItemRequest().withRequestItems(requestItems); final BatchWriteItemResult result = this.dynamoDBClient.batchWriteItem(batchWriteItemRequest); requestItems = result.getUnprocessedItems(); if (!requestItems.isEmpty()) { try { long sleepMillis = (long) Math.pow(2, numAttempts) * 50; LOGGER.debug("Throttled by DynamoDB, sleeping for ms.", sleepMillis); Thread.sleep(sleepMillis); } catch (InterruptedException e) { LOGGER.error("Interrupted while attempting exponential backoff."); } } } while (!requestItems.isEmpty() && (numAttempts < MAX_BATCH_WRITE_ATTEMPTS)); if (!requestItems.isEmpty()) { LOGGER.warn("Exceeded attempts to batch write to Dynamo. items left over.", MAX_BATCH_WRITE_ATTEMPTS, requestItems.get(tableName).size()); return writeRequestMap.size() - requestItems.get(tableName).size(); } return writeRequestMap.size(); }

Target:
public int batchInsert(final List<DeviceData> deviceDataList) { final Map<String, WriteRequest> writeRequestMap = Maps.newHashMap(); for (final DeviceData data: deviceDataList) { final Map<String, AttributeValue> item = deviceDataToAttributeMap(data); final String hashAndRangeKey = item.get(AttributeNames.DEVICE_ID).getN() + item.get(RANGE_KEY_NAME).getS(); final WriteRequest request = new WriteRequest().withPutRequest(new PutRequest().withItem(item)); writeRequestMap.put(hashAndRangeKey, request); } Map<String, List<WriteRequest>> requestItems = Maps.newHashMapWithExpectedSize(1); requestItems.put(this.tableName, Lists.newArrayList(writeRequestMap.values())); int numAttempts = 0; do { numAttempts++; final BatchWriteItemRequest batchWriteItemRequest = new BatchWriteItemRequest().withRequestItems(requestItems); final BatchWriteItemResult result = this.dynamoDBClient.batchWriteItem(batchWriteItemRequest); requestItems = result.getUnprocessedItems(); if (!requestItems.isEmpty()) { try { long sleepMillis = (long) Math.pow(2, numAttempts) * 50; LOGGER.debug("Throttled by DynamoDB, sleeping for ms.", sleepMillis); Thread.sleep(sleepMillis); } catch (InterruptedException e) { LOGGER.error("Interrupted while attempting exponential backoff."); } } } while (!requestItems.isEmpty() && (numAttempts < MAX_BATCH_WRITE_ATTEMPTS)); if (!requestItems.isEmpty()) { LOGGER.warn("Exceeded attempts to batch write to Dynamo. items left over.", MAX_BATCH_WRITE_ATTEMPTS, requestItems.get(tableName).size()); return writeRequestMap.size() - requestItems.get(tableName).size(); } return writeRequestMap.size(); }