How to query a Dynamo DB having a GSI with only hashKeys using DynamoDBMapper

后端 未结 1 1493
甜味超标
甜味超标 2020-12-10 01:49

I am very new to Dynamo DB and may be this is very trivial question, but i went through the documents of Dynamo DB and stack overflow questions but i couldn

相关标签:
1条回答
  • 2020-12-10 02:04

    On your DynamoDB annotated model object, you should use @DynamoDBIndexHashKey(globalSecondaryIndexName = "gsiIndexName) to signify that it 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 the 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);
    
    0 讨论(0)
提交回复
热议问题