DynamoDb 查询与 Java 中的过滤器表达式

T A*_*nna 5 java filter amazon-dynamodb

我在 AWS 中有一个名为 school-data 的 DynamoDb 表。以下是获取所有学校名称的现有代码:

private DynamoDBQueryExpression<School> createQueryBySchoolName(String schoolName) {
    String matchSchoolName = "schoolName = :schoolName";
    Map<String, AttributeValue> schoolNames = new HashMap<>();
    schoolNames.put(":schoolName", new AttributeValue().withS(schoolName));

    return new DynamoDBQueryExpression<School>()
            .withIndexName("schoolName-index")
            .withKeyConditionExpression(matchSchoolName)
            .withExpressionAttributeValues(schoolNames)
            .withConsistentRead(false);
}
Run Code Online (Sandbox Code Playgroud)

上面的查询工作正常。但是现在我需要获取具有特定学校名称及其地址的所有学校。因此,以下是表中的 3 列:

id   schoolName  details
Run Code Online (Sandbox Code Playgroud)

详细信息列中的数据如下所示:

{
"zone": "North",
"type": "Convent",
"address": {
    "id": "138",
    "street1": "123 Street",
    "street2": "456 Road"
}
}
Run Code Online (Sandbox Code Playgroud)

因此,我需要获取名称为“ABC”且地址为“123 Street”的所有学校。所以,我更新了上面的查询如下:

private DynamoDBQueryExpression<School> createQueryBySchoolName(String schoolName) {
    String matchSchoolName = "schoolName = :schoolName";
    Map<String, AttributeValue> schoolNames = new HashMap<>();
    schoolNames.put(":schoolName", new AttributeValue().withS(schoolName));
    schoolNames.put(":streetName", new AttributeValue().withS("123 Street"));


    return new DynamoDBQueryExpression<School>()
            .withIndexName("schoolName-index")
            .withKeyConditionExpression(matchSchoolName)
            .withFilterExpression("details.address.street1 = :streetName")
            .withExpressionAttributeValues(schoolNames)
            .withConsistentRead(false);
}
Run Code Online (Sandbox Code Playgroud)

但是,这不返回任何数据。你能告诉我我做错了什么吗?

小智 2

您需要为 DynamoDBQueryExpression 设置 HashKeyValues 并添加页面限制

private DynamoDBQueryExpression<School> createQueryBySchoolName(String schoolName) {
String matchSchoolName = "schoolName = :schoolName";
Map<String, AttributeValue> schoolNames = new HashMap<>();
schoolNames.put(":schoolName", new AttributeValue().withS(schoolName));
schoolNames.put(":streetName", new AttributeValue().withS("123 Street"));


return new DynamoDBQueryExpression<Voucher>()
        .withHashKeyValues(schoolName)
        .withIndexName("schoolName-index")
        .withKeyConditionExpression(matchSchoolName)
        .withFilterExpression("details.address.street1 = :streetName")
        .withExpressionAttributeValues(schoolNames)
        .withConsistentRead(false)
        .withLimit(pPageSize);
Run Code Online (Sandbox Code Playgroud)

}