Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

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

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 couldnt 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...