How to query Dynamo database with GSI only with hashKeys using DynamoDBMapper

I am very new to Dynamo DB , and maybe this is a very trivial question, but I looked through Dynamo DB docs and questions, but I could not find any links that tells how query DDB for GSI , which has only a hash key, and the range key is not specified for it.

I get an exception Unalloyed request expression: the hash key condition was not found in the request.

+6
source share
1 answer

In your object with the annotated DynamoDB model, you should use @DynamoDBIndexHashKey(globalSecondaryIndexName = "gsiIndexName) to indicate that this is a hash key for the GSI:

 @DynamoDBTable(tableName = "myTable") public class MyTable { ... @DynamoDBIndexHashKey(globalSecondaryIndexName = "myGsi") public String getGsiHk() { return gsiHk; } ... } 

And then use the query method on DynamoDBMapper :

 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); 
+13
source

Source: https://habr.com/ru/post/987927/


All Articles