@SuppressWarnings("unchecked") @Override public BatchGetItemResult batchGetItem(BatchGetItemRequest batchGetItemRequest) { this.batchGetItemRequest = batchGetItemRequest; Map<String, List<Map<String, AttributeValue>>> responseMap = new HashMap<String, List<Map<String, AttributeValue>>>(); List<Map<String, AttributeValue>> p = new ArrayList<Map<String, AttributeValue>>(); p.add(getAttributes()); responseMap.put("DOMAIN1", p); Map<String, AttributeValue> keysMap = new HashMap<String, AttributeValue>(); keysMap.put("1", new AttributeValue("UNPROCESSED_KEY")); Map<String, KeysAndAttributes> unprocessedKeys = new HashMap<String, KeysAndAttributes>(); unprocessedKeys.put("DOMAIN1", new KeysAndAttributes().withKeys(keysMap)); return new BatchGetItemResult() .withResponses(responseMap) .withUnprocessedKeys(unprocessedKeys); }
private List<Map<String, AttributeValue>> batchGetDataByKeys( final String tableName, final KeysAndAttributes keys) { Map<String, KeysAndAttributes> requestMap = new HashMap<>(); keys.setConsistentRead(true); requestMap.put(tableName, keys); BatchGetItemResult result = null; try { result = dynamoClient.batchGetItem(new BatchGetItemRequest( requestMap)); } catch (AmazonServiceException e) { LOG.error(e); throw e; } return result.getResponses().get(this.tableName); }
@Override public void execute() { BatchGetItemResult result = ddbClient.batchGetItem( new BatchGetItemRequest().withRequestItems(determineBatchItems())); Map tmp = new HashMap<>(); tmp.put(DdbConstants.BATCH_RESPONSE, result.getResponses()); tmp.put(DdbConstants.UNPROCESSED_KEYS, result.getUnprocessedKeys()); addToResults(tmp); }
@Test public void test_batchGetItem_WithAllParameters() throws Exception { String TEST_ATTRIBUTE_2 = "Attribute2"; String TEST_ATTRIBUTE_VALUE_2 = "AttributeValue2"; createTable(); putItem(TEST_ATTRIBUTE, TEST_ATTRIBUTE_VALUE); putItem(TEST_ATTRIBUTE_2, TEST_ATTRIBUTE_VALUE_2); List<Map<String, AttributeValue>> keys = new ArrayList<Map<String, AttributeValue>>(); Map<String, AttributeValue> key1 = new HashMap<String, AttributeValue>(); key1.put(TEST_ATTRIBUTE, new AttributeValue() .withS(TEST_ATTRIBUTE_VALUE)); keys.add(key1); Map<String, AttributeValue> key2 = new HashMap<String, AttributeValue>(); key2.put(TEST_ATTRIBUTE_2, new AttributeValue() .withS(TEST_ATTRIBUTE_VALUE_2)); keys.add(key2); Map<String, KeysAndAttributes> requestItems = new HashMap<String, KeysAndAttributes>(); requestItems.put(TEST_TABLE_NAME, new KeysAndAttributes() .withKeys(keys)); String returnConsumedCapacity = ""; BatchGetItemResult result = dynamoDb.batchGetItem(requestItems,returnConsumedCapacity); String tableName = result.getResponses().keySet().toArray(new String[1])[0]; List<Map<String, AttributeValue>> items = result.getResponses().get(tableName); AttributeValue value1 = items.get(0).get(TEST_ATTRIBUTE); AttributeValue value2 = items.get(1).get(TEST_ATTRIBUTE_2); assertThat(tableName, equalTo(TEST_TABLE_NAME)); assertThat(items.size(), equalTo(keys.size())); assertThat(value1.getS(), equalTo(TEST_ATTRIBUTE_VALUE)); assertThat(value2.getS(), equalTo(TEST_ATTRIBUTE_VALUE_2)); }
/** * Reads multiple items from DynamoDB, in batch. * @param <P> type of object * @param kna a map of row key->data * @param results a map of ID->ParaObject */ protected static <P extends ParaObject> void batchGet(Map<String, KeysAndAttributes> kna, Map<String, P> results) { if (kna == null || kna.isEmpty() || results == null) { return; } try { BatchGetItemResult result = getClient().batchGetItem(new BatchGetItemRequest(). withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL).withRequestItems(kna)); if (result == null) { return; } List<Map<String, AttributeValue>> res = result.getResponses().get(kna.keySet().iterator().next()); for (Map<String, AttributeValue> item : res) { P obj = fromRow(item); if (obj != null) { results.put(obj.getId(), obj); } } logger.debug("batchGet(): total {}, cc {}", res.size(), result.getConsumedCapacity()); if (result.getUnprocessedKeys() != null && !result.getUnprocessedKeys().isEmpty()) { Thread.sleep(1000); logger.warn("{} UNPROCESSED read requests!", result.getUnprocessedKeys().size()); batchGet(result.getUnprocessedKeys(), results); } } catch (Exception e) { logger.error(null, e); } }
public <T> List<T> batchGet(String tableName, KeysAndAttributes keysAndAttributes, final ObjectExtractor<T> extractor) throws EmptyResultDataAccessException { Assert.notNull(tableName, "Table must not be null"); Assert.notNull(extractor, "ObjectExtractor must not be null"); if (logger.isDebugEnabled()) { logger.debug("Executing batch get on " + tableName + " for " + keysAndAttributes.toString()); } List<T> results = new ArrayList<T>(keysAndAttributes.getKeys().size()); Map<String, KeysAndAttributes> unprocessedKeys = Collections.singletonMap(tableName, keysAndAttributes); while (unprocessedKeys.size() > 0) { BatchGetItemResult result = client.batchGetItem(unprocessedKeys); List<Map<String, AttributeValue>> items = result.getResponses().get(tableName); if (items != null) { for (Map<String, AttributeValue> item : items) { results.add(extractor.extract(item)); } } unprocessedKeys = result.getUnprocessedKeys(); } if (results.size() == 0) { throw new EmptyResultDataAccessException("No results found in " + tableName + "for " + keysAndAttributes.toString()); } return results; }