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

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

Introduction

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

Prototype

KeysAndAttributes

Source Link

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  va2  s .c  o  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()));
    }/*from   w ww.j a  v  a2  s. c o m*/
    KeysAndAttributes keysAndAttributes = new KeysAndAttributes();
    keysAndAttributes.setKeys(keys);
    return keysAndAttributes;
}

From source file:com.dell.doradus.db.s3.DynamoDBService2.java

License:Apache License

@Override
public List<DColumn> getColumns(String storeName, String rowKey, Collection<String> columnNames) {
    Timer t = new Timer();
    String namespace = getTenant().getName();
    String key = storeName + "_" + rowKey;
    List<DColumn> list = new ArrayList<>();

    List<Map<String, AttributeValue>> keys = new ArrayList<>();
    for (String col : columnNames) {
        keys.add(getPrimaryKey(key, col));
        if (keys.size() >= 100) {
            KeysAndAttributes x = new KeysAndAttributes().withKeys(keys);
            Map<String, KeysAndAttributes> map = new HashMap<>();
            map.put(namespace, x);//from w  w  w  .  j  ava  2  s.  co m
            BatchGetItemResult result = m_client.batchGetItem(new BatchGetItemRequest(map));
            if (result.getUnprocessedKeys().size() > 0)
                throw new RuntimeException("Could not process all items");
            list.addAll(fromItems(result.getResponses().get(namespace)));
            keys.clear();
        }
    }
    if (keys.size() > 0) {
        KeysAndAttributes x = new KeysAndAttributes().withKeys(keys);
        Map<String, KeysAndAttributes> map = new HashMap<>();
        map.put(namespace, x);
        BatchGetItemResult result = m_client.batchGetItem(new BatchGetItemRequest(map));
        if (result.getUnprocessedKeys().size() > 0)
            throw new RuntimeException("Could not process all items");
        list.addAll(fromItems(result.getResponses().get(namespace)));
        keys.clear();
    }
    m_logger.debug("get columns for {} in {}", namespace, t);
    return list;
}

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

License:Apache License

@Override
public <P extends ParaObject> Map<String, P> readAll(String appid, List<String> keys, boolean getAllColumns) {
    if (keys == null || keys.isEmpty() || StringUtils.isBlank(appid)) {
        return new LinkedHashMap<String, P>();
    }/* ww w .j  a v  a  2 s .  c  o  m*/

    // DynamoDB doesn't allow duplicate keys in batch requests
    Set<String> keySet = new TreeSet<String>(keys);
    if (keySet.size() < keys.size() && !keySet.isEmpty()) {
        logger.debug("Duplicate keys found - readAll({})", keys);
    }

    Map<String, P> results = new LinkedHashMap<String, P>(keySet.size(), 0.75f, true);
    ArrayList<Map<String, AttributeValue>> keyz = new ArrayList<Map<String, AttributeValue>>(MAX_KEYS_PER_READ);

    try {
        int batchSteps = 1;
        if ((keySet.size() > MAX_KEYS_PER_READ)) {
            batchSteps = (keySet.size() / MAX_KEYS_PER_READ)
                    + ((keySet.size() % MAX_KEYS_PER_READ > 0) ? 1 : 0);
        }

        Iterator<String> it = keySet.iterator();
        int j = 0;

        for (int i = 0; i < batchSteps; i++) {
            while (it.hasNext() && j < MAX_KEYS_PER_READ) {
                String key = it.next();
                results.put(key, null);
                keyz.add(Collections.singletonMap(Config._KEY, new AttributeValue(getKeyForAppid(key, appid))));
                j++;
            }

            KeysAndAttributes kna = new KeysAndAttributes().withKeys(keyz);
            if (!getAllColumns) {
                kna.setAttributesToGet(Arrays.asList(Config._KEY, Config._TYPE));
            }

            batchGet(Collections.singletonMap(getTableNameForAppid(appid), kna), results);
            keyz.clear();
            j = 0;
        }
        logger.debug("DAO.readAll({}) {}", keySet, results.size());
    } catch (Exception e) {
        logger.error("Failed to readAll({}), {}", keys, e);
    }
    return results;
}

From source file:com.vivastream.security.oauth2.provider.token.store.DynamoDBTokenStore.java

License:Apache License

private Collection<OAuth2AccessToken> loadTokensByClientAndUserIndex(Map<String, Condition> keyCondition,
        boolean filterOutNullUsers) {
    List<OAuth2AccessToken> accessTokens = new ArrayList<OAuth2AccessToken>();

    List<String> accessTokenIds = null;
    try {//from   w w w .  j  av a  2s  . co  m
        accessTokenIds = dynamoDBTemplate.query(schema.getAccessTableName(),
                schema.getAccessIndexClientIdAndUserName(), keyCondition, //
                new ObjectExtractor<String>() {

                    public String extract(Map<String, AttributeValue> values) {
                        return values.get(schema.getAccessColumnTokenId()).getS();
                    }
                }, schema.getAccessColumnTokenId());

        List<Map<String, AttributeValue>> keys = new ArrayList<Map<String, AttributeValue>>(
                accessTokenIds.size());
        for (String accessTokenId : accessTokenIds) {
            keys.add(Collections.singletonMap(schema.getAccessColumnTokenId(),
                    new AttributeValue(accessTokenId)));
        }
        if (filterOutNullUsers) {
            accessTokens = dynamoDBTemplate.batchGet(schema.getAccessTableName(), // 
                    new KeysAndAttributes().withKeys(keys).withConsistentRead(true).withAttributesToGet(
                            schema.getAccessColumnTokenId(), schema.getAccessColumnToken(),
                            schema.getAccessColumnIsNullUser()), // 
                    new NonNullUserSafeAccessTokenExtractor());
        } else {
            accessTokens = dynamoDBTemplate.batchGet(schema.getAccessTableName(), // 
                    new KeysAndAttributes().withKeys(keys).withConsistentRead(true).withAttributesToGet(
                            schema.getAccessColumnTokenId(), schema.getAccessColumnToken()), // 
                    new SafeAccessTokenExtractor());
        }
    } catch (EmptyResultDataAccessException e) {
        if (LOG.isInfoEnabled()) {
            LOG.info("Failed to find access token for " + keyCondition.toString());
        }
    }
    accessTokens = removeNulls(accessTokens);

    return accessTokens;
}

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 v a 2 s  . 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  av a 2s . c o 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);
}