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

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

Introduction

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

Prototype

public BatchGetItemRequest() 

Source Link

Document

Default constructor for BatchGetItemRequest object.

Usage

From source file:com.clicktravel.infrastructure.persistence.aws.dynamodb.DynamoDbTemplate.java

License:Apache License

public <T extends Item> Collection<T> executeQuery(final KeySetQuery query, final Class<T> itemClass) {
    final ItemConfiguration itemConfiguration = getItemConfiguration(itemClass);
    final Collection<Map<String, AttributeValue>> keys = new ArrayList<>();
    if (query.itemIds().size() == 0) {
        return new ArrayList<>();
    }//from w ww.j  a v a 2s .co  m
    final PrimaryKeyDefinition primaryKeyDefinition = itemConfiguration.primaryKeyDefinition();
    for (final ItemId itemId : query.itemIds()) {
        final Map<String, AttributeValue> key = new HashMap<>();
        key.put(primaryKeyDefinition.propertyName(), new AttributeValue(itemId.value()));
        if (CompoundPrimaryKeyDefinition.class.isAssignableFrom(primaryKeyDefinition.getClass())) {
            final CompoundPrimaryKeyDefinition compoundPrimaryKeyDefinition = (CompoundPrimaryKeyDefinition) primaryKeyDefinition;
            key.put(compoundPrimaryKeyDefinition.supportingPropertyName(),
                    new AttributeValue(itemId.supportingValue()));
        }
        keys.add(key);
    }
    final Map<String, KeysAndAttributes> requestItems = new HashMap<>();
    final KeysAndAttributes keysAndAttributes = new KeysAndAttributes();
    keysAndAttributes.setKeys(keys);
    final String tableName = databaseSchemaHolder.schemaName() + "." + itemConfiguration.tableName();
    requestItems.put(tableName, keysAndAttributes);
    final BatchGetItemRequest batchGetItemRequest = new BatchGetItemRequest().withRequestItems(requestItems);
    final BatchGetItemResult batchGetItemResult;
    try {
        batchGetItemResult = amazonDynamoDbClient.batchGetItem(batchGetItemRequest);
    } catch (final AmazonServiceException e) {
        throw new PersistenceResourceFailureException("Failure while attempting DynamoDb Batch Get Item", e);
    }
    final List<Map<String, AttributeValue>> itemAttributeMaps = batchGetItemResult.getResponses()
            .get(tableName);
    return marshallIntoObjects(itemClass, itemAttributeMaps);
}

From source file:com.dell.doradus.db.dynamodb.DDBRowIterator.java

License:Apache License

/**
 * Create a row iterator that returns specific rows and columns for the given table. A
 * series of batch requests are made if needed.
 * /*w  ww .ja v a  2  s .  c om*/
 * @param tableName Name for which to get rows for.
 * @param rowKeys   Collection of row keys to get.
 * @param colNames  Set of column names to fetch. If null or empty, all columns are
 *                  fetched.
 */
public DDBRowIterator(String tableName, Collection<String> rowKeys, Collection<String> colNames) {
    Iterator<String> rowKeyIter = rowKeys.iterator();
    KeysAndAttributes keysAndAttributes = makeKeyBatch(rowKeyIter);

    while (true) {
        BatchGetItemRequest batchRequest = new BatchGetItemRequest();
        batchRequest.addRequestItemsEntry(tableName, keysAndAttributes);

        BatchGetItemResult batchResult = DynamoDBService.instance().batchGetItem(batchRequest);
        Map<String, List<Map<String, AttributeValue>>> responseMap = batchResult.getResponses();

        List<Map<String, AttributeValue>> itemsList = responseMap.get(tableName);
        if (itemsList == null || itemsList.size() == 0) {
            break;
        }
        for (Map<String, AttributeValue> attributeMap : itemsList) {
            AttributeValue rowAttr = attributeMap.get(DynamoDBService.ROW_KEY_ATTR_NAME);
            m_rowList.add(new DDBRow(rowAttr.getS(), new DDBColumnIterator(attributeMap, colNames)));
        }

        Map<String, KeysAndAttributes> unprocessedKeys = batchResult.getUnprocessedKeys();
        if (unprocessedKeys != null && unprocessedKeys.containsKey(tableName)) {
            keysAndAttributes = unprocessedKeys.get(tableName);
        } else if (rowKeyIter.hasNext()) {
            keysAndAttributes = makeKeyBatch(rowKeyIter);
        } else {
            break;
        }
    }
}

