Example usage for com.amazonaws.services.dynamodbv2.model QueryRequest setIndexName

List of usage examples for com.amazonaws.services.dynamodbv2.model QueryRequest setIndexName

Introduction

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

Prototype


public void setIndexName(String indexName) 

Source Link

Document

The name of an index to query.

Usage

From source file:com.vivastream.dynamodb.core.DynamoDBTemplate.java

License:Apache License

public <T> List<T> query(String tableName, String indexName, Map<String, Condition> keyConditions,
        final ObjectExtractor<T> extractor, String... columnsToInclude) throws EmptyResultDataAccessException {
    Assert.notNull(tableName, "Table must not be null");
    Assert.notNull(extractor, "ObjectExtractor must not be null");
    if (logger.isDebugEnabled()) {
        logger.debug("Executing query on " + tableName + " for " + renderKey(keyConditions));
    }//w w w . ja v  a2s .c  om

    QueryRequest request = new QueryRequest(tableName) //
            .withConsistentRead(false) // because query is used on GSIs [where consistent reads are not supported] - if we needed to query on the primary index could make this a parameter
            .withKeyConditions(keyConditions);

    if (columnsToInclude != null && columnsToInclude.length > 0) {
        request.setAttributesToGet(Arrays.asList(columnsToInclude));
    }

    if (indexName != null) {
        request.setIndexName(indexName);
    }

    QueryResult result = client.query(request);

    List<Map<String, AttributeValue>> items = result.getItems();
    List<T> convertedItems = new ArrayList<T>(items.size());
    for (Map<String, AttributeValue> item : items) {
        convertedItems.add(extractor.extract(item));
    }

    return convertedItems;
}

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

License:Apache License

private List<String> applyIndexAndGetKeyFields(ConditionExpressionBuilder conditionExpressionBuilder,
        QueryRequest queryRequest, List<Sort> sorts) {
    String hashKey = conditionExpressionBuilder.getHashKey();
    String rangeKey = conditionExpressionBuilder.getRangeKey();
    IndexData indexData;//from w  w  w.  j  a  v a 2  s  .  c om

    if (sorts == null || sorts.isEmpty()) {
        indexData = indexes.get(hashKey).get(rangeKey);
    } else if (sorts.size() == 1) {
        Sort sort = sorts.get(0);
        String sortKey = sort.getField();

        // DynamoDB can only sort on the effective range key.  If a range key is specified, ensure the range key and
        // sort key are the same.
        if (rangeKey != null && !rangeKey.equals(sortKey)) {
            throw new JeppettoException(
                    "DynamoDB can only sort on the effective range key. Unable to sort on: " + sortKey);
        }

        queryRequest.setScanIndexForward(sort.getSortDirection() == SortDirection.Ascending);

        // Index is based off the sort key
        indexData = indexes.get(hashKey).get(sortKey);
    } else {
        throw new JeppettoException("DynamoDB only supports one sort value.");
    }

    if (indexData.indexName != null && !indexData.projectsOverEntity) {
        logger.warn(
                "Query using index {} incurs additional costs to fully fetch a {} type. Use a projected object"
                        + " DAO to avoid this overhead.",
                indexData.indexName, entityClass.getSimpleName());
    }

    queryRequest.setIndexName(indexData.indexName);

    return indexData.keyFields;
}

From source file:org.socialsignin.spring.data.dynamodb.repository.query.AbstractDynamoDBQueryCriteria.java

License:Apache License

protected QueryRequest buildQueryRequest(String tableName, String theIndexName, String hashKeyAttributeName,
        String rangeKeyAttributeName, String rangeKeyPropertyName, List<Condition> hashKeyConditions,
        List<Condition> rangeKeyConditions) {

    // TODO Set other query request properties based on config
    QueryRequest queryRequest = new QueryRequest();
    queryRequest.setTableName(tableName);
    queryRequest.setIndexName(theIndexName);

    if (isApplicableForGlobalSecondaryIndex()) {
        List<String> allowedSortProperties = new ArrayList<String>();

        for (Entry<String, List<Condition>> singlePropertyCondition : propertyConditions.entrySet()) {
            if (entityInformation.getGlobalSecondaryIndexNamesByPropertyName().keySet()
                    .contains(singlePropertyCondition.getKey())) {
                allowedSortProperties.add(singlePropertyCondition.getKey());
            }/*  w ww  .  j a  va2  s .  co m*/
        }

        HashMap<String, Condition> keyConditions = new HashMap<String, Condition>();

        if (hashKeyConditions != null && hashKeyConditions.size() > 0) {
            for (Condition hashKeyCondition : hashKeyConditions) {
                keyConditions.put(hashKeyAttributeName, hashKeyCondition);
                allowedSortProperties.add(hashKeyPropertyName);
            }
        }
        if (rangeKeyConditions != null && rangeKeyConditions.size() > 0) {
            for (Condition rangeKeyCondition : rangeKeyConditions) {
                keyConditions.put(rangeKeyAttributeName, rangeKeyCondition);
                allowedSortProperties.add(rangeKeyPropertyName);
            }
        }

        for (Entry<String, List<Condition>> singleAttributeConditions : attributeConditions.entrySet()) {

            for (Condition condition : singleAttributeConditions.getValue()) {
                keyConditions.put(singleAttributeConditions.getKey(), condition);
            }
        }

        if (sort != null) {
            for (Order order : sort) {
                final String sortProperty = order.getProperty();
                if (entityInformation.isGlobalIndexRangeKeyProperty(sortProperty)) {
                    allowedSortProperties.add(sortProperty);
                }
            }
        }

        queryRequest.setKeyConditions(keyConditions);
        queryRequest.setSelect(Select.ALL_PROJECTED_ATTRIBUTES);
        applySortIfSpecified(queryRequest, new ArrayList<String>(new HashSet<String>(allowedSortProperties)));
    }
    return queryRequest;
}