Reputation: 147
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?
Upvotes: 1
Views: 2932
Reputation: 39226
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());
}
Upvotes: 3