DynamoDB: how to use a query filter to check conditions in MAP

I have a table and the structure is as follows:

DynamoDB document

When I make a request, I would like to be able to do a filter of requests on the data map; but I'm not quite sure how to configure the request.

This is what I have so far:

HashMap<String, AttributeValue> map = new HashMap<String, AttributeValue>(); map.put("byUserId", new AttributeValue().withS("vl49uga5ljjcoln65rcaspmg8u")); queryExpression .withQueryFilterEntry("data", new Condition() .withAttributeValueList(new AttributeValue().withM(map)) .withComparisonOperator(ComparisonOperator.CONTAINS)); 

but the way I create the filter is incorrect, and I continue to work with the following error:

 Exception in thread "main" com.amazonaws.AmazonServiceException: One or more parameter values were invalid: ComparisonOperator CONTAINS is not valid for M AttributeValue type (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: CHIOME68L1HVGO81URD7CIOS6BVV4KQNSO5AEMVJF66Q9ASUAAJG) at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:1077) at com.amazonaws.http.AmazonHttpClient.executeOneRequest(AmazonHttpClient.java:725) at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:460) at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:295) at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.invoke(AmazonDynamoDBClient.java:3106) at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.query(AmazonDynamoDBClient.java:1118) 

So, what is the comparison operator that I have to use (since IN is for list types) and how can I create a query filter so that I can specify the comparison inside the MAP.

Thanks!

+5
source share
1 answer

Try comparing the map data.byUserId attribute instead of the entire map structure:

 Table table = dynamoDB.getTable(tableName); Map<String, Object> expressionAttributeValues = new HashMap<String, Object>(); expressionAttributeValues.put(":x", "vl49uga5ljjcoln65rcaspmg8u"); QuerySpec spec = new QuerySpec() .withHashKey("HashKeyAttribute", "HashKeyAttributeValue") .withFilterExpression("data.byUserId = :x") .withValueMap(expressionAttributeValues); ItemCollection<QueryOutcome> items = table.query(spec); Iterator<Item> iterator = items.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next().toJSONPretty()); } 

Some important recommendations:

  • Make sure the tableName variable has the correct table name represented as string .

  • Be sure to replace the HashKeyAttribute string HashKeyAttribute name of the attribute that your Hash Key represents.

  • Be sure to replace the HashKeyAttributeValue string HashKeyAttributeValue a value that represents the hash key that you want to map.

  • Make sure the corresponding entry has data.byUserId with the value specified in the comparison expression. In this example, the value "vl49uga5ljjcoln65rcaspmg8u" was provided.

Here is another example that has an id attribute as a hash key:

 Table table = dynamoDB.getTable(tableName); Map<String, Object> expressionAttributeValues = new HashMap<String, Object>(); expressionAttributeValues.put(":x", "vl49uga5ljjcoln65rcaspmg8u"); QuerySpec spec = new QuerySpec() .withHashKey("id", "25g77vmummpr4mc5mb9vq36q43") .withFilterExpression("data.byUserId = :x") .withValueMap(expressionAttributeValues); ItemCollection<QueryOutcome> items = table.query(spec); Iterator<Item> iterator = items.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next().toJSONPretty()); } 
+5
source

All Articles