Build an Application Using a NoSQL Key-Value Data Store
GETTING STARTED GUIDE
Module 4: Modifying items
Use the API to update attributes of existing items and remove items
Introduction
This module demonstrates how to update an attribute of an item in our table, for example, when we need to update the format of a book because a new audio version has been released. We need to add this new format to our product catalog. In some cases, we may need to remove a format if it is no longer available. The steps below show how to make these changes.
Time to Complete
12 minutes
Prerequisites
- An AWS account: if you don't already have one, follow the Setting Up Your Environment getting started guide for a quick overview.
- An installed version of the AWS SDK via pip install boto3
Implementation
Step 1: Update
import boto3
dynamodb = boto3.client('dynamodb', region_name='us-east-1')
resp = dynamodb.execute_statement(Statement='UPDATE Books
SET Formats.Audiobook = \'JCV555\'
WHERE
Author = \'Antje Barth\' AND Title = \'Data Science on AWS\'')
print(resp['Items'])
Step 2: Remove
import boto3
dynamodb = boto3.client('dynamodb', region_name='us-east-1')
resp = dynamodb.execute_statement(Statement='UPDATE Books
REMOVE Formats.Audiobook
WHERE
Author = \'Antje Barth\' AND Title = \'Data Science on AWS\'')
print(resp['Items'])
Conclusion
In this module you used the DynamoDB API to update and remove attributes of items in our product catalog.
Up Next: Cleanup and next steps