Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AWS by (19.1k points)

I am very new to Dynamo DB and maybe this is a very trivial question, but I went through the documents of Dynamo DB and stack overflow questions but I couldn't find a single link which tells how to query DDB for GSI which has only hash key and there are no range key specified for the same.

I get the exception Illegal query expression: No hash key condition is found in the query.

1 Answer

0 votes
by (44.4k points)

You should use this on your DynamoDB annotated model object:

To show that for the GSI it’s the hash key using this @DynamoDBIndexHashKey(globalSecondaryIndexName = "gsiIndexName) :

@DynamoDBTable(tableName = "myTable")

public class MyTable {

    ...

 

    @DynamoDBIndexHashKey(globalSecondaryIndexName = "myGsi")

    public String getGsiHk() {

        return gsiHk;

    }

 

    ...

}

Use the query method on DynamoDBMapper after that:

final MyTable gsiKeyObj = new MyTable();

gsiKeyObj.setGsiHk("myGsiHkValue");

final DynamoDBQueryExpression<MyTable> queryExpression = 

    new DynamoDBQueryExpression<>();

queryExpression.setHashKeyValues(gsiKeyObj);

queryExpression.setIndexName("myGsi");

queryExpression.setConsistentRead(false);   // cannot use consistent read on GSI

final PaginatedQueryList<MyTable> results = 

    mapper.query(MyTable.class, queryExpression);

Related questions

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer
asked Jul 27, 2019 in AWS by yuvraj (19.1k points)

Browse Categories

...