【问题标题】:Nested Query in DynamoDB returns nothingDynamoDB 中的嵌套查询不返回任何内容
【发布时间】:2015-05-23 07:13:07
【问题描述】:

我将 DynamoDB 与 Java SDK 结合使用,但在查询嵌套文档时遇到了一些问题。我在下面包含了简化的代码。如果我删除过滤器表达式,那么所有内容都会返回。使用过滤器表达式,不会返回任何内容。我也尝试过使用 withQueryFilterEntry(我更喜欢使用),我得到了相同的结果。任何帮助表示赞赏。大多数在线文档和论坛似乎使用的 java sdk 版本比我使用的要旧。

这是 Json

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

这是查询

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

【问题讨论】:

    标签: java amazon-dynamodb


    【解决方案1】:

    是命名问题吗? (您的示例 json 具有“类型”,但查询使用的是“类型”)

    例如以下对我使用 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; }
    }
    

    【讨论】:

    • 感谢您试用。 “T”是一个错字,就像我说的那样,这是一个简化的例子。这与我尝试的查询表达式几乎相同。如果它对您有用,那意味着从概念上讲它应该有效,并且任何错误对我来说都是奇怪的。我正在使用同事的通用 dao,我认为这是开始调查的地方。
    猜你喜欢
    • 2012-04-14
    • 2018-10-24
    • 1970-01-01
    • 1970-01-01
    • 2021-04-24
    • 2013-09-10
    • 1970-01-01
    相关资源
    最近更新 更多