DBMapper查询仅具有hashKeys的具有GSI的Dyna

DBMapper查询仅具有hashKeys的具有GSI的Dyna

本文介绍了如何使用DynamoDBMapper查询仅具有hashKeys的具有GSI的Dynamo DB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Dynamo DB 来说是个新手,也许这是一个非常琐碎的问题,但是我浏览了Dynamo DB的文档并发现堆栈溢出问题,但是我找不到单个链接来说明如何查询DDB中的 GSI ,该GSI仅具有哈希键,并且没有为其指定范围键。

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.

推荐答案

在带有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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 15:16