简体   繁体   中英

how to create/update item in dynamo db?

I tried a sample code ( see below), to create new items in dynamo db. based on the docs for dynamodb and boto3, the sample code adds the item in dynamodb in batch, but just from the code, it looks like put item is being called in each iteration of the for loop below. any thoughts, also, i understand for updating item, there is no batch operation, we have to call update item one at a time?

import boto3
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('my-table')

with table.batch_writer() as writer:
    for item in somelist:
        writer.put_item(Item=item)

The boto3 batch writer buffers internally and sends each batch automatically. It's like magic.

Note that you called the put_item() method on the writer object. This writer object is a batch writer - it is a wrapper of the original table object. This wrapper doesn't perform every put_item() request individually, Instead,

  • As its name suggests, the batch writer collects batches of up to 25 writes in memory, and only on the 25th call, it sends all 25 writes as one DynamoDB BatchWriteItem request.
  • Then, at the end of the loop, the writer object is destroyed when the the with block ends, and this sends the final partial batch as one last BatchWriteItem request.

As you can see, Python made efficient writing using batches very transparent and easy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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