AWS DynamoDB - combining multiple query filters by one non-key attribute in java

Earlier this year, Amazon announced support for query filters for non-key attributes.

Is it possible to combine conditions on one attribute value? For example, in this scenario, I would like to get all the elements that do not match a specific list of values ​​in a single non-key column.

Their documentation states that each condition can contain only one attribute value for comparisons of type NOT_EQUALS or BEGINS_WITH . Therefore, the following does not work:

 HashMap<String, Condition> queryFilter = new HashMap<String, Condition>(); List<AttributeValue> AttributeValues = new ArrayList<AttributeValue>(); AttributeValues.add(new AttributeValue().withS("someValue1")); AttributeValues.add(new AttributeValue().withS("someValue2")); Condition attributeCondition = new Condition() .withComparisonOperator(ComparisonOperator.NE) .withAttributeValueList(AttributeValues); queryFilter.put("COLUMN_1", attributeCondition); DynamoDBQueryExpression<Item> queryExpression = new DynamoDBQueryExpression<Item>() .withHashKeyValues(itemKey) .withQueryFilter(queryFilter) .withLimit(pPageSize); 

It seems that only the comparison operator IN can contain a list of attribute values. Ideally, should these conditions be related to each other? Since the query filter is a hash map, we cannot put several conditions in the same column (I tried):

 Condition c1 = new Condition() .withAttributeValueList(new AttributeValue().withS("someValue1")) .withComparisonOperator(ComparisonOperator.NE); Condition c2 = new Condition() .withAttributeValueList(new AttributeValue().withS("someValue2")) .withComparisonOperator(ComparisonOperator.NE); DynamoDBQueryExpression<Item> queryExpression = new DynamoDBQueryExpression<Item>() .withHashKeyValues(itemKey) .withConditionalOperator(ConditionalOperator.AND) .withQueryFilterEntry("COLUMN_1", c1) .withQueryFilterEntry("COLUMN_1", c2) .withLimit(pPageSize); 

Any help or clarification would be greatly appreciated!

thanks

+7
java amazon-web-services nosql amazon-dynamodb
source share
1 answer

Thus, it becomes possible by adding a request (recently presented in this message) to the FilterExpression request.

I saw this in the DynamoDB documentation, but did not upgrade it to the latest AWS Java SDK :(

Using my example above, it will look like this:

 Map<String, AttributeValue> expressionAttributeValues = new HashMap<String, AttributeValue>(); expressionAttributeValues.put(":val1", new AttributeValue().withS("someValue1")); expressionAttributeValues.put(":val2", new AttributeValue().withS("someValue2")); DynamoDBQueryExpression<Item> queryExpression = new DynamoDBQueryExpression<Item>() .withHashKeyValues(itemKey) .withFilterExpression("COLUMN_1 <> :val1 AND COLUMN_1 <> :val2") .withExpressionAttributeValues(expressionAttributeValues) .withLimit(pPageSize); 

Thanks! AWS support!

+14
source share

All Articles