首页 > 解决方案 > 使用带有嵌套属性的 ScanFilter 扫描 AWS DynamoDB

问题描述

我正在尝试扫描 dynamodb,并且我的扫描在根属性上运行良好,但不适用于嵌套属性。我的代码库是:

String workingProperty = "name"
String notWorkingProperty1 = "name.firstName"
String notWorkingProperty2 = "#name.firstName"
String notWorkingProperty3 = "#name.#firstName"

private Table table;
public List<Item> getAllFilteredItems() {

    ScanFilter scanFilter = new ScanFilter(propertyToLookFor).exists();
    StreamSupport.stream(table.scan(scanFilter).spliterator(), false)
                .collect(Collectors.toList());
}

我的 JSON 是:

{
  "name": {
    "firstName": "Manish"
  }
}

标签: amazon-web-servicesamazon-dynamodbaws-sdkaws-sdk-java-2.0

解决方案


扫描过滤器是遗留的,您应该尝试使用过滤器表达式而不是使用attribute_exists 运算符

尝试这样的事情(您可能可以删除withNameMapandwithValueMap但尚未测试)

ScanSpec scanSpec = new ScanSpec().withFilterExpression("attribute_exists(name.firstName)").withNameMap(new NameMap()).withValueMap(new ValueMap());

results = table.scan(scanSpec)

推荐阅读