使用DynamoDBmapper查询GSI,对Sort键进行排序并返回n个结果的当前方法?

时间:2019-06-05 02:30:59

标签: java android amazon-dynamodb dynamodb-queries

使用DynamoDBmapper和DynamoDBQueryExpression查询全局二级索引,对Sort Index排序并返回前n个结果的当前方法是什么?

第一个尝试的解决方案如下所示: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBMapper.Methods.html#DynamoDBMapper.Methods.query

但是,DynamoDBQueryExpression不再具有withKeyConditionExpression。接下来,我尝试了此处显示的示例: How to query a Dynamo DB having a GSI with only hashKeys using DynamoDBMapper

哪个使用setHashKeyValues,但是出现以下错误:com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMappingException:公开的零参数哈希键属性必须使用com.amazonaws.mobileconnectors.dynamodbv2.dynaamobHapper接口注释。

如果我进行扫描,我可以提取结果,因此我认为权限是正确的:

                Map<String, AttributeValue> eav = new HashMap<String, AttributeValue>();
                eav.put(":val1", new AttributeValue().withS(Leaderboard));

                DynamoDBScanExpression scanExpression = new DynamoDBScanExpression()
                        .withFilterExpression("Leaderboard = :val1") // and Ranking <= :val2")
                        .withExpressionAttributeValues(eav);

DynamoDBmapper对象看起来像这样。我没有包括主表中的任何主键属性,只是这个子集。

@DynamoDBTable(tableName = "Users")

public class DynamoLeaderboard implements Serializable {

    private String leaderboard;
    private String userName;
    private int ranking;

    public DynamoLeaderboard() {}

    @DynamoDBIndexHashKey(globalSecondaryIndexName = "Leaderboard-Ranking-index", attributeName = "Leaderboard")
    public String getLeaderboard() {
        return leaderboard;
    }
    public void setLeaderboard(final String leaderboard) {
        this.leaderboard = leaderboard;
    }

    @DynamoDBIndexRangeKey(globalSecondaryIndexName = "Leaderboard-Ranking-index", attributeName = "Ranking")
    public int getRanking() {
        return ranking;
    }
    public void setRanking(int ranking) {
        this.ranking = ranking;
    }

    @DynamoDBAttribute(attributeName = "UserName")
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
}

然后将查询称为:

final DynamoLeaderboard tmpLeaderboard = new DynamoLeaderboard();
tmpLeaderboard.setLeaderboard("0");

Map<String, AttributeValue> eav = new HashMap<String, AttributeValue>();
eav.put(":val1", new AttributeValue().withS("0"));

DynamoDBQueryExpression<DynamoLeaderboard> queryExpression = new DynamoDBQueryExpression<DynamoLeaderboard>();
queryExpression.withIndexName("Leaderboard-index");
queryExpression.setHashKeyValues(tmpLeaderboard);
queryExpression.withConsistentRead(false);

scanResult = mapper.query(DynamoLeaderboard.class, queryExpression);

有关导致零财产哈希键异常的原因的评论?与设置表主哈希键和GSI哈希键有关吗?

有关在GSI上执行查询的当前正确方法的建议?

非常感谢!

2 个答案:

答案 0 :(得分:0)

您需要将主表的关键属性添加到模型中。 DynamoDBMapper陷入困境,因为带有@DynamoDBTable的带注释的类没有@DynamoDBHashKey属性。

或者,您可以尝试将@DynamoDBTable更改为@DynamoDBDocument,尽管我不确定这是否可行。

答案 1 :(得分:0)

这是我的方式:

final Example example = new Example();
example.setID(senderAcid);

final DynamoDBQueryExpression<Example> queryExpression = new DynamoDBQueryExpression<>();

queryExpression.withIndexName("SomeGSIndex")
    .withConsistentRead(false)
    .withProjectionExpression("id,omeOtherData,someOtherFieldName")
    .withHashKeyValues(example);

---------- SortKey的重要条件

queryExpression.withRangeKeyCondition("sortKeyField",
    new Condition()
        .withComparisonOperator(ComparisonOperator.EQ)
        .withAttributeValueList(new AttributeValue().withS(sortKeyValue)));

--------最终

mapper.query(Example.class, query);