Java中具有过滤器表达式的DynamoDb查询 [英] DynamoDb query with filter expression in Java
问题描述
我在AWS中有一个名为school-data的DynamoDb表。以下是使所有学校都具有学校名称的现有代码:
I have a DynamoDb table named school-data in AWS. Below is the existing code to get all the school with a school's name:
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);
}
上面的查询工作正常。但是现在我需要获取所有具有特定学校名称和地址的学校。因此,表中的3列如下:
The above query works fine. But Now I need to fetch all schools with a particular school name and their address . So, below are the 3 columns in the table:
id schoolName details
详细信息列中的数据如下所示:
The data in details column looks like below:
{
"zone": "North",
"type": "Convent",
"address": {
"id": "138",
"street1": "123 Street",
"street2": "456 Road"
}
}
因此,我需要提取所有名称为'ABC'并将street1命名为'123 Street'的学校。因此,我更新了以下查询,如下所示:
So, I need to fetch all the schools with name 'ABC' and address street1 as '123 Street'. So, I updated the above query as below:
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);
}
但是,此操作不会返回任何数据。能否让我知道我在做什么错?
But, this returns no data. Can you please let me know what am I doing wrong?
推荐答案
您需要为DynamoDBQueryExpression设置HashKeyValues并添加页数限制
You need to set HashKeyValues for the DynamoDBQueryExpression and add the page limit
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);
}
这篇关于Java中具有过滤器表达式的DynamoDb查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!