仅使用hashKey查询dynamoDB

问题描述:

我想仅使用Hashkey查询我的dynamoDB。 我的表(名称= TestTable的)架构如下:仅使用hashKey查询dynamoDB

  • 字符串自动识别(HashKey)
  • 字符串AlexandriaID(RangeKey)
  • 字符串DOCTYPE

我dynamoDBQueryExpression是:


String hashKey = "dummyHashKey"; 

testTable hashKeyValues = new testTable(); 

hashKeyValues.setAutoID(hashKey); 

DynamoDBQueryExpression<testTable> queryExpression = new DynamoDBQueryExpression<testTable>(); 
queryExpression.withHashKeyValues(hashKeyValues); 

//Assuming I have a dynamoDBMapper object mapper 

List<testTable> docList = mapper.query(testTable.class, queryExpression); 

我在等待一个具有相同autoID的testTable对象列表。由于我是新手,如果我错了,请纠正我。

当我拨打mapper.query()时没有任何反应。

在StackOverflow上的问题由Vikdor参考意见 query using hashKey in dynamoDB

进一步编辑:

我确切QueryMethod:

public void queryFromRFIDocumentDetails(String hashKey){ 
    System.out.println((new Throwable()).getStackTrace()[0].toString() + "***Enter***"); 

    testTable hashKeyValues = new testTable(); 
    hashKeyValues.setAutoID(hashKey); 

    System.out.println("AutoID for hashKeyValues " + hashKeyValues.getAutoID()); 
    System.out.println("DocTYpe for hashKeyValues " + hashKeyValues.getDocType()); 
    System.out.println("AlexandriaID for hashKeyValues " + hashKeyValues.getAlexandraiID()); 

    DynamoDBQueryExpression<testTable> queryExpression = new DynamoDBQueryExpression<testTable>(); 
    queryExpression.withHashKeyValues(hashKeyValues); 
    queryExpression.withConsistentRead(false); 

    System.out.println("calling mapper.query"); //nothing happens after this 

    List<testTable> docList = new ArrayList<testTable>(); 
    docList = mapper.query(testTable.class, queryExpression); 

    for(int i=0; i<docList.size(); i++){ 
     System.out.println("***iterating at retrieved index " + i); 
     System.out.println("AutoID for retrieved document " + docList.get(i).getAutoID()); 
     System.out.println("DocTYpe for retrieved document " + docList.get(i).getDocType()); 
     System.out.println("AlexandriaID for retrieved document " + docList.get(i).getAlexandraiID()); 
    } 
} 

堆栈我的计划跟踪:

调用方法保存表中的对象:

***iterating at index 0 
[java] AutoID for document to be saved abc 
[java] DocTYpe for document to be saved foo 
[java] AlexandriaID for document to be saved id1 
[java] com.amazon.sduservice.db.dynamoDB.saveInRFIDocumentDetails(dynamoDB.java:201)***Enter*** 
[java] com.amazon.sduservice.db.dynamoDB.saveInRFIDocumentDetails(dynamoDB.java:203)***Exit*** 
[java] ***iterating at index 1 
[java] AutoID for document to be saved abc 
[java] DocTYpe for document to be saved foo 
[java] AlexandriaID for document to be saved id2 
[java] com.amazon.sduservice.db.dynamoDB.saveInRFIDocumentDetails(dynamoDB.java:201)***Enter*** 
[java] com.amazon.sduservice.db.dynamoDB.saveInRFIDocumentDetails(dynamoDB.java:203)***Exit*** 
[java] ***iterating at index 2 
[java] AutoID for document to be saved abc 
[java] DocTYpe for document to be saved foo 
[java] AlexandriaID for document to be saved id3 
[java] com.amazon.sduservice.db.dynamoDB.saveInRFIDocumentDetails(dynamoDB.java:201)***Enter*** 
[java] com.amazon.sduservice.db.dynamoDB.saveInRFIDocumentDetails(dynamoDB.java:203)***Exit*** 
[java] hashKey is abc 

调用方法来查询自动识别的基础上,该表:

[java] com.amazon.sduservice.db.dynamoDB.queryFromRFIDocumentDetails(dynamoDB.java:207)***Enter*** 
[java] AutoID for hashKeyValues abc 
[java] DocTYpe for hashKeyValues null 
[java] AlexandriaID for hashKeyValues null 
[java] calling mapper.query 
在桌子上

扫描操作的输出:

Scanning Table RFIDocumentDetails 
[java] {docType={S: foo,}, autoID={S: abc,}, alexandriaID={S: id1,}} 
[java] {docType={S: foo,}, autoID={S: abc,}, alexandriaID={S: id2,}} 
[java] {docType={S: foo,}, autoID={S: abc,}, alexandriaID={S: id3,}} 
[java] {docType={S: pdf,}, autoID={S: HashKey,}, alexandriaID={S: alexandriaID1,}} 
[java] {docType={S: pdf,}, autoID={S: HashKey,}, alexandriaID={S: alexandriaID2,}} 
[java] {docType={S: foo,}, autoID={S: asdf,}, alexandriaID={S: id1,}} 
[java] {docType={S: foo,}, autoID={S: asdf,}, alexandriaID={S: id2,}} 
[java] {docType={S: foo,}, autoID={S: foo,}, alexandriaID={S: id1,}} 
[java] {docType={S: foo,}, autoID={S: foo,}, alexandriaID={S: id2,}} 
[java] Scanning Table Finishes 

TestTable的类别:

public class testTable {  
    private String autoID; 
    private String docType; 
    private String alexandriaID;  

    @DynamoDBHashKey(attributeName="autoID") 
    public String getAutoID(){ return autoID;} 
    public void setAutoID(String autoID){ this.autoID = autoID;}  

    @DynamoDBRangeKey(attributeName="alexandriaID") 
    public String getAlexandraiID(){ return alexandriaID;} 
    public void setAlexandriaID(String alexandriaID){ this.alexandriaID = alexandriaID;}  

    @DynamoDBAttribute(attributeName="docType") 
    public String getDocType(){ return docType;}  
    public void setDocType(String docType){ this.docType = docType;}  

} 
+0

请问您的表包含自动识别值“dummyHashKey “? – notionquest

+0

你是否迭代了列表docList并检查结果? – notionquest

+0

@notionquest是的,在前面的步骤中,我使用mapper.save()在表中添加了多个对象,其中两个具有autoId作为“dummyHashKey”。 在我的代码中,我已经在mapper.query()之后迭代列表,但在调用mapper.query()之后没有任何反应。 –

正如所讨论的,这个问题似乎是在getAlexandraiID声明中。

请更改方法名称如下所述: -

来源: -

public String getAlexandraiID(){ return alexandriaID;} 

为: -

@DynamoDBRangeKey(attributeName = "alexandriaID") 
public String getAlexandriaID() { 
    return alexandriaID; 
}