如何使用 DynamoDBMapper 查询具有仅具有 hashKeys 的 GSI 的 Dynamo DB [英] How to query a Dynamo DB having a GSI with only hashKeys using DynamoDBMapper
问题描述
我对 Dynamo DB 很陌生,可能这是一个非常微不足道的问题,但我浏览了 Dynamo DB 的文档和堆栈溢出问题,但我找不到一个链接来说明如何查询 GSI 的 DDB,它只有散列键,没有指定范围键.
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.
我收到异常 Illegal query expression: No hash key condition is found in the query.
I get the exception Illegal query expression: No hash key condition is found in the query.
推荐答案
在你的 DynamoDB 注释模型对象上,你应该使用 @DynamoDBIndexHashKey(globalSecondaryIndexName = "gsiIndexName)
来表示它是一个哈希键对于 GSI:
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;
}
...
}
然后在DynamoDBMapper
上使用query
方法:
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);
这篇关于如何使用 DynamoDBMapper 查询具有仅具有 hashKeys 的 GSI 的 Dynamo DB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!