仅使用hashKey查询dynamoDB [英] Query dynamoDB using only hashKey
问题描述
我只想使用Hashkey查询dynamoDB。
我的表(名称= testTable)架构如下:
I want to query my dynamoDB using only the Hashkey. My table(name = testTable) schema is as follows:
- 字符串自动ID(哈希键)
- 字符串AlexandriaID(RangeKey)
- 字符串docType
我的dynamoDBQueryExpression是:
My dynamoDBQueryExpression is:
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);
我期望有相同的testTable对象列表autoID。
I am expecting a list of testTable objects having same autoID. As I am new to this, please correct me if I am wrong.
当我制作 mapper.query()$ c时,什么也没有发生$ c>调用。
Referring comment by Vikdor at the StackOverflow question 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());
}
}
我的程序的堆栈跟踪:
将对象保存到表中的调用方法:
Calling method to save objects in the table:
***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的查询表的调用方法:
Calling method to query the table on the basis of 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类:
testTable Class:
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;}
}
推荐答案
,问题似乎出在getAlexandraiID声明中。
As discussed, the problem seems to be in getAlexandraiID declaration.
请按如下所述更改方法名称:-
Please change the method name as mentioned below:-
发件人:-
public String getAlexandraiID(){ return alexandriaID;}
收件人:-
@DynamoDBRangeKey(attributeName = "alexandriaID")
public String getAlexandriaID() {
return alexandriaID;
}
这篇关于仅使用hashKey查询dynamoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!