Complete scan of dynamoDb with boto3

前端 未结 8 1982
有刺的猬
有刺的猬 2020-11-29 04:08

My table is around 220mb with 250k records within it. I\'m trying to pull all of this data into python. I realize this needs to be a chunked batch process and looped throug

8条回答
  •  忘掉有多难
    2020-11-29 05:00

    DynamoDB limits the scan method to 1mb of data per scan.

    Documentation: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/dynamodb.html#DynamoDB.Client.scan

    Here is an example loop to get all the data from a DynamoDB table using LastEvaluatedKey:

    import boto3
    client = boto3.client('dynamodb')
    
    def dump_table(table_name):
        results = []
        last_evaluated_key = None
        while True:
            if last_evaluated_key:
                response = client.scan(
                    TableName=table_name,
                    ExclusiveStartKey=last_evaluated_key
                )
            else: 
                response = client.scan(TableName=table_name)
            last_evaluated_key = response.get('LastEvaluatedKey')
            
            results.extend(response['Items'])
            
            if not last_evaluated_key:
                break
        return results
    
    # Usage
    data = dump_table('your-table-name')
    
    # do something with data
    
    

提交回复
热议问题