2
我想僅使用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;}
}
請問您的表包含自動識別值「dummyHashKey 「? – notionquest
你是否迭代了列表docList並檢查結果? – notionquest
@notionquest是的,在前面的步驟中,我使用mapper.save()在表中添加了多個對象,其中兩個具有autoId作爲「dummyHashKey」。 在我的代碼中,我已經在mapper.query()之後迭代列表,但在調用mapper.query()之後沒有任何反應。 –