3

I'm using DynamoDB with the Java SDK, but I'm having some issues with querying nested documents. I've included simplified code below. If I remove the filter expression, then everything gets returned. With the filter expression, nothing is returned. I've also tried using withQueryFilterEntry(which I'd prefer to use) and I get the same results. Any help is appreciated. Most of the documentation and forums online seem to use an older version of the java sdk than I'm using.

Here's the Json

{
  conf:
    {type:"some"},
  desc: "else"
}

Here's the query

DynamoDBQueryExpression<JobDO> queryExpression = new DynamoDBQueryExpression<PJobDO>();
queryExpression.withFilterExpression("conf.Type = :type").addExpressionAttributeValuesEntry(":type", new AttributeValue(type));
return dbMapper.query(getItemType(), queryExpression);

1 Answer 1

1

Is it a naming issue? (your sample json has "type" but the query is using "Type")

e.g. the following is working for me using DynamoDB Local:

public static void main(String [] args) {

    AmazonDynamoDBClient client = new AmazonDynamoDBClient(new BasicAWSCredentials("akey1", "skey1"));
    client.setEndpoint("http://localhost:8000");
    DynamoDBMapper mapper = new DynamoDBMapper(client);

    client.createTable(new CreateTableRequest()
        .withTableName("nested-data-test")
        .withAttributeDefinitions(new AttributeDefinition().withAttributeName("desc").withAttributeType("S"))
        .withKeySchema(new KeySchemaElement().withKeyType("HASH").withAttributeName("desc"))
        .withProvisionedThroughput(new ProvisionedThroughput().withReadCapacityUnits(1L).withWriteCapacityUnits(1L)));

    NestedData u = new NestedData();
    u.setDesc("else");
    Map<String, String> c = new HashMap<String, String>();
    c.put("type", "some");
    u.setConf(c);
    mapper.save(u);

    DynamoDBQueryExpression<NestedData> queryExpression = new DynamoDBQueryExpression<NestedData>();
    queryExpression.withHashKeyValues(u);
    queryExpression.withFilterExpression("conf.#t = :type")
        .addExpressionAttributeNamesEntry("#t", "type") // returns nothing if use "Type"
        .addExpressionAttributeValuesEntry(":type", new AttributeValue("some"));
    for(NestedData u2 : mapper.query(NestedData.class, queryExpression)) {
        System.out.println(u2.getDesc()); // "else"
    }
}

NestedData.java:

@DynamoDBTable(tableName = "nested-data-test")
public class NestedData {

    private String desc;
    private Map<String, String> conf;

    @DynamoDBHashKey
    public String getDesc() { return desc; }
    public void setDesc(String desc) { this.desc = desc; }

    @DynamoDBAttribute
    public Map<String, String> getConf() { return conf; }
    public void setConf(Map<String, String> conf) { this.conf = conf; }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for trying it out. The "T" was a typo, like I said it was a simplified example. This is pretty much the same query expression I was trying. If it worked for you, that means that conceptually it should work and any errors is something weird on my end. I'm using a coworker's generic dao, I think that's the place to start investigating.

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.