【问题标题】:Amazon DynamoDB scan is not scanning complete tableAmazon DynamoDB 扫描未扫描完整表
【发布时间】:2021-05-25 23:21:07
【问题描述】:

我正在尝试扫描和更新我的 Amazon DynamoDB 表中具有特定属性值的所有条目,这将是一次性操作,我查询的参数不是索引。

如果我理解正确,我唯一的选择是扫描整个 Amazon DynamoDB 表,每当遇到该条目时,我都应该更新它们。

我的表大小约为 2 GB,我的表有超过 850 万条记录。

下面是我的脚本的 sn-p:

scan_kwargs = {
    'FilterExpression': Key('someKey').eq(sometargetNumber)
}
matched_records = my_table.scan(**scan_kwargs)

print 'Number of records impacted by this operations: ' + str(matched_records['Count'])
user_response = raw_input('Would you like to continue?\n')

if user_response == 'y':
    for item in matched_records['Items']:
        print '\nTarget Record:'
        print(item)
        updated_record = my_table.update_item(
            Key={
                'sessionId': item['attr0']
            },
            UpdateExpression="set att1=:t, att2=:s, att3=:p, att4=:k, att5=:si",
            ExpressionAttributeValues={
                ':t': sourceResponse['Items'][0]['att1'],
                ':s': sourceResponse['Items'][0]['att2'],
                ':p': sourceResponse['Items'][0]['att3'],
                ':k': sourceResponse['Items'][0]['att4'],
                ':si': sourceResponse['Items'][0]['att5']
            },
            ReturnValues="UPDATED_NEW"
        )
        print '\nUpdated Target Record:'
        print(updated_record)
else:
    print('Operation terminated!')

我在 TEST 环境(

我是否需要以不同的方式执行扫描,我是否遗漏了什么?还是只是 dynamoDB 中“扫描”操作的限制?

【问题讨论】:

  • scan只返回最大1MB的数据,需要通过LastEvaluatedKey继续调用api获取更多。
  • @BaluVyamajala 是的,我相信这会奏效,谢谢!

标签: amazon-web-services amazon-dynamodb


【解决方案1】:

听起来您的问题与 DynamoDB 如何过滤数据和对结果进行分页有关。要查看此处发生的情况,请考虑在过滤时执行 DynamoDB 扫描/查询操作时的操作顺序。 DynamoDB 执行以下操作in this order

  1. 从表中读取项目
  2. 应用过滤器
  3. 返回结果

DynamoDB queryscan 操作一次最多返回 1MB 的数据。除此之外的任何内容都将被分页。如果 DynamoDB 在您的响应中返回 LastEvaluatedKey 元素,您就知道您的结果正在分页。

过滤器在 1MB 限制之后应用。这是经常让人们措手不及的关键步骤。在您的情况下,正在发生以下情况:

您执行从表中读取 1MB 数据的扫描操作。 您对 1MB 响应应用过滤器,这会导致第一步中的所有记录都从响应中删除。 DDB 返回带有 LastEvaluatedKey 元素的剩余项目,这表明有更多数据要搜索。 换句话说,您的过滤器不适用于整个表格。它一次应用于 1MB 的表。为了获得您要查找的结果,您将需要重复执行扫描操作,直到到达表的最后一个“页面”。

【讨论】:

  • 是的,这可行,我们如何确定它已到达所有 850 万条记录的最后一页?是否有任何特定的 json 密钥 DynamoDB 返回?想知道即使是最后一页也会有 LastEvaluatedKey 吗?感谢您的帮助
  • 当有更多页面要获取时,DynamoDB 将返回 LastEvaluatedKey。到达分页结果的末尾后,LastEvaluatedKey 字段将不存在。更多信息:docs.aws.amazon.com/amazondynamodb/latest/developerguide/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-01
  • 2016-10-15
  • 1970-01-01
  • 2018-01-10
相关资源
最近更新 更多