Java中带过滤表达式的DynamoDb查询
我在AWS中有一个名为school data的DynamoDb表。以下是获取具有学校名称的所有学校的现有代码:Java中带过滤表达式的DynamoDb查询,java,filter,amazon-dynamodb,Java,Filter,Amazon Dynamodb,我在AWS中有一个名为school data的DynamoDb表。以下是获取具有学校名称的所有学校的现有代码: private DynamoDBQueryExpression<School> createQueryBySchoolName(String schoolName) { String matchSchoolName = "schoolName = :schoolName"; Map<String, AttributeValue> schoolNa
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);
}
“详细信息”列中的数据如下所示:
{
"zone": "North",
"type": "Convent",
"address": {
"id": "138",
"street1": "123 Street",
"street2": "456 Road"
}
}
所以,我需要把所有名为“ABC”的学校和地址为“123街”的1号街的学校都叫来。因此,我将上述查询更新如下:
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);
}
private DynamoDBQueryExpression createQueryBySchoolName(字符串schoolName){
字符串matchSchoolName=“schoolName=:schoolName”;
Map schoolNames=newhashmap();
schoolName.put(“:schoolName”,新的AttributeValue().with(schoolName));
学校名称。放置(“:streetName”,新属性值()。带有(“123 Street”);
返回新的DynamoDBQueryExpression()
.withIndexName(“学名索引”)
.withKeyConditionExpression(匹配学校名称)
.withFilterExpression(“details.address.street1=:streetName”)
.带有表达式属性值(学校名称)
.具有一致性读取(错误);
}
但是,这不会返回任何数据。您能告诉我我做错了什么吗?您需要为DynamoDBQueryExpression设置hashkeyvalue并添加页面限制
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);
private DynamoDBQueryExpression createQueryBySchoolName(字符串schoolName){
字符串matchSchoolName=“schoolName=:schoolName”;
Map schoolNames=newhashmap();
schoolName.put(“:schoolName”,新的AttributeValue().with(schoolName));
学校名称。放置(“:streetName”,新属性值()。带有(“123 Street”);
返回新的DynamoDBQueryExpression()
.withHashKeyValues(学校名称)
.withIndexName(“学名索引”)
.withKeyConditionExpression(匹配学校名称)
.withFilterExpression(“details.address.street1=:streetName”)
.带有表达式属性值(学校名称)
.withConsistentRead(假)
.有限制(pPageSize);
}这本应该有效,所以我想知道我错过了什么。有多少学校只匹配名称“ABC”(没有对地址进行额外筛选)?如果数量较大,则可能在过滤后响应的第一页确实为空。你确认你正确阅读了所有的结果页面,而不仅仅是第一个吗?@NadavHar'El我还没有实现分页。此外,我还尝试测试了一种情况,在这种情况下,我只需要10条记录,即使是这样,响应也是空的..withHashKeyValues需要一个School对象,而不是您在这里使用的字符串。此外,我还遇到了以下错误:非法查询表达式:必须指定哈希键条件或键条件表达式,但不能同时指定两者。
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);