【发布时间】:2016-12-25 02:39:54
【问题描述】:
我想只使用 Hashkey 来查询我的 dynamoDB。 我的 table(name = testTable) 架构如下:
- 字符串 autoID (HashKey)
- 字符串 AlexandriaID (RangeKey)
- 字符串文档类型
我的 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() 时没有任何反应。
引用 Vikdor 在 StackOverflow 问题上的评论 query using hashKey in dynamoDB
进一步修改:
我的确切查询方法:
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
调用方法根据autoID查询表:
[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;}
}
【问题讨论】:
-
您的表是否包含 autoId 值“dummyHashKey”?
-
你迭代列表docList并检查结果了吗?
-
@notionquest 是的,在前面的步骤中,我使用 mapper.save() 在表中添加了多个对象,其中两个对象的 autoId 为“dummyHashKey”。在我的代码中,我已经在 mapper.query() 之后迭代列表,但是在调用 mapper.query() 之后没有任何反应。
-
你的意思是你在同一个程序中插入和读取同一个键?全部作为一个单元执行?如果是,请单独运行插入并单独阅读以调试问题。
-
@notionquest 是的,我正在以相同的方法进行这两个调用。让我把它们分开,然后再回复你。谢谢。
标签: amazon-dynamodb