【问题标题】:Infinite loop in DynamoDB scanDynamoDB 扫描中的无限循环
【发布时间】:2017-04-10 15:12:53
【问题描述】:

正如Complete scan of dynamoDb with boto3 中所述,我构建了一个解决方案,以使用某些条件对 DynamoDB 表进行全扫描。这是我的代码:

def dynamo_scan(table_name, params_dict):
    dynamo_table = boto3.resource('dynamodb').Table(table_name)
    response = do_scan(dynamo_table, params_dict)
    results = response['Items']
    while response.get('LastEvaluatedKey'):
        print "Iterating"
        print response.get('LastEvaluatedKey')
        params_dict['ExclusiveStartKey'] = response['LastEvaluatedKey']
        response = do_scan(dynamo_table, params_dict)
        results.extend(response['Items'])
    return results

def do_scan(dynamo_table, params_dict):
    return dynamo_table.scan(**params_dict)

但是当代码执行时,它进入了一个无限循环。这是 LastEvaluatedKey 每次迭代的输出:

{u'my_id': u'1698', u'identity': u'2017075002312'}
{u'my_id': u'1883', u'identity': u'85500397082900013318629'}
{u'my_id': u'1698', u'identity': u'2017075002312'}
{u'my_id': u'1883', u'identity': u'85500397082900013318629'}
{u'my_id': u'1698', u'identity': u'2017075002312'}
{u'my_id': u'1883', u'identity': u'85500397082900013318629'}

而且它一直在这两对中循环。

编辑:

我添加了方法 do_scan。

【问题讨论】:

    标签: python amazon-web-services amazon-dynamodb boto3 nosql


    【解决方案1】:

    我不确定您的 do_scan() 究竟做了什么,但这有效:

    def dynamo_scan(table_name, params_dict):
        dynamo_table = boto3.resource('dynamodb').Table(table_name)
        response = dynamo_table.scan(params_dict)
        results = response['Items']
    
        while 'LastEvaluatedKey' in response:
            response = dynamo_table.scan(ExclusiveStartKey=response['LastEvaluatedKey'])
            results += response['Items']
    
        return results
    

    【讨论】:

    • 是的,我添加了我的方法 do_scan。我认为与您的解决方案相同,但我每次都陷入无限循环。
    • 您的数据库中有多少数据?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-20
    • 1970-01-01
    • 2021-02-07
    • 2013-05-21
    • 2011-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多