【问题标题】:DynamoDB: How to use a query filter to check for conditions in a MAPDynamoDB:如何使用查询过滤器检查 MAP 中的条件
【发布时间】:2015-05-09 18:58:11
【问题描述】:

我有一张桌子,结构如下:

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

这是我目前所拥有的:

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));

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

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)

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

谢谢!

【问题讨论】:

    标签: java amazon-web-services amazon-dynamodb aws-sdk


    【解决方案1】:

    尝试比较地图属性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());
    }
    

    一些重要的指导方针:

    1. 确保变量tableName 具有正确的表名,以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());
    }
    

    【讨论】:

    • 感谢您的回复,但很遗憾没有成功。 :(
    • 你能提供更多细节吗?
    • 提供的答案的最终结果是什么?错误?没有返回结果?
    • 太棒了!!!太感谢了!这很好用。我要为未来的读者提及的几件事是data 是保留关键字。有一次,我更改了属性名称,我可以使用@spin 代码。
    • 我相信这些关于如何使用占位符的指南将对这种情况有所帮助:docs.aws.amazon.com/amazondynamodb/latest/developerguide/…
    猜你喜欢
    • 2020-04-30
    • 1970-01-01
    • 2014-06-04
    • 2021-04-17
    • 1970-01-01
    • 2020-08-29
    • 1970-01-01
    • 2016-12-31
    • 1970-01-01
    相关资源
    最近更新 更多