2015-05-22 45 views
2

我正在將DynamoDB與Java SDK一起使用,但我在查詢嵌套文檔時遇到了一些問題。我在下面包含了簡化代碼。如果我刪除過濾器表達式,那麼一切都會返回。使用過濾器表達式,不會返回任何內容。我也嘗試使用withQueryFilterEntry(我更喜歡使用它),並得到相同的結果。任何幫助表示讚賞。網上的大多數文檔和論壇似乎都使用比我使用的java sdk更早的版本。DynamoDB中的嵌套查詢不會返回任何信息

這裏的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); 

回答

1

是它的命名問題? (您的示例json有「類型」,但查詢使用的是「類型」)

例如,下面是使用DynamoDB本地工作對我來說:

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; } 
} 
+0

感謝您試用一下。 「T」是一個錯字,就像我說的那樣是一個簡單的例子。這幾乎是我嘗試的同一個查詢表達式。如果它對你有用,那就意味着它在概念上應該起作用,任何錯誤在我的結尾都是奇怪的。我正在使用同事的通用道,我認爲這是開始調查的地方。 – nilacqua