Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing dynamoDB "OR" condition query?

I want to query the dynamodb table with boolean or condition like SQL e.g. Get me all the items where attribute1 = "no" or attribute2="no"

I tried with scanRequest.withScanFilter but all the conditions are performed by doing boolean ANDing. How do I do boolean ORing.?

like image 541
Vallabh Patade Avatar asked Jun 18 '14 00:06

Vallabh Patade


People also ask

Can conditional operations be used in a DynamoDB query?

Yes, like all the other database management systems, DynamoDB also supports all the conditional operators, User can specify a condition that is satisfied for a put, update, or delete operation to work on an item.

Is DynamoDB good for querying?

Querying is a very powerful operation in DynamoDB. It allows you to select multiple Items that have the same partition ("HASH") key but different sort ("RANGE") keys.

Is DynamoDB good for writes?

High-performance reads and writes are easy to manage with DynamoDB, and you can expect performance that is effectively constant across widely varying loads.

What's the difference between querying or scanning a table at DynamoDB?

DynamoDB supports two types of read operations: Query and Scan. To find information, a query operation uses either the primary key or the index key. Scan, as the name implies, is a read call that scans the entire table for a specified result. DynamoDB is designed to be query-optimized.


1 Answers

You can set ConditionalOperator of your ScanRequest to "OR". The default value is "AND"

http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Scan.html

ScanRequest scanRequest = new ScanRequest("tableName");
scanRequest.setConditionalOperator(ConditionalOperator.OR);

Map<String, Condition> scanFilter = new HashMap<String, Condition>();
scanFilter.put("attribute1", new Condition().withAttributeValueList(new AttributeValue("no")).withComparisonOperator(ComparisonOperator.EQ));
scanFilter.put("attribute2", new Condition().withAttributeValueList(new AttributeValue("no")).withComparisonOperator(ComparisonOperator.EQ));

scanRequest.setScanFilter(scanFilter);
ScanResult scanResult = dynamo.scan(scanRequest);

for(Map<String, AttributeValue> item : scanResult.getItems()) {
    System.out.println(item);
}
like image 58
Erben Mo Avatar answered Oct 23 '22 12:10

Erben Mo