skip to Main Content

When I use boto3 I can paginate if I am making a query or scan

Is it possible to do the same with put_item?

2

Answers


  1. Paginating is when DynamoDB reaches its maximum of 1MB response size or it you are using --limit. It allows you to get the next "page" of data.

    That does not make sense with a PutItem as you are simply putting a single item.

    If what you mean is you want to put more than 1 item at a time, then use BatchWriteItem API where you can pass in a batch of up to 25 items.

    You can also use high level interfaces like the batch_writer in boto3 where you can give it a list of items any size and it breaks the list into chunks of 25 for you and writes those batches while also handling any retry logic:

    import boto3
    
    dynamodb = boto3.resource("dynamodb")
    table = dynamodb.Table("name")
    
    with table.batch_writer() as batch_writer:
       for _ in range(1000):
            batch_writer.put_item(Item=myitem)  
    

    https://boto3.amazonaws.com/v1/documentation/api/latest/guide/dynamodb.html#

    Login or Signup to reply.
  2. The closest to "paginating" PutItem with boto3 is probably the included BatchWriter class and associated context manager. This class handles buffering and sending items in batches. Aside from PutItem, it supports DeleteItem as well.

    Here is an example of how to use it:

    import boto3
    
    dynamodb = boto3.resource("dynamodb")
    table = dynamodb.Table("name")
    
    with table.batch_writer() as batch_writer:
       for _ in range(1000):
            batch_writer.put_item(Item={"HashKey": "...",
                                        "Otherstuff": "..."}) 
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search