0
0
DynamoDBquery~5 mins

BatchWriteItem in DynamoDB

Choose your learning style9 modes available
Introduction

BatchWriteItem lets you add or delete many items in a DynamoDB table at once. It saves time by doing many changes in a single request.

You want to add multiple new records to your database quickly.
You need to delete several items from a table in one go.
You want to update many items by deleting old ones and adding new versions.
You are importing a large list of data into DynamoDB.
You want to reduce the number of requests to DynamoDB for efficiency.
Syntax
DynamoDB
BatchWriteItem
{
  RequestItems: {
    "TableName1": [
      { "PutRequest": { "Item": { /* item attributes */ } } },
      { "DeleteRequest": { "Key": { /* primary key attributes */ } } }
    ],
    "TableName2": [
      { "PutRequest": { "Item": { /* item attributes */ } } }
    ]
  }
}

You can mix PutRequest and DeleteRequest in the same batch.

Each batch can write up to 25 items or 16 MB of data.

Examples
This example adds one book and deletes another in the "Books" table.
DynamoDB
BatchWriteItem
{
  RequestItems: {
    "Books": [
      { "PutRequest": { "Item": { "ISBN": "12345", "Title": "Learn SQL" } } },
      { "DeleteRequest": { "Key": { "ISBN": "54321" } } }
    ]
  }
}
This example adds two new users to the "Users" table.
DynamoDB
BatchWriteItem
{
  RequestItems: {
    "Users": [
      { "PutRequest": { "Item": { "UserID": "u1", "Name": "Alice" } } },
      { "PutRequest": { "Item": { "UserID": "u2", "Name": "Bob" } } }
    ]
  }
}
Sample Program

This request adds two movies and deletes one movie from the "Movies" table in a single batch.

DynamoDB
BatchWriteItem
{
  RequestItems: {
    "Movies": [
      { "PutRequest": { "Item": { "MovieID": "m1", "Title": "Inception", "Year": 2010 } } },
      { "PutRequest": { "Item": { "MovieID": "m2", "Title": "Interstellar", "Year": 2014 } } },
      { "DeleteRequest": { "Key": { "MovieID": "m3" } } }
    ]
  }
}
OutputSuccess
Important Notes

If some items are not processed, they appear in UnprocessedItems. You can retry those.

BatchWriteItem does not support update operations directly; use delete and put to update.

Summary

BatchWriteItem lets you add or delete many items in one request.

It helps save time and reduce the number of requests.

Remember to check for unprocessed items and retry if needed.