Keshav Sharma
Keshav Sharma

Reputation: 124

Query dynamoDB using only hashKey

I want to query my dynamoDB using only the Hashkey. My table(name = testTable) schema is as follows:

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);

I am expecting a list of testTable objects having same autoID. As I am new to this, please correct me if I am wrong.

Nothing happens when i make mapper.query() call.

Referring comment by Vikdor at the StackOverflow question query using hashKey in dynamoDB

Further Edits:

My exact 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());
    }
}

Stack Trace of my Program:

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

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

Scan Operation output on the table:

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 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;}    

}

Upvotes: 2

Views: 2479

Answers (1)

notionquest
notionquest

Reputation: 39166

As discussed, the problem seems to be in getAlexandraiID declaration.

Please change the method name as mentioned below:-

From:-

public String getAlexandraiID(){ return alexandriaID;} 

To:-

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

Upvotes: 1

Related Questions