我想仅使用 Hashkey 查询我的 dynamoDB。
我的表(名称= testTable)架构如下:
- 字符串自动ID(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()
call.
参考 Vikdor 在 StackOverflow 问题上的评论在 dynamoDB 中使用 hashKey 进行查询 https://stackoverflow.com/questions/30969594/query-all-items-in-dynamodb-from-a-given-hash-key-with-a-hash-range-schema-using
进一步编辑:
我的确切查询方法:
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
测试表类:
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;}
}