分页在 AWS 中的工作原理是什么?
DynamoDB 对 Scan 操作的结果进行分页。带分页,
扫描结果分为 1 MB 的数据“页面”
大小(或更小)。应用程序可以处理结果的第一页,
然后是第二页,以此类推。
因此,对于每个请求,如果结果中有更多项目,您将始终获得LastEvaluatedKey。您将使用此LastEvaluatedKey 重新发出扫描请求以获得完整结果。
例如,对于一个示例查询,您有400 结果并且每个结果都提取到上限100 结果,您将不得不重新发出扫描请求,直到lastEvaluatedKey 返回为空。您将执行以下操作。 documentation
var result *ScanOutput
for{
if(len(resultLastEvaluatedKey) == 0){
break;
}
input := & ScanInput{
ExclusiveStartKey= LastEvaluatedKey
// Copying all parameters of original scanInput request
}
output = dynamoClient.Scan(input)
}
AWS-CLI 上的页面大小是多少?
scan 操作扫描所有 dynamoDB 并根据过滤器返回结果。通常,AWS CLI 会自动处理分页。AWS CLI 会不断为我们重新发出扫描请求。这种请求和响应模式一直持续到最终响应为止。
page-size 专门告诉一次只扫描数据库表中的page-size 行数并过滤这些行。如果未扫描完整表或结果超过1MB,则结果将发送lastEvaluatedKey,cli 将重新发出请求。
这是来自documentation 的示例请求响应。
aws dynamodb scan \
--table-name Movies \
--projection-expression "title" \
--filter-expression 'contains(info.genres,:gen)' \
--expression-attribute-values '{":gen":{"S":"Sci-Fi"}}' \
--page-size 100 \
--debug
b'{"Count":7,"Items":[{"title":{"S":"Monster on the Campus"}},{"title":{"S":"+1"}},
{"title":{"S":"100 Degrees Below Zero"}},{"title":{"S":"About Time"}},{"title":{"S":"After Earth"}},
{"title":{"S":"Age of Dinosaurs"}},{"title":{"S":"Cloudy with a Chance of Meatballs 2"}}],
"LastEvaluatedKey":{"year":{"N":"2013"},"title":{"S":"Curse of Chucky"}},"ScannedCount":100}'
我们可以清楚地看到scannedCount:100 和过滤计数Count:7,因此在扫描的 100 个项目中只有 7 个项目被过滤。 documentation
来自 Limit 的 Documentation
// The maximum number of items to evaluate (not necessarily the number of matching
// items). If DynamoDB processes the number of items up to the limit while processing
// the results, it stops the operation and returns the matching values up to
// that point, and a key in LastEvaluatedKey to apply in a subsequent operation,
// so that you can pick up where you left off.
所以基本上,page-size 和 limit 是相同的。 Limit 将限制一个扫描请求中扫描的行数。