1

I have aws dynamo db table as follows. enter image description here

Now I want to filter it as follows. enter image description here

For that I used the following code and i get 0 results in my code.what's wrong with my code.how to fix it?

public ScanResult getAllMemos() {
    ScanRequest scanRequest = new ScanRequest()
            .withTableName(DB_NAME)
            .withFilterExpression("contains(imgName,thumbnail)");
    return util.getAmazonDynamoDBClient(getActivity()).scan(scanRequest);
}

Without filter expression I get all results.

6
  • The filter expression syntax is quite weird, I think you need to do something like "contains(imgName, :search)" and then additionally call withExpressionAttributeValues("thumbnail") on the ScanRequest, see docs.aws.amazon.com/amazondynamodb/latest/developerguide/… for an example. By the way: you know that scan operations should really not be used in any production environment, it defeats the purpose of dynamodb as a key-value store, it actually is quite "expensive" when your db contains a few million records. Commented Apr 23, 2021 at 18:54
  • @luk2302 if I use :search this gives error and cannot pass String to withExpressionAttributeValues.it expects a map Commented Apr 23, 2021 at 18:58
  • Ah yes, then pass a proper map (as the link) does, have not used the method yet. Commented Apr 23, 2021 at 18:59
  • expressionAttributeValues.put("imgName", new AttributeValue().withS("thumbnail")); like this? Commented Apr 23, 2021 at 19:18
  • 1
    Should be ":imgName" if you use "contains(imgName, :imgName)" Commented Apr 23, 2021 at 19:20

1 Answer 1

2

Like was discussed in the comments, you need to create an expression attribute value map with the value of the filter and use this.

Try something like this:

public ScanResult getAllMemos() {

    Map<String, AttributeValue> expressionAttributeValues =
        new HashMap<String, AttributeValue>();
    expressionAttributeValues.put(":val", new AttributeValue().withS("thumbnail"));


    ScanRequest scanRequest = new ScanRequest()
            .withTableName(DB_NAME)
            .withFilterExpression("contains(imgName,:val)")
            .withExpressionAttributeValues(expressionAttributeValues);
    return util.getAmazonDynamoDBClient(getActivity()).scan(scanRequest);
}
Sign up to request clarification or add additional context in comments.

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.