Categories
DynamoDB tutorial

Insert items in DynamoDB table

In the last section of the tutorial, we learned how we can create a table in DynamoDB. In this section, we will be learning to insert an item into the DynamoDB table we created.

The table we created has the following structure:

TableName: books
PartitionKey: Author
SortKey: Category

In this tutorial we will be using the following 3 mechanisms to insert data:

  1. AWS DynamoDB console(UI)
  2. AWS CLI
  3. AWS SDK GoLang

AWS console to insert data into the DynamoDB table

This is a very simple and straight forward way to insert data into the books table we created.

Go to AWS > Services > DynamoDB > Tables

There select the books table that you created and go to the items tab. Click create item and insert the values you want to add to the table. You can add any attributes to the table. The attributes you did not define during the table creation can also be added.

Insert Data into the DynamoDB table

As we can see we can add the name of the book even though we did not specify it during the table creation.

Insert Item In DynamoDB table using AWS CLI

To insert an item in DynamoDB table using AWS CLI we need AWS CLI installed. With AWS CLI in place, we can now use the following command to insert an item:

aws dynamodb put-item \

    --table-name books \

    --item '{

        "Author": {"S": "War and Peace"},

        "Category": {"S": "Novel"} ,

        "Title": {"S": "Leo Tolstoy"} }'

This will insert the item into the DynamoDB table.

Here we only need to run the put-item command with two arguments

— table-name – Specify the table name where you want to insert the data
— item – The items you want to insert into the table. This needs to be passed in as a JSON. The format is:

“<FieldName>”: {“<field tyoe>”: “<Value>”}

Insert item using a file to pass in arguments

Instead of defining the arguments in the CLI command we can use a file to define them and reference the file in the CLI command:

aws dynamodb put-item --table-name Books --item file://item.json

Here the items can now be defined in a file. If there are a lot of attributes writing all of them in the CLI command can be difficult to read. So for simplicity, we can move them in a file. The file with items looks like:

{
    "Author": {"S": "Leo Tolstoy"},
    "Category": {"S": "Novel"},
    "Title": {"S": "War and Peace"}
}

Insert item in DyanmoDB table using Golang SDK

We can use Golang or any other AWS supported SDK to insert an item into DynamoDB. Let us look at how we can use Golang to insert the item into DynamoDB:

// insert.go
svc := dynamodb.New(session.New())
input := &dynamodb.PutItemInput{
    Item: map[string]*dynamodb.AttributeValue{
        "Author": {
            S: aws.String("Leo Tolstoy"),
        },
        "Category": {
            S: aws.String("Novel"),
        },
        "Title": {
            S: aws.String("War and Peace"),
        },
    },
    TableName: aws.String("books"),
}

result, err := svc.PutItem(input)
if err != nil {
  fmt.Println(err)
  return
}
fmt.Println(result)
> Make sure you have imported the right dependencies
and ensure that you have the right session for AWS.

DynamoDB by default overrides the item if its primary key already exists. You can also specify a condition that needs to be fulfilled before inserting the item into the table. One of those conditions is the primary key already exists. For this, you can add an extra argument --condition-expression to ensure that the item does not replace it when you provide the same ID.

--condtion-expression attribute_not_exists(Id) in the CLI command specified above.

Learn more about the condition expression here.

Batch Insert Items Into DynamoDB table

We can also insert a group of items into the table using the batch insert command. This can be done using CLI as well as AWS SDK. In this tutorial, we will be using the AWS CLI command.

Similar to single insert, batch item insert can be done with a file or by specifying the items in the command itself. Here we will be using a file to insert a batch of items. The following command is used to do batch write of items: You can insert data into multiple tables using batch insert.

aws dynamodb batch-write-item --request-items file://books.json

Let us create a file called books.json where we will write the items we want to insert into the table:

// books.json

 {     "books": [
          {
              "PutRequest": {
                  "Item": {
                      "Author": {"S": "Leo Tolstoy"},
                      "Category": {"S": "Novel"},
                      "Title": {"S": "War and Peace"}
                  }
              }         
          },
          {
              "PutRequest": {
                  "Item": {
                      "Author": {"S": "Daniel Kahneman"},
                      "Category": {"S": "Self help"},
                      "Title": {"S": "Thinking, Fast and Slow"}
                   }
              }
          }
 }

In the next section, we will be learning how we can query the items from a DynamoDB table.

Leave a Reply

Your email address will not be published. Required fields are marked *