Menu
Amazon DynamoDB
Getting Started Guide (API Version 2012-08-10)

Step 3: Write Items to the Table

When you write an item to a DynamoDB table, only the primary key attribute(s) are required. Other than the primary key, the table does not require a schema. In this section, you write an item to a table (PutItem operation), write an item conditionally, and also write multiple items in a single operation (BatchWriteItem operation).

Step 3.1: Write a Single Item

Use the PutItem API operation to write an item.

  1. Replace everything in the left side of the DynamoDB JavaScript shell window with the following code:

    
    var params = {
        TableName: "Music",
        Item: {
            "Artist":"No One You Know",
            "SongTitle":"Call Me Today",
            "AlbumTitle":"Somewhat Famous",
            "Year": 2015,
            "Price": 2.14,
            "Genre": "Country",
            "Tags": {
                "Composers": [
                      "Smith",
                      "Jones",
                      "Davis"
                ],
                "LengthInSeconds": 214
            }
        }
    };
    
    docClient.put(params, function(err, data) {
        if (err)
            console.log(JSON.stringify(err, null, 2));
        else
            console.log(JSON.stringify(data, null, 2));
    });
    
    
  2. Click the play button arrow to run the code. If the write is successful, the response is an empty map: {}

Note the following about the item you just added:

  • Artist and SongTitle are primary key attributes (partition key and sort key, respectively). Both are of string type. Every item that you add to the table must have values for these attributes.

  • Other attributes are AlbumTitle (string), Year (number), Price (number), Genre (string), and Tags (map).

  • DynamoDB allows you to nest attributes within other attributes. The Tags map contains two nested attributesComposers (list) and LengthInSeconds (number).

  • Artist, SongTitle, AlbumTitle, Year, Price, Genre, and Tags are top-level attributes because they are not nested within any other attributes.

For more information, see Data Types in the Amazon DynamoDB Developer Guide.

Step 3.2: Perform a Conditional Write

By default, PutItem does not check first to see if there is already an item with the same key, it simply overwrites any existing item. If you want to ensure that you do not overwrite an existing item, you can add a ConditionExpression parameter. This is a logical condition that must be satisfied in order for the write to succeed.

In this step, you try to write the same item, but this time you specify a condition to see whether an item with the same primary key already exists. The write fails because there is already an item in the table with the same primary key.

  1. Modify the params object so that it looks like this:

    
    var params = {
        TableName: "Music",
        Item: {
            "Artist":"No One You Know",
            "SongTitle":"Call Me Today",
            "AlbumTitle":"Somewhat Famous",
            "Year": 2015,
            "Price": 2.14,
            "Genre": "Country",
            "Tags": {
                "Composers": [
                      "Smith",
                      "Jones",
                      "Davis"
                ],
                "LengthInSeconds": 214
            }
        },
        "ConditionExpression": "attribute_not_exists(Artist) and attribute_not_exists(SongTitle)"
    };
    

    Note

    The only difference is the ConditionExpression parameter. This prevents you from overwriting the item. if there is already an item in the table with the same primary key values (No One You Know, Call Me Today).

    For more information, see Condition Expressions in the Amazon DynamoDB Developer Guide.

  2. Click the play button arrow to run the code.

    The conditional write fails because the item already exists.

Step 3.3: Write Multiple Items

You can use the BatchWriteItem operation to perform multiple writes in one step. The following code adds several items, with the required primary key attributes, but different non-key attributes.

  1. Replace everything in the left side of the DynamoDB JavaScript shell window with the following code:

    
    var params = {
        RequestItems: {
            "Music": [ 
                {  
                    PutRequest: {
                        Item: {
                            "Artist": "No One You Know",
                            "SongTitle": "My Dog Spot",
                            "AlbumTitle":"Hey Now",
                            "Price": 1.98,
                            "Genre": "Country",
                            "CriticRating": 8.4
                        }
                    }
                }, 
                { 
                    PutRequest: {
                        Item: {
                            "Artist": "No One You Know",
                            "SongTitle": "Somewhere Down The Road",
                            "AlbumTitle":"Somewhat Famous",
                            "Genre": "Country",
                            "CriticRating": 8.4,
                            "Year": 1984
                        }
                    }
                }, 
                { 
                    PutRequest: {
                        Item: {
                            "Artist": "The Acme Band",
                            "SongTitle": "Still In Love",
                            "AlbumTitle":"The Buck Starts Here",
                            "Price": 2.47,
                            "Genre": "Rock",
                            "PromotionInfo": {
                                "RadioStationsPlaying":[
                                    "KHCR", "KBQX", "WTNR", "WJJH"
                                ],
                                "TourDates": {
                                    "Seattle": "20150625",
                                    "Cleveland": "20150630"
                                },
                                "Rotation": "Heavy"
                            }
                        }
                    }
                }, 
                { 
                    PutRequest: {
                        Item: {
                            "Artist": "The Acme Band",
                            "SongTitle": "Look Out, World",
                            "AlbumTitle":"The Buck Starts Here",
                            "Price": 0.99,
                            "Genre": "Rock"
                        }
                    }
                }
            ]
        }
    };
    
    docClient.batchWrite(params, function (err, data) {
        if (err)
            console.log(JSON.stringify(err, null, 2));
        else
            console.log(JSON.stringify(data, null, 2));
    });
    
    
  2. Click the play button arrow to run the code. If the batch write is successful, the response contains the following: "UnprocessedItems": {}. This indicates that all of the items in the batch have been written.