我在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查询
发布评论