Creating Items in Bulk Using CLI

Write the request file and use BatchWriteItem API for creating items.

We can perform two operations on a bulk of items, using this API. These operations are:

1. Write

2. Delete

These operations work similarly to the individual put and delete operations. In this lesson, we will discuss how to put items using batch-write-item. I would highly recommend that we figure out how to delete items on our own.

Creating a request file

Although you don’t need a request file, it is a convenient source for creating items. To create a new batch.json file, write the following command in the terminal:

vi batch.json

Then, paste the following JSON content into the file. To edit the file, press i when the editor opens.

{
  "Blog" : [
  {
    "PutRequest": {
      "Item": {
         "Author" : {"S":"Anshul"},
         "Topic_Title" : {"S":"DynamoDB, Introduction To AWS CLI"}
      }
    }
  },
  {
    "PutRequest": {
      "Item": {
         "Author" : {"S":"Neil Patel"},
         "Topic_Title" : {"S":"Marketing, 33 Things to Learn from 33 Top Marketing Blogs"},
         "Website" : {"S": "www.neilpatel.com"}
      }
    }
  },
  {
    "PutRequest": {
      "Item": {
         "Author" : {"S":"Neil Patel"},
         "Topic_Title" : {"S":"Entrepreneurship, Bluehost Vs. Hostgator"},
         "Website" : {"S": "www.neilpatel.com"}
      }
    }
  }
  ]
}

After pasting the JSON content, press “ESC” to exit the edit mode and then “Shift+Z+Z” to exit the editor. We’ll use this file batch.json in the API call.

Calling the BatchWriteItem API

Now that we have created the “request file”, it’s time to have some fun! Call the function given below to write the items into our DynamoDB table.

aws dynamodb batch-write-item \
	--request-items file://batch.json \
	--return-consumed-capacity INDEXES \
	--return-item-collection-metrics SIZE

Practice

Please use the below terminal to play around with the commands mentioned above.

Get hands-on with 1200+ tech skills courses.