我想用布尔或SQL之类的条件查询dynamodb表
例如Get me all the items where attribute1 = "no" or attribute2="no"
我尝试使用scanRequest.withScanFilter
,但所有条件都是通过布尔运算执行的。我该怎么做布尔ORing。?
答案 0 :(得分:6)
您可以将ScanRequest的ConditionalOperator设置为" OR"。默认值为" 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);
}
答案 1 :(得分:5)
如果您碰巧知道HashKey
值,则另一个选项是使用QUERY和FilterExpression。以下是Java SDK的一个示例:
Table table = dynamoDB.getTable(tableName);
Map<String, Object> expressionAttributeValues = new HashMap<String, Object>();
expressionAttributeValues.put(":x", "no");
expressionAttributeValues.put(":y", "no");
QuerySpec spec = new QuerySpec()
.withHashKey("HashKeyAttributeName", "HashKeyValueHere")
.withFilterExpression("attribute1 = :x or attribute2 = :y")
.withValueMap(expressionAttributeValues);
ItemCollection<QueryOutcome> items = table.query(spec);
Iterator<Item> iterator = items.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next().toJSONPretty());
}
答案 2 :(得分:0)
您还可以在FilterExpression中使用方括号:
const params = {
TableName: process.env.PROJECTS_TABLE,
IndexName: 'teamId-createdAt-index',
KeyConditionExpression: 'teamId = :teamId',
ExpressionAttributeValues: {
':teamId': verifiedJwt.teamId,
':userId': verifiedJwt.userId,
':provider': verifiedJwt.provider
},
FilterExpression: 'attribute_exists(isNotDeleted) and ((attribute_not_exists(isPrivate)) or (attribute_exists(isPrivate) and userId = :userId and provider = :provider))'
};