1

I'm using DynamoDB and also storing documents by passing JSON to it, all using the DynamoDBMapper class, in Java.

It's straightforward enough to put data in to the table. And also to query the table if you have the Hash or Range values available.

But I want to perform a scan (I presume) for a value with the JSON document. I've been hunting around for examples but I can't find any, or at least not when using the DynamoDBMapper way of doing things.

So would I be right in thinking this can be done? If so, does anyone have an example of doing this? Specifically, how do you specify the attribute in the document/JSON that you want to query on?

My table is set up with a Hash, Range and a "document" attribute containing the JSON that was passed to it. So if I wanted to check a "name" value within that "document", how do I specify that in a Condition?

Here's an example of a code snippet I have tried:

DynamoDBScanExpression scanExpression = new DynamoDBScanExpression();
    scanExpression
            .addFilterCondition(
                    "document.name",
                    new Condition().withComparisonOperator(
                            ComparisonOperator.EQ)
                            .withAttributeValueList(
                                    new AttributeValue()
                                            .withS(existingWebsiteName)));
    List<PublisherSite> scanResult = getMapper().scan(PublisherSite.class, scanExpression);

I have also tried specifiying the attributeName as just "name" too, but that doesn't work either. I get no results back.

2
  • Can you add your PublisherSite code, and a mapper.save() call to make an MCVE? Commented Apr 23, 2015 at 15:17
  • Try the query with a simpler a scalar value first (instead of map or document type)... see if it works, and please put some example of the JSON that you are storing in the db. thxs Commented Apr 24, 2015 at 20:42

1 Answer 1

1

FilterConditions only work for top-level attributes. Your snippet tries to put a FilterCondition on a top-level attribute named "document.name". Instead, you can use a FilterExpression to condition item retrieval on nested attributes, and you can use a ProjectionExpression to select the top-level and nested attributes you desire.

final String filterExpression = "document.name = :existingWebsiteName";
final Map<String, AttributeValue> expressionAttributeValues = Collections.singletonMap(":existingWebsiteName", existingWebsiteName);

DynamoDBScanExpression scanExpression = new DynamoDBScanExpression()
    .withFilterExpression("document.name = :existingWebsiteName")
    .withExpressionAttributeValues(expressionAttributeValues);
List<PublisherSite> scanResult = getMapper().scan(PublisherSite.class, scanExpression);
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.