繁体   English   中英

DynamoDB 客户端不满足限制

[英]DynamoDB client doesn't fulfil the limit

客户端库:“@aws-sdk/client-dynamodb”:“3.188.0”

我有一个 DynamoDB 分页实现。

我的用户数是 98,页面大小是 20。因此我期望有 5 个页面,每个页面有 20、20、20、20 和 18 个用户。

但实际上我得到了 5 个以上的页面,每个页面都有可变数量的用户,如 10、12、11 等。

如何让用户获得适当的页面限制,如 20、20、20、20 和 18?

public async pagedList(usersPerPage: number, lastEvaluatedKey?: string): Promise<PagedUser> {

      const params = {
         TableName: tableName,
         Limit: usersPerPage,
         FilterExpression: '#type = :type',
         ExpressionAttributeValues: {
            ':type': { S: type },
         },
         ExpressionAttributeNames: {
            '#type': 'type',
         },
      } as ScanCommandInput;

      if (lastEvaluatedKey) {
         params.ExclusiveStartKey = { 'oid': { S: lastEvaluatedKey } };
      }

      const command = new ScanCommand(params);
      const data = await client.send(command);

      const users: User[] = [];
      if (data.Items !== undefined) {
         data.Items.forEach((item) => {
            if (item !== undefined) {
               users.push(this.makeUser(item));
            }
         });
      }

      let lastKey;
      if (data.LastEvaluatedKey !== undefined) {
         lastKey = data.LastEvaluatedKey.oid.S?.valueOf();
      }
      return {
         users: users,
         lastEvaluatedKey: lastKey
      };
   }

扫描命令文档https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Scan.html#Scan.Pagination

提供您的结果可能包含较少结果的几个原因:

  • 结果必须在 1 MB 以内
  • 如果应用过滤器,则数据将在“扫描后”过滤。 您的查询中有一个过滤器。

从文档

在扫描完成之后但在返回结果之前应用过滤器表达式。 因此,无论是否存在过滤器表达式,扫描都会消耗相同数量的读取容量。

...

现在假设您向 Scan 添加了一个过滤器表达式。 在这种情况下,DynamoDB 将筛选表达式应用于返回的六个项目,丢弃不匹配的项目。 最终扫描结果包含六个或更少的项目,具体取决于过滤的项目数。

在下一节中,将解释如何验证它是否可能是您的情况:

计算结果中的项目

除了符合您的条件的项目外,扫描响应还包含以下元素:

ScannedCount — 在应用任何ScanFilter之前评估的项目数。 具有很少或没有Count结果的高ScannedCount值表示扫描操作效率低下。 如果您没有在请求中使用过滤器,则ScannedCountCount相同。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM