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

Step 3: Create, Read, Update, and Delete an Item

In this step, you perform read and write operations on an item in the Movies table.

To learn more about reading and writing data, see Working with Items in the Amazon DynamoDB Developer Guide.

Step 3.1: Create a New Item

In this step, you add a new item to the table.

  1. Copy the following program into a file named MoviesItemOps01.rb:

    require "aws-sdk-core"
    
    Aws.config.update({
      region: "us-west-2",
      endpoint: "http://localhost:8000"
    })
    
    dynamodb = Aws::DynamoDB::Client.new
    
    tableName = 'Movies'
    
    year = 2015
    title = "The Big New Movie"
    
    item = {
        year: year,
        title: title,
        info: {
                plot: "Nothing happens at all.",
                rating: 0
        }
    }
    
    params = {
        table_name: "Movies",
        item: item
    }
    
    begin
        result = dynamodb.put_item(params)
        puts "Added item: #{year}  - #{title}"
    
    rescue  Aws::DynamoDB::Errors::ServiceError => error
        puts "Unable to add item:"
        puts "#{error.message}"
    end
    

    Note

    The primary key is required. This code adds an item that has primary key (year, title) and info attributes. The info attribute stores a map that provides more information about the movie.

  2. Type the following command to run the program:

    ruby MoviesItemOps01.rb

Step 3.2: Read an Item

In the previous program, you added the following item to the table:

{
   year: 2015,
   title: "The Big New Movie",
   info: { 
        plot: "Nothing happens at all.",
        rating: 0
   }
}

You can use the get_item method to read the item from the Movies table. You must specify the primary key values, so you can read any item from Movies if you know its year and title.

  1. Copy the following program into a file named MoviesItemOps02.rb:

    require "aws-sdk-core"
    
    Aws.config.update({
      region: "us-west-2",
      endpoint: "http://localhost:8000"
    })
    
    dynamodb = Aws::DynamoDB::Client.new
    
    tableName = 'Movies'
    
    year = 2015
    title = "The Big New Movie"
    
    key = {
        year: year,
        title: title
    }
    
    params = {
        table_name: "Movies",
        key: {
            year: year,
            title: title
        }
    }
    
    begin
        result = dynamodb.get_item(params)
        printf "%i - %s\n%s\n%d\n", 
            result.item["year"],
            result.item["title"],
            result.item["info"]["plot"],
            result.item["info"]["rating"]
    
    rescue  Aws::DynamoDB::Errors::ServiceError => error
        puts "Unable to read item:"
        puts "#{error.message}"
    end
    
  2. Type the following command to run the program:

    ruby MoviesItemOps02.rb

Step 3.3: Update an Item

You can use the update_item method to modify an existing item. You can update values of existing attributes, add new attributes, or remove attributes.

In this example, you perform the following updates:

  • Change the value of the existing attributes (rating, plot).

  • Add a new list attribute (actors) to the existing info map.

The item changes from:

{
   year: 2015,
   title: "The Big New Movie",
   info: { 
        plot: "Nothing happens at all.",
        rating: 0
   }
}

To the following:

{
   year: 2015,
   title: "The Big New Movie",
   info: { 
           plot: "Everything happens all at once.",
           rating: 5.5,
           actors: ["Larry", "Moe", "Curly"]
   }
}

  1. Copy the following program into a file named MoviesItemOps03.rb:

    require "aws-sdk-core"
    
    Aws.config.update({
      region: "us-west-2",
      endpoint: "http://localhost:8000"
    })
    
    dynamodb = Aws::DynamoDB::Client.new
    
    tableName = 'Movies'
    
    year = 2015
    title = "The Big New Movie"
    
    params = {
        table_name: "Movies",
        key: {
            year: year,
            title: title
        },
        update_expression: "set info.rating = :r, info.plot=:p, info.actors=:a",
        expression_attribute_values: {
            ":r" => 5.5,
            ":p" => "Everything happens all at once.", # value <Hash,Array,String,Numeric,Boolean,IO,Set,nil>
            ":a" => ["Larry", "Moe", "Curly"]
        },
        return_values: "UPDATED_NEW"
    }
    
    begin
        result = dynamodb.update_item(params)
        puts "Added item: #{year}  - #{title}"
    
    rescue  Aws::DynamoDB::Errors::ServiceError => error
        puts "Unable to add item:"
        puts "#{error.message}"
    end
    

    Note

    This program uses update_expression to describe all updates you want to perform on the specified item.

    The return_values parameter instructs DynamoDB to return only the updated attributes (UPDATED_NEW).

  2. Type the following command to run the program:

    ruby MoviesItemOps03.rb

Step 3.4: Increment an Atomic Counter

DynamoDB supports atomic counters, where you use the update_item method to increment or decrement the value of an existing attribute without interfering with other write requests. (All write requests are applied in the order in which they were received.)

The following program shows how to increment the rating for a movie. Each time you run it, the program increments this attribute by one.

  1. Copy the following program into a file named MoviesItemOps04.rb:

    require "aws-sdk-core"
    
    Aws.config.update({
      region: "us-west-2",
      endpoint: "http://localhost:8000"
    })
    
    dynamodb = Aws::DynamoDB::Client.new
    
    tableName = 'Movies'
    
    year = 2015
    title = "The Big New Movie"
    
    params = {
        table_name: "Movies",
        key: {
            year: year,
            title: title
        },
        update_expression: "set info.rating = info.rating + :val",
        expression_attribute_values: {
            ":val" => 1
        },
        return_values: "UPDATED_NEW"
    }
    
    begin
        result = dynamodb.update_item(params)
        puts "Updated item. ReturnValues are:"
        result.attributes["info"].each do |key, value| 
            if key == "rating" 
                puts "#{key}: #{value.to_f}" 
            else
                puts "#{key}: #{value}" 
            end
        end
    rescue  Aws::DynamoDB::Errors::ServiceError => error
        puts "Unable to update item:"
        puts "#{error.message}"
    end
    
  2. Type the following command to run the program:

    ruby MoviesItemOps04.rb

Step 3.5: Update an Item (Conditionally)

The following program shows how to use update_item with a condition. If the condition evaluates to true, the update succeeds; otherwise, the update is not performed.

In this case, the item is only updated if there are more than three actors.

  1. Copy the following program into a file named MoviesItemOps05.rb:

    require "aws-sdk-core"
    
    Aws.config.update({
      region: "us-west-2",
      endpoint: "http://localhost:8000"
    })
    
    dynamodb = Aws::DynamoDB::Client.new
    
    tableName = 'Movies'
    
    year = 2015
    title = "The Big New Movie"
    
    params = {
        table_name: "Movies",
        key: {
            year: year,
            title: title
        },
        update_expression: "remove info.actors[0]",
        condition_expression: "size(info.actors) > :num",
        expression_attribute_values: {
            ":num" => 3
        },
        return_values: "UPDATED_NEW"
    }
    
    begin
        result = dynamodb.update_item(params)
        puts "Updated item. ReturnValues are:"
        result.attributes["info"].each do |key, value| 
            if key == "rating" 
                puts "#{key}: #{value.to_f}" 
            else
                puts "#{key}: #{value}" 
            end
        end
    
    rescue  Aws::DynamoDB::Errors::ServiceError => error
        puts "Unable to update item:"
        puts "#{error.message}"
    end
    
  2. Type the following command to run the program:

    ruby MoviesItemOps05.rb

    The program should fail with the following message:

    The conditional request failed

    This is because the movie has three actors in it, but the condition is checking for greater than three actors.

  3. Modify the program so that the ConditionExpression looks like this:

    
        condition_expression: "size(info.actors) >= :num",
    

    The condition is now greater than or equal to 3 instead of greater than 3.

  4. Run the program again. The update_item method should now succeed.

Step 3.6: Delete an Item

You can use the delete_item method to delete one item by specifying its primary key. You can optionally provide a condition_expression to prevent item deletion if the condition is not met.

In the following example, you try to delete a specific movie item if its rating is 5 or less.

  1. Copy the following program into a file named MoviesItemOps06.rb:

    require "aws-sdk-core"
    
    Aws.config.update({
      region: "us-west-2",
      endpoint: "http://localhost:8000"
    })
    
    dynamodb = Aws::DynamoDB::Client.new
    
    tableName = 'Movies'
    
    year = 2015
    title = "The Big New Movie"
    
    params = {
        table_name: "Movies",
        key: {
            year: year,
            title: title
        },
        condition_expression: "info.rating <= :val",
        expression_attribute_values: {
            ":val" => 5
        }
    }
    
    begin
        result = dynamodb.delete_item(params)
        puts "Deleted item."
    
    rescue  Aws::DynamoDB::Errors::ServiceError => error
        puts "Unable to update item:"
        puts "#{error.message}"
    end
    
  2. Type the following command to run the program:

    ruby MoviesItemOps06.rb

    The program should fail with the following message:

    The conditional request failed

    This is because the rating for this particular move is greater than 5.

  3. Modify the program to remove the condition:

    
    params = {
        table_name: "Movies",
        key: {
            year: year,
            title: title
        }
    }
  4. Run the program. Now, the delete succeeds because you removed the condition.

Next Step

Step 4: Query and Scan the Data