I'm creating REST based api, for a client to push data to dynamodb in aws. sample code below. I want to add a functionality, where client can post items in bulk, say 100 item, and i will validate the data and push to dynamo db. say a json payload with 100 items, i iterate over the items and call put_item function. any examples around this would be great?
def add_item(self, item):
try:
if item.get("id") is NOT None:
table.put_item(Item=item)
except ClientError as err:
logger.exception("Add item failed in table %s.")
raise err
The most efficient way of doing this is using BatchWriteItems which allows you to put 25 items in a single requests. However, as you use boto3, there is a wrapper that allows you to supply as many items as you like, and will do the batches for you.
with table.batch_writer() as batch:
for _ in range(1000000):
batch.put_item(Item={'HashKey': '...',
'Otherstuff': '...'})
# You can also delete_items in a batch.
batch.delete_item(Key={'HashKey': 'SomeHashKey'})