9

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);
}

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"
}
}

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?

2
  • This should have worked, so I'm wondering what I'm missing. How many schools matched just the name "ABC" (without the additional filtering for the address)? If the number is large, it is possible the first page of responses will indeed be empty after the filtering. Did you verify that you correctly read all the result pages, not just the first one? Commented May 17, 2020 at 10:10
  • @NadavHar'El I haven't implemented pagination. Also, I tried testing for the case where i would expect just 10 records and even for that , the response was empty. Commented May 19, 2020 at 10:26

2 Answers 2

2

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);

}

Sign up to request clarification or add additional context in comments.

2 Comments

.withHashKeyValues expects a School object and not a string as you have used here.
Also, i get this error : Illegal query expression: Either the hash key conditions or the key condition expression must be specified but not both.
0

.withHashKeyValues(schoolName) will only accept a Hash Key object as shown in this image

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.