DynamoDB:如何使用查询过滤器检查MAP中的条件

The*_*ian 12 java amazon-web-services amazon-dynamodb aws-sdk

我有一个表,结构如下:

DynamoDB文档

当我进行查询时,我希望能够对数据映射进行查询过滤; 但我不确定如何设置查询.

这是我到目前为止:

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));
Run Code Online (Sandbox Code Playgroud)

但我构建过滤器的方式不正确,我一直遇到以下错误:

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)
Run Code Online (Sandbox Code Playgroud)

那么我应该使用什么比较运算符(因为IN用于列表类型),以及如何构建查询过滤器以便我可以在MAP内部指定比较.

谢谢!

bsd*_*bsd 9

尝试比较map属性data.byUserId而不是整个地图结构:

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());
}
Run Code Online (Sandbox Code Playgroud)

一些重要指南:

  1. 确保变量tableName具有表示为a的正确表名string.

  2. 确保HashKeyAttribute使用代表您的属性名称替换字符串Hash Key.

  3. 确保HashKeyAttributeValue使用表示要匹配的哈希键的值替换字符串.

  4. 确保匹配的记录具有data.byUserId比较表达式中提供的值.在示例中"vl49uga5ljjcoln65rcaspmg8u",提供了值.

这是另一个将id属性作为哈希键的示例:

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());
}
Run Code Online (Sandbox Code Playgroud)

  • 我相信这些关于如何使用占位符的指南将对此案例有所帮助:http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ExpressionPlaceholders.html (2认同)