Java Code Examples for com.amazonaws.services.dynamodbv2.model.QueryRequest#setIndexName()

The following examples show how to use com.amazonaws.services.dynamodbv2.model.QueryRequest#setIndexName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LowLevelLocalSecondaryIndexExample.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public static void query(String indexName) {

        System.out.println("\n***********************************************************\n");
        System.out.println("Querying table " + tableName + "...");

        QueryRequest queryRequest = new QueryRequest().withTableName(tableName).withConsistentRead(true)
            .withScanIndexForward(true).withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL);

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

        keyConditions.put("CustomerId", new Condition().withComparisonOperator(ComparisonOperator.EQ)
            .withAttributeValueList(new AttributeValue().withS("[email protected]")));

        if (indexName == "IsOpenIndex") {
            System.out.println("\nUsing index: '" + indexName + "': Bob's orders that are open.");
            System.out.println("Only a user-specified list of attributes are returned\n");
            queryRequest.setIndexName(indexName);

            keyConditions.put("IsOpen", new Condition().withComparisonOperator(ComparisonOperator.EQ)
                .withAttributeValueList(new AttributeValue().withN("1")));

            // ProjectionExpression
            queryRequest.setProjectionExpression("OrderCreationDate, ProductCategory, ProductName, OrderStatus");

        }
        else if (indexName == "OrderCreationDateIndex") {
            System.out.println("\nUsing index: '" + indexName + "': Bob's orders that were placed after 01/31/2013.");
            System.out.println("Only the projected attributes are returned\n");
            queryRequest.setIndexName(indexName);

            keyConditions.put("OrderCreationDate", new Condition().withComparisonOperator(ComparisonOperator.GT)
                .withAttributeValueList(new AttributeValue().withN("20130131")));

            // Select
            queryRequest.setSelect(Select.ALL_PROJECTED_ATTRIBUTES);

        }
        else {
            System.out.println("\nNo index: All of Bob's orders, by OrderId:\n");
        }

        queryRequest.setKeyConditions(keyConditions);

        QueryResult result = client.query(queryRequest);
        List<Map<String, AttributeValue>> items = result.getItems();
        Iterator<Map<String, AttributeValue>> itemsIter = items.iterator();
        while (itemsIter.hasNext()) {
            Map<String, AttributeValue> currentItem = itemsIter.next();

            Iterator<String> currentItemIter = currentItem.keySet().iterator();
            while (currentItemIter.hasNext()) {
                String attr = (String) currentItemIter.next();
                if (attr == "OrderId" || attr == "IsOpen" || attr == "OrderCreationDate") {
                    System.out.println(attr + "---> " + currentItem.get(attr).getN());
                }
                else {
                    System.out.println(attr + "---> " + currentItem.get(attr).getS());
                }
            }
            System.out.println();
        }
        System.out.println("\nConsumed capacity: " + result.getConsumedCapacity() + "\n");

    }
 
Example 2
Source File: AbstractDynamoDBQueryCriteria.java    From spring-data-dynamodb with Apache License 2.0 4 votes vote down vote up
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());
			}
		}

		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);
			}
		}

		queryRequest.setKeyConditions(keyConditions);
		queryRequest.setSelect(Select.ALL_PROJECTED_ATTRIBUTES);
		applySortIfSpecified(queryRequest, new ArrayList<String>(new HashSet<String>(allowedSortProperties)));
	}
	return queryRequest;
}
 
Example 3
Source File: LowLevelLocalSecondaryIndexExample.java    From aws-dynamodb-examples with Apache License 2.0 4 votes vote down vote up
public static void query(String indexName) {

        System.out.println("\n***********************************************************\n");
        System.out.println("Querying table " + tableName + "...");

        QueryRequest queryRequest = new QueryRequest()
            .withTableName(tableName)
            .withConsistentRead(true).withScanIndexForward(true)
            .withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL);

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

        keyConditions.put(
            "CustomerId",
            new Condition()
                .withComparisonOperator(ComparisonOperator.EQ)
                .withAttributeValueList(new AttributeValue()
                    .withS("[email protected]")));

        if (indexName == "IsOpenIndex") {
            System.out.println("\nUsing index: '" + indexName
                    + "': Bob's orders that are open.");
            System.out.println("Only a user-specified list of attributes are returned\n");
            queryRequest.setIndexName(indexName);
            
            keyConditions.put(
                "IsOpen",
                new Condition()
                    .withComparisonOperator(ComparisonOperator.EQ)
                    .withAttributeValueList(new AttributeValue().withN("1")));
            
            // ProjectionExpression
            queryRequest.setProjectionExpression("OrderCreationDate, ProductCategory, ProductName, OrderStatus");

        } else if (indexName == "OrderCreationDateIndex") {
            System.out.println("\nUsing index: '" + indexName
                + "': Bob's orders that were placed after 01/31/2013.");
            System.out.println("Only the projected attributes are returned\n");
            queryRequest.setIndexName(indexName);
            
            keyConditions.put("OrderCreationDate", new Condition()
                .withComparisonOperator(ComparisonOperator.GT)
                .withAttributeValueList(new AttributeValue()
                    .withN("20130131")));
            
            // Select
            queryRequest.setSelect(Select.ALL_PROJECTED_ATTRIBUTES);
            
        } else {
            System.out.println("\nNo index: All of Bob's orders, by OrderId:\n");
        }

        queryRequest.setKeyConditions(keyConditions);

        QueryResult result = client.query(queryRequest);
        List<Map<String, AttributeValue>> items = result.getItems();
        Iterator<Map<String, AttributeValue>> itemsIter = items.iterator();
        while (itemsIter.hasNext()) {
            Map<String, AttributeValue> currentItem = itemsIter.next();
            
            Iterator<String> currentItemIter = currentItem.keySet().iterator();
            while (currentItemIter.hasNext()) {
                String attr = (String) currentItemIter.next();
                if (attr == "OrderId" || attr == "IsOpen"
                        || attr == "OrderCreationDate") {
                    System.out.println(attr + "---> "
                            + currentItem.get(attr).getN());
                } else {
                    System.out.println(attr + "---> "
                            + currentItem.get(attr).getS());
                }
            }
            System.out.println();    
        }
        System.out.println("\nConsumed capacity: " + result.getConsumedCapacity() + "\n");

    }