繁体   English   中英

使用Java和DynamoDBMapper扫描DynamoDB JSON文档

[英]Scan DynamoDB JSON document using Java and DynamoDBMapper

我正在使用DynamoDB,并且还在Java中通过使用DynamoDBMapper类将JSON传递给它来存储文档。

将数据放入表中非常简单。 并且还查询表是否具有可用的哈希值或范围值。

但我想对JSON文档的值进行扫描(假定)。 我一直在寻找示例,但找不到任何东西,或者至少在使用DynamoDBMapper的处理方式时找不到。

我认为这样做可以正确吗? 如果是这样,是否有人有这样做的例子? 具体来说,如何在要查询的文档/ JSON中指定属性?

我的表设置有一个Hash,Range和一个“ document”属性,其中包含传递给它的JSON。 因此,如果我想检查该“文档”中的“名称”值,如何在条件中指定该值?

这是我尝试过的代码片段的示例:

DynamoDBScanExpression scanExpression = new DynamoDBScanExpression();
    scanExpression
            .addFilterCondition(
                    "document.name",
                    new Condition().withComparisonOperator(
                            ComparisonOperator.EQ)
                            .withAttributeValueList(
                                    new AttributeValue()
                                            .withS(existingWebsiteName)));
    List<PublisherSite> scanResult = getMapper().scan(PublisherSite.class, scanExpression);

我也尝试将attributeName指定为“ name”,但这也不起作用。 我没有结果。

FilterConditions仅适用于顶级属性。 您的代码段尝试将FilterCondition放置在名为“ document.name”的顶级属性上。 相反,您可以使用FilterExpression来限制对嵌套属性的项目检索,还可以使用ProjectionExpression选择所需的顶级和嵌套属性。

final String filterExpression = "document.name = :existingWebsiteName";
final Map<String, AttributeValue> expressionAttributeValues = Collections.singletonMap(":existingWebsiteName", existingWebsiteName);

DynamoDBScanExpression scanExpression = new DynamoDBScanExpression()
    .withFilterExpression("document.name = :existingWebsiteName")
    .withExpressionAttributeValues(expressionAttributeValues);
List<PublisherSite> scanResult = getMapper().scan(PublisherSite.class, scanExpression);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM