1

What is the best way to do complex queries using DynamoDBQueryExpression.

By default the fetch will executed an AND on all the Conditions provided.

How do i execute something like (C1 && C2 && (C3|| C4|| C5))? Is it even possible with DynamoDB java api?

1 Answer 1

3

DynamoDB supports the complex conditions using "com.amazonaws.services.dynamodbv2.document.spec.QuerySpec". You can write the AND and OR conditions using FilterExpression.

Please see the example below:-

1) withKeyConditionExpression - For hash key and range key expressions

2) withFilterExpression - For all other attributes (i.e. other than key attributes)

ItemCollection<QueryOutcome> items = null;

        QuerySpec querySpec = new QuerySpec();

        ValueMap valueMap = new ValueMap();
        valueMap.withString(":autoIdVal", autoID);
        valueMap.withString(":docTypeVal", docType);
        valueMap.withString(":username", username);
        valueMap.withString(":comment", comment);

        Map<String, String> nameMap = new LinkedHashMap<>();
        nameMap.put("#comment", "comment");

        querySpec.withKeyConditionExpression("autoID = :autoIdVal").withFilterExpression("(docType = :docTypeVal AND username = :username) OR (#comment = :comment)")
                .withValueMap(valueMap)
                .withNameMap(nameMap);

        items = table.query(querySpec);

        Iterator<Item> iterator = items.iterator();

        Item itemData = null;

        while (iterator.hasNext()) {
            itemData = iterator.next();

            System.out.println("Json data ====================>" + itemData.toJSONPretty());

        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.