Example usage for com.amazonaws.services.dynamodbv2.model BatchGetItemRequest setRequestItems

List of usage examples for com.amazonaws.services.dynamodbv2.model BatchGetItemRequest setRequestItems

Introduction

In this page you can find the example usage for com.amazonaws.services.dynamodbv2.model BatchGetItemRequest setRequestItems.

Prototype


public void setRequestItems(java.util.Map<String, KeysAndAttributes> requestItems) 

Source Link

Document

A map of one or more table names and, for each table, a map that describes one or more items to retrieve from that table.

Usage

From source file:jp.classmethod.aws.dynamodb.DynamoDbRepository.java

License:Open Source License

private List<E> findAll(Iterable<AttributeValue> ids, boolean useParallelBatches) {
    Preconditions.checkNotNull(ids, "ids may not be null");
    List<AttributeValue> idList = Lists.newArrayList(ids);
    if (idList.isEmpty()) {
        return new ArrayList<>();
    }//from  www.j  a v  a 2 s. c  om
    List<Map<String, AttributeValue>> resultantItems = new ArrayList<>();

    StreamSupport.stream(Iterables.partition(idList, 25).spliterator(), useParallelBatches).forEach(inner -> {
        BatchGetItemRequest req = new BatchGetItemRequest();
        KeysAndAttributes keysAndAttributes = new KeysAndAttributes();
        keysAndAttributes.setConsistentRead(true);
        keysAndAttributes.setKeys(
                inner.stream().map(id -> ImmutableMap.of(hashKeyName, id)).collect(Collectors.toList()));
        String tableName = tableName();
        req.withRequestItems(ImmutableMap.of(tableName, keysAndAttributes));

        BatchGetItemResult result;

        do {
            try {
                result = dynamoDB.batchGetItem(req);
                resultantItems.addAll(result.getResponses().get(tableName));
                req.setRequestItems(result.getUnprocessedKeys());
            } catch (AmazonClientException e) {
                throw this.convertDynamoDBException(e, "batch get", null /*no conditions for reads*/);
            }
        } while (false == result.getUnprocessedKeys().isEmpty());
    });

    return resultantItems.stream().map(legacyItem -> Item.fromMap(InternalUtils.toSimpleMapValue(legacyItem)))
            .map(item -> convertItemToDomain(item)).collect(Collectors.toList());
}