From source file:com.dell.doradus.db.dynamodb.DDBRowIterator.java

License:Apache License

/**
 * Create a row iterator that returns specific rows and column ranges for the given
 * table. A series of batch item requests are made if needed.
 * /*from  w w w.j a  v  a  2 s  .c  o  m*/
 * @param tableName Name for which to get rows for.
 * @param rowKeys   Collection of row keys to get.
 * @param startCol  If non-null/empty, only column names greater than or equal to this
 *                  name are captured.
 * @param endCol    If non-null/empty, only column names less than or equal to this
 *                  name are captured.
 */
public DDBRowIterator(String tableName, Collection<String> rowKeys, String startCol, String endCol) {
    Iterator<String> rowKeyIter = rowKeys.iterator();
    KeysAndAttributes keysAndAttributes = makeKeyBatch(rowKeyIter);
    while (true) {
        BatchGetItemRequest batchRequest = new BatchGetItemRequest();
        batchRequest.addRequestItemsEntry(tableName, keysAndAttributes);

        BatchGetItemResult batchResult = DynamoDBService.instance().batchGetItem(batchRequest);
        Map<String, List<Map<String, AttributeValue>>> responseMap = batchResult.getResponses();

        List<Map<String, AttributeValue>> itemsList = responseMap.get(tableName);
        if (itemsList == null || itemsList.size() == 0) {
            break;
        }
        for (Map<String, AttributeValue> attributeMap : itemsList) {
            AttributeValue rowAttr = attributeMap.get(DynamoDBService.ROW_KEY_ATTR_NAME);
            m_rowList.add(new DDBRow(rowAttr.getS(), new DDBColumnIterator(attributeMap, startCol, endCol)));
        }

        Map<String, KeysAndAttributes> unprocessedKeys = batchResult.getUnprocessedKeys();
        if (unprocessedKeys != null && unprocessedKeys.containsKey(tableName)) {
            keysAndAttributes = unprocessedKeys.get(tableName);
        } else if (rowKeyIter.hasNext()) {
            keysAndAttributes = makeKeyBatch(rowKeyIter);
        } else {
            break;
        }
    }
}

From source file:com.erudika.para.persistence.AWSDynamoDAO.java

License:Apache License

private <P extends ParaObject> void batchGet(Map<String, KeysAndAttributes> kna, Map<String, P> results) {
    if (kna == null || kna.isEmpty() || results == null) {
        return;/*from   www .  ja  va2 s . c om*/
    }
    try {
        BatchGetItemResult result = client().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 {}", result.getUnprocessedKeys().size());
            batchGet(result.getUnprocessedKeys(), results);
        }
    } catch (Exception e) {
        logger.error(null, e);
    }
}

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<>();
    }/* ww w .j ava  2s  . c  o m*/
    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());
}

From source file:org.iternine.jeppetto.dao.dynamodb.DynamoDBQueryModelDAO.java

License:Apache License

@Override
public Iterable<T> findByIds(ID... ids) throws JeppettoException {
    Collection<Map<String, AttributeValue>> keys = new ArrayList<Map<String, AttributeValue>>();

    for (ID id : ids) {
        keys.add(getKeyFrom(id));/*from   w w w .j  a v a2  s.co m*/
    }

    KeysAndAttributes keysAndAttributes = new KeysAndAttributes().withKeys(keys);

    keysAndAttributes.setConsistentRead(consistentRead);
    keysAndAttributes.setProjectionExpression(projectionExpression);

    if (!projectionExpressionNames.isEmpty()) {
        keysAndAttributes.setExpressionAttributeNames(projectionExpressionNames);
    }

    BatchGetItemRequest batchGetItemRequest = new BatchGetItemRequest()
            .withRequestItems(Collections.singletonMap(tableName, keysAndAttributes));

    return new BatchGetIterable<T>(dynamoDB, persistableEnhancer, batchGetItemRequest, tableName);
}