List of usage examples for com.amazonaws.services.dynamodbv2.model KeysAndAttributes setKeys
public void setKeys(java.util.Collection<java.util.Map<String, AttributeValue>> keys)
The primary key attribute values that define the items and the attributes associated with the items.
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<>(); }/*w ww . j a va2s.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
private KeysAndAttributes makeKeyBatch(Iterator<String> rowKeyIter) { List<Map<String, AttributeValue>> keys = new ArrayList<>(); for (int count = 0; count < 100 && rowKeyIter.hasNext(); count++) { keys.add(DynamoDBService.makeDDBKey(rowKeyIter.next())); }// ww w .j a v a2 s . co m KeysAndAttributes keysAndAttributes = new KeysAndAttributes(); keysAndAttributes.setKeys(keys); return keysAndAttributes; }
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 w w w. j a va 2s . 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()); }