# DynamoDB code examples for the SDK for Python ## Overview Shows how to use the AWS SDK for Python (Boto3) to work with Amazon DynamoDB. *DynamoDB is a fully managed NoSQL database service that provides fast and predictable performance with seamless scalability.* ## ⚠ Important * Running this code might result in charges to your AWS account. For more details, see [AWS Pricing](https://aws.amazon.com/pricing/?aws-products-pricing.sort-by=item.additionalFields.productNameLowercase&aws-products-pricing.sort-order=asc&awsf.Free%20Tier%20Type=*all&awsf.tech-category=*all) and [Free Tier](https://aws.amazon.com/free/?all-free-tier.sort-by=item.additionalFields.SortRank&all-free-tier.sort-order=asc&awsf.Free%20Tier%20Types=*all&awsf.Free%20Tier%20Categories=*all). * Running the tests might result in charges to your AWS account. * We recommend that you grant your code least privilege. At most, grant only the minimum permissions required to perform the task. For more information, see [Grant least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege). * This code is not tested in every AWS Region. For more information, see [AWS Regional Services](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services). ## Code examples ### Prerequisites For prerequisites, see the [README](../../README.md#Prerequisites) in the `python` folder. Install the packages required by these examples by running the following in a virtual environment: ``` python -m pip install -r requirements.txt ``` ### Single actions Code excerpts that show you how to call individual service functions. * [Create a table](GettingStarted/scenario_getting_started_movies.py#L75) (`CreateTable`) * [Delete a table](GettingStarted/scenario_getting_started_movies.py#L295) (`DeleteTable`) * [Delete an item from a table](GettingStarted/scenario_getting_started_movies.py#L278) (`DeleteItem`) * [Get a batch of items](batching/dynamo_batching.py#L62) (`BatchGetItem`) * [Get an item from a table](GettingStarted/scenario_getting_started_movies.py#L177) (`GetItem`) * [Get information about a table](GettingStarted/scenario_getting_started_movies.py#L48) (`DescribeTable`) * [List tables](GettingStarted/scenario_getting_started_movies.py#L107) (`ListTables`) * [Put an item in a table](GettingStarted/scenario_getting_started_movies.py#L153) (`PutItem`) * [Query a table](GettingStarted/scenario_getting_started_movies.py#L226) (`Query`) * [Run a PartiQL statement](partiql/scenario_partiql_single.py#L41) (`ExecuteStatement`) * [Run batches of PartiQL statements](partiql/scenario_partiql_batch.py#L42) (`BatchExecuteStatement`) * [Scan a table](GettingStarted/scenario_getting_started_movies.py#L245) (`Scan`) * [Update an item in a table](GettingStarted/scenario_getting_started_movies.py#L198) (`UpdateItem`) * [Write a batch of items](GettingStarted/scenario_getting_started_movies.py#L128) (`BatchWriteItem`) ### Scenarios Code examples that show you how to accomplish a specific task by calling multiple functions within the same service. * [Accelerate reads with DynamoDB Accelerator](TryDax/01-create-table.py) * [Get started with tables, items, and queries](GettingStarted/scenario_getting_started_movies.py) * [Query a table by using batches of PartiQL statements](partiql/scenario_partiql_batch.py) * [Query a table using PartiQL](partiql/scenario_partiql_single.py) ### Cross-service examples Sample applications that work across multiple AWS services. * [Create a REST API to track COVID-19 data](../../cross_service/apigateway_covid-19_tracker) * [Create a messenger application](../../cross_service/stepfunctions_messenger) * [Create a web application to track DynamoDB data](../../cross_service/dynamodb_item_tracker) * [Create a websocket chat application](../../cross_service/apigateway_websocket_chat) ## Run the examples ### Instructions #### Accelerate reads with DynamoDB Accelerator This example shows you how to do the following: * Create and write data to a table with both the DynamoDB Accelerator and SDK clients. * Get, query, and scan the table with both clients and compare their performance. Start the example by running the following at a command prompt: ``` python TryDax/01-create-table.py ``` To run the scripts with the DAX client, you must run them on an Amazon Elastic Compute Cloud (Amazon EC2) instance within your virtual private cloud (VPC). This process is described in the Python sample application tutorial in the [Developing with the DAX Client](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DAX.client.html) section of the *Amazon DynamoDB Developer Guide*. The tutorial shows you how to set up the following additional resources: - A VPC from Amazon Virtual Private Cloud (Amazon VPC) - A DAX cluster set up in your VPC - An EC2 instance running in your VPC with the following installed: - Python 3.7 or later - Boto3 1.11.10 or later - Amazon DAX Client for Python 1.1.7 or later On an EC2 instance, run the get item, query, and scan test scripts with the DAX client by specifying a DAX cluster endpoint as the first positional argument. To run the query test script with the DAX client, run the following from a command prompt window. Start the example by running the following at a command prompt: ```commandline python 04-query-test.py YOUR-CLUSTER-NAME.111111.clustercfg.dax.usw2.cache.amazonaws.com:8111 ``` #### Get started with tables, items, and queries This example shows you how to do the following: * Create a table that can hold movie data. * Put, get, and update a single movie in the table. * Write movie data to the table from a sample JSON file. * Query for movies that were released in a given year. * Scan for movies that were released in a range of years. * Delete a movie from the table, then delete the table. Start the example by running the following at a command prompt: ``` python GettingStarted/scenario_getting_started_movies.py ``` #### Query a table by using batches of PartiQL statements This example shows you how to do the following: * Get a batch of items by running multiple SELECT statements. * Add a batch of items by running multiple INSERT statements. * Update a batch of items by running multiple UPDATE statements. * Delete a batch of items by running multiple DELETE statements. Start the example by running the following at a command prompt: ``` python partiql/scenario_partiql_batch.py ``` #### Query a table using PartiQL This example shows you how to do the following: * Get an item by running a SELECT statement. * Add an item by running an INSERT statement. * Update an item by running an UPDATE statement. * Delete an item by running a DELETE statement. Start the example by running the following at a command prompt: ``` python partiql/scenario_partiql_single.py ``` ### Tests ⚠ Running tests might result in charges to your AWS account. To find instructions for running these tests, see the [README](../../README.md#Tests) in the `python` folder. ## Additional resources * [DynamoDB Developer Guide](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Introduction.html) * [DynamoDB API Reference](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/Welcome.html) * [SDK for Python DynamoDB reference](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/dynamodb.html) --- Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. SPDX-License-Identifier: Apache-2.0