gpt4 book ai didi

java - DynamoDB 分页 - 最后评估的键在最后一页不为空

转载 作者:搜寻专家 更新时间:2023-11-01 02:57:05 27 4
gpt4 key购买 nike

我正在尝试使用 Java SDK 在 DynamoDB 中实现分页。

我有一个简单的数据模型,带有一个 HashKey id 和一个日期作为 RangeKey。我想查询给定日期之后的所有日期。到目前为止这是可行的,但问题是使用最后评估的键的分页部分。

在查询最后一页时,lastEvaluatedKey不为null,它仍然指向查询的最后一页的最后一项。将此键设置为 èxclusiveStartKey 的另一个查询然后返回 0 个结果和 null lastEvaluatedKey

我的代码如下所示:

var query = new DynamoDBQueryExpression<DynamoModel>();
var keyCondition = ImmutableMap.<String, AttributeValue>builder()
.put(":v_userid", new AttributeValue().withS(userId))
.put(":v_date", new AttributeValue().withS(date.toString()))
.build();

if (!StringUtils.isEmpty(lastKey)) {
query.setExclusiveStartKey(ImmutableMap.<String, AttributeValue>builder()
.put("userId", new AttributeValue().withS(userId))
.put("date", new AttributeValue().withS(lastKey)).build());
}

query.withKeyConditionExpression("userId = :v_userid AND date >= :v_date");
query.withExpressionAttributeValues(keyCondition);
query.setLimit(2);

QueryResultPage<DynamoModel> resultPage = mapper.queryPage(DynamoModel.class, query);

有人知道为什么在到达与 KeyCondition 匹配的最后一项时 lastEvaluatedKey 不为空吗?当我只保存符合条件的项目时,LastEvaluatedKey 如预期的那样为 null。

最佳答案

这是 DynamoDB 的预期行为。

If LastEvaluatedKey is not empty, it does not necessarily mean that there is more data in the result set. The only way to know when you have reached the end of the result set is when LastEvaluatedKey is empty. (source)

这是 AWS 的设计决定。我能想到的最可能的解释是,为了拥有一个 LastEvaluatedKey 当且仅当有更多项目时,他们会不断扫描以找到更多项目,并且如果您使用过滤器表达式,他们可能必须扫描分区的其余部分以确定是否还有更多项目。这是一个有助于最小化查询(和扫描)操作延迟的选择。

关于java - DynamoDB 分页 - 最后评估的键在最后一页不为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53653552/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com