可以用 dynamodb.ScanPages 设置页面大小吗?

Can page size be set with dynamodb.ScanPages?

使用 dynamodb 扫描的文档 found here 引用了 AWS CLI 的 page-size 参数。

在查看 go AWS SDK 的文档时,found here,有函数 ScanPages。有一个如何使用该函数的示例,但文档中没有任何地方可以像 AWS CLI 那样指定类似 page-size 的内容。除了假设结果超过 1MB 之外,我无法确定分页是如何发生的,那么根据 go 文档和一般扫描文档,这将被视为一个页面。

我也知道可以在 ScanInput 上设置的 Limit 值,但文档表明只有当每个处理的项目都与过滤器匹配时,该值才可以用作页面大小扫描表达式:

The maximum number of items to evaluate (not necessarily the number of matching items)

有没有办法用 go SDK 设置等同于 page-size 的东西?

分页在 AWS 中的工作原理?

DynamoDB paginates the results from Scan operations. With pagination, the Scan results are divided into "pages" of data that are 1 MB in size (or less). An application can process the first page of results, then the second page, and so on.

因此对于每个请求,如果结果中有更多项目,您将始终获得 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 上的页面大小是多少?

扫描操作扫描所有 dynamoDB 和 returns 根据过滤器的结果。通常,AWS CLI 会处理分页 automatically.The AWS CLI 不断为我们重新发出扫描请求。这种请求和响应模式一直持续到最终响应。

page-size 专门告诉一次只扫描数据库 table 中的 page-size 行数并过滤这些行。如果没有扫描到完整的table或者结果大于1MB结果会发出lastEvaluatedKeycli会重新发出请求

这是来自 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

从极限的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-sizelimit 是一样的。 Limit 将限制一次扫描请求中要扫描的行数。