DynamoDB examples using SDK for C++ - Amazon SDK for C++
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

DynamoDB examples using SDK for C++

The following code examples show you how to perform actions and implement common scenarios by using the Amazon SDK for C++ with DynamoDB.

Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios and cross-service examples.

Scenarios are code examples that show you how to accomplish a specific task by calling multiple functions within the same service.

Each example includes a link to GitHub, where you can find instructions on how to set up and run the code in context.

Get started

The following code examples show how to get started using DynamoDB.

SDK for C++
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

Code for the CMakeLists.txt CMake file.

# Set the minimum required version of CMake for this project. cmake_minimum_required(VERSION 3.13) # Set the AWS service components used by this project. set(SERVICE_COMPONENTS dynamodb) # Set this project's name. project("hello_dynamodb") # Set the C++ standard to use to build this target. # At least C++ 11 is required for the AWS SDK for C++. set(CMAKE_CXX_STANDARD 11) # Use the MSVC variable to determine if this is a Windows build. set(WINDOWS_BUILD ${MSVC}) if (WINDOWS_BUILD) # Set the location where CMake can find the installed libraries for the AWS SDK. string(REPLACE ";" "/aws-cpp-sdk-all;" SYSTEM_MODULE_PATH "${CMAKE_SYSTEM_PREFIX_PATH}/aws-cpp-sdk-all") list(APPEND CMAKE_PREFIX_PATH ${SYSTEM_MODULE_PATH}) endif () # Find the AWS SDK for C++ package. find_package(AWSSDK REQUIRED COMPONENTS ${SERVICE_COMPONENTS}) if (WINDOWS_BUILD AND AWSSDK_INSTALL_AS_SHARED_LIBS) # Copy relevant AWS SDK for C++ libraries into the current binary directory for running and debugging. # set(BIN_SUB_DIR "/Debug") # if you are building from the command line you may need to uncomment this # and set the proper subdirectory to the executables' location. AWSSDK_CPY_DYN_LIBS(SERVICE_COMPONENTS "" ${CMAKE_CURRENT_BINARY_DIR}${BIN_SUB_DIR}) endif () add_executable(${PROJECT_NAME} hello_dynamodb.cpp) target_link_libraries(${PROJECT_NAME} ${AWSSDK_LINK_LIBRARIES})

Code for the hello_dynamodb.cpp source file.

#include <aws/core/Aws.h> #include <aws/dynamodb/DynamoDBClient.h> #include <aws/dynamodb/model/ListTablesRequest.h> #include <iostream> /* * A "Hello DynamoDB" starter application which initializes an Amazon DynamoDB (DynamoDB) client and lists the * DynamoDB tables. * * main function * * Usage: 'hello_dynamodb' * */ int main(int argc, char **argv) { Aws::SDKOptions options; // Optionally change the log level for debugging. // options.loggingOptions.logLevel = Utils::Logging::LogLevel::Debug; Aws::InitAPI(options); // Should only be called once. int result = 0; { Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::DynamoDB::DynamoDBClient dynamodbClient(clientConfig); Aws::DynamoDB::Model::ListTablesRequest listTablesRequest; listTablesRequest.SetLimit(50); do { const Aws::DynamoDB::Model::ListTablesOutcome &outcome = dynamodbClient.ListTables( listTablesRequest); if (!outcome.IsSuccess()) { std::cout << "Error: " << outcome.GetError().GetMessage() << std::endl; result = 1; break; } for (const auto &tableName: outcome.GetResult().GetTableNames()) { std::cout << tableName << std::endl; } listTablesRequest.SetExclusiveStartTableName( outcome.GetResult().GetLastEvaluatedTableName()); } while (!listTablesRequest.GetExclusiveStartTableName().empty()); } Aws::ShutdownAPI(options); // Should only be called once. return result; }
  • For API details, see ListTables in Amazon SDK for C++ API Reference.

Actions

The following code example shows how to use BatchExecuteStatement.

SDK for C++
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

Use batches of INSERT statements to add items.

// 2. Add multiple movies using "Insert" statements. (BatchExecuteStatement) Aws::DynamoDB::DynamoDBClient dynamoClient(clientConfiguration); std::vector<Aws::String> titles; std::vector<float> ratings; std::vector<int> years; std::vector<Aws::String> plots; Aws::String doAgain = "n"; do { Aws::String aTitle = askQuestion( "Enter the title of a movie you want to add to the table: "); titles.push_back(aTitle); int aYear = askQuestionForInt("What year was it released? "); years.push_back(aYear); float aRating = askQuestionForFloatRange( "On a scale of 1 - 10, how do you rate it? ", 1, 10); ratings.push_back(aRating); Aws::String aPlot = askQuestion("Summarize the plot for me: "); plots.push_back(aPlot); doAgain = askQuestion(Aws::String("Would you like to add more movies? (y/n) ")); } while (doAgain == "y"); std::cout << "Adding " << titles.size() << (titles.size() == 1 ? " movie " : " movies ") << "to the table using a batch \"INSERT\" statement." << std::endl; { Aws::Vector<Aws::DynamoDB::Model::BatchStatementRequest> statements( titles.size()); std::stringstream sqlStream; sqlStream << "INSERT INTO \"" << MOVIE_TABLE_NAME << "\" VALUE {'" << TITLE_KEY << "': ?, '" << YEAR_KEY << "': ?, '" << INFO_KEY << "': ?}"; std::string sql(sqlStream.str()); for (size_t i = 0; i < statements.size(); ++i) { statements[i].SetStatement(sql); Aws::Vector<Aws::DynamoDB::Model::AttributeValue> attributes; attributes.push_back( Aws::DynamoDB::Model::AttributeValue().SetS(titles[i])); attributes.push_back(Aws::DynamoDB::Model::AttributeValue().SetN(years[i])); // Create attribute for the info map. Aws::DynamoDB::Model::AttributeValue infoMapAttribute; std::shared_ptr<Aws::DynamoDB::Model::AttributeValue> ratingAttribute = Aws::MakeShared<Aws::DynamoDB::Model::AttributeValue>( ALLOCATION_TAG.c_str()); ratingAttribute->SetN(ratings[i]); infoMapAttribute.AddMEntry(RATING_KEY, ratingAttribute); std::shared_ptr<Aws::DynamoDB::Model::AttributeValue> plotAttribute = Aws::MakeShared<Aws::DynamoDB::Model::AttributeValue>( ALLOCATION_TAG.c_str()); plotAttribute->SetS(plots[i]); infoMapAttribute.AddMEntry(PLOT_KEY, plotAttribute); attributes.push_back(infoMapAttribute); statements[i].SetParameters(attributes); } Aws::DynamoDB::Model::BatchExecuteStatementRequest request; request.SetStatements(statements); Aws::DynamoDB::Model::BatchExecuteStatementOutcome outcome = dynamoClient.BatchExecuteStatement( request); if (!outcome.IsSuccess()) { std::cerr << "Failed to add the movies: " << outcome.GetError().GetMessage() << std::endl; return false; } }

Use batches of SELECT statements to get items.

// 3. Get the data for multiple movies using "Select" statements. (BatchExecuteStatement) { Aws::Vector<Aws::DynamoDB::Model::BatchStatementRequest> statements( titles.size()); std::stringstream sqlStream; sqlStream << "SELECT * FROM \"" << MOVIE_TABLE_NAME << "\" WHERE " << TITLE_KEY << "=? and " << YEAR_KEY << "=?"; std::string sql(sqlStream.str()); for (size_t i = 0; i < statements.size(); ++i) { statements[i].SetStatement(sql); Aws::Vector<Aws::DynamoDB::Model::AttributeValue> attributes; attributes.push_back( Aws::DynamoDB::Model::AttributeValue().SetS(titles[i])); attributes.push_back(Aws::DynamoDB::Model::AttributeValue().SetN(years[i])); statements[i].SetParameters(attributes); } Aws::DynamoDB::Model::BatchExecuteStatementRequest request; request.SetStatements(statements); Aws::DynamoDB::Model::BatchExecuteStatementOutcome outcome = dynamoClient.BatchExecuteStatement( request); if (outcome.IsSuccess()) { const Aws::DynamoDB::Model::BatchExecuteStatementResult &result = outcome.GetResult(); const Aws::Vector<Aws::DynamoDB::Model::BatchStatementResponse> &responses = result.GetResponses(); for (const Aws::DynamoDB::Model::BatchStatementResponse &response: responses) { const Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue> &item = response.GetItem(); printMovieInfo(item); } } else { std::cerr << "Failed to retrieve the movie information: " << outcome.GetError().GetMessage() << std::endl; return false; } }

Use batches of UPDATE statements to update items.

// 4. Update the data for multiple movies using "Update" statements. (BatchExecuteStatement) for (size_t i = 0; i < titles.size(); ++i) { ratings[i] = askQuestionForFloatRange( Aws::String("\nLet's update your the movie, \"") + titles[i] + ".\nYou rated it " + std::to_string(ratings[i]) + ", what new rating would you give it? ", 1, 10); } std::cout << "Updating the movie with a batch \"UPDATE\" statement." << std::endl; { Aws::Vector<Aws::DynamoDB::Model::BatchStatementRequest> statements( titles.size()); std::stringstream sqlStream; sqlStream << "UPDATE \"" << MOVIE_TABLE_NAME << "\" SET " << INFO_KEY << "." << RATING_KEY << "=? WHERE " << TITLE_KEY << "=? AND " << YEAR_KEY << "=?"; std::string sql(sqlStream.str()); for (size_t i = 0; i < statements.size(); ++i) { statements[i].SetStatement(sql); Aws::Vector<Aws::DynamoDB::Model::AttributeValue> attributes; attributes.push_back( Aws::DynamoDB::Model::AttributeValue().SetN(ratings[i])); attributes.push_back( Aws::DynamoDB::Model::AttributeValue().SetS(titles[i])); attributes.push_back(Aws::DynamoDB::Model::AttributeValue().SetN(years[i])); statements[i].SetParameters(attributes); } Aws::DynamoDB::Model::BatchExecuteStatementRequest request; request.SetStatements(statements); Aws::DynamoDB::Model::BatchExecuteStatementOutcome outcome = dynamoClient.BatchExecuteStatement( request); if (!outcome.IsSuccess()) { std::cerr << "Failed to update movie information: " << outcome.GetError().GetMessage() << std::endl; return false; } }

Use batches of DELETE statements to delete items.

// 6. Delete multiple movies using "Delete" statements. (BatchExecuteStatement) { Aws::Vector<Aws::DynamoDB::Model::BatchStatementRequest> statements( titles.size()); std::stringstream sqlStream; sqlStream << "DELETE FROM \"" << MOVIE_TABLE_NAME << "\" WHERE " << TITLE_KEY << "=? and " << YEAR_KEY << "=?"; std::string sql(sqlStream.str()); for (size_t i = 0; i < statements.size(); ++i) { statements[i].SetStatement(sql); Aws::Vector<Aws::DynamoDB::Model::AttributeValue> attributes; attributes.push_back( Aws::DynamoDB::Model::AttributeValue().SetS(titles[i])); attributes.push_back(Aws::DynamoDB::Model::AttributeValue().SetN(years[i])); statements[i].SetParameters(attributes); } Aws::DynamoDB::Model::BatchExecuteStatementRequest request; request.SetStatements(statements); Aws::DynamoDB::Model::BatchExecuteStatementOutcome outcome = dynamoClient.BatchExecuteStatement( request); if (!outcome.IsSuccess()) { std::cerr << "Failed to delete the movies: " << outcome.GetError().GetMessage() << std::endl; return false; } }

The following code example shows how to use BatchGetItem.

SDK for C++
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

//! Batch get items from different Amazon DynamoDB tables. /*! \sa batchGetItem() \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::DynamoDB::batchGetItem( const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::DynamoDB::DynamoDBClient dynamoClient(clientConfiguration); Aws::DynamoDB::Model::BatchGetItemRequest request; // Table1: Forum. Aws::String table1Name = "Forum"; Aws::DynamoDB::Model::KeysAndAttributes table1KeysAndAttributes; // Table1: Projection expression. table1KeysAndAttributes.SetProjectionExpression("#n, Category, Messages, #v"); // Table1: Expression attribute names. Aws::Http::HeaderValueCollection headerValueCollection; headerValueCollection.emplace("#n", "Name"); headerValueCollection.emplace("#v", "Views"); table1KeysAndAttributes.SetExpressionAttributeNames(headerValueCollection); // Table1: Set key name, type, and value to search. std::vector<Aws::String> nameValues = {"Amazon DynamoDB", "Amazon S3"}; for (const Aws::String &name: nameValues) { Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue> keys; Aws::DynamoDB::Model::AttributeValue key; key.SetS(name); keys.emplace("Name", key); table1KeysAndAttributes.AddKeys(keys); } Aws::Map<Aws::String, Aws::DynamoDB::Model::KeysAndAttributes> requestItems; requestItems.emplace(table1Name, table1KeysAndAttributes); // Table2: ProductCatalog. Aws::String table2Name = "ProductCatalog"; Aws::DynamoDB::Model::KeysAndAttributes table2KeysAndAttributes; table2KeysAndAttributes.SetProjectionExpression("Title, Price, Color"); // Table2: Set key name, type, and value to search. std::vector<Aws::String> idValues = {"102", "103", "201"}; for (const Aws::String &id: idValues) { Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue> keys; Aws::DynamoDB::Model::AttributeValue key; key.SetN(id); keys.emplace("Id", key); table2KeysAndAttributes.AddKeys(keys); } requestItems.emplace(table2Name, table2KeysAndAttributes); bool result = true; do { // Use a do loop to handle pagination. request.SetRequestItems(requestItems); const Aws::DynamoDB::Model::BatchGetItemOutcome &outcome = dynamoClient.BatchGetItem( request); if (outcome.IsSuccess()) { for (const auto &responsesMapEntry: outcome.GetResult().GetResponses()) { Aws::String tableName = responsesMapEntry.first; const Aws::Vector<Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue>> &tableResults = responsesMapEntry.second; std::cout << "Retrieved " << tableResults.size() << " responses for table '" << tableName << "'.\n" << std::endl; if (tableName == "Forum") { std::cout << "Name | Category | Message | Views" << std::endl; for (const Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue> &item: tableResults) { std::cout << item.at("Name").GetS() << " | "; std::cout << item.at("Category").GetS() << " | "; std::cout << (item.count("Message") == 0 ? "" : item.at( "Messages").GetN()) << " | "; std::cout << (item.count("Views") == 0 ? "" : item.at( "Views").GetN()) << std::endl; } } else { std::cout << "Title | Price | Color" << std::endl; for (const Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue> &item: tableResults) { std::cout << item.at("Title").GetS() << " | "; std::cout << (item.count("Price") == 0 ? "" : item.at( "Price").GetN()); if (item.count("Color")) { std::cout << " | "; for (const std::shared_ptr<Aws::DynamoDB::Model::AttributeValue> &listItem: item.at( "Color").GetL()) std::cout << listItem->GetS() << " "; } std::cout << std::endl; } } std::cout << std::endl; } // If necessary, repeat request for remaining items. requestItems = outcome.GetResult().GetUnprocessedKeys(); } else { std::cerr << "Batch get item failed: " << outcome.GetError().GetMessage() << std::endl; result = false; break; } } while (!requestItems.empty()); return result; }
  • For API details, see BatchGetItem in Amazon SDK for C++ API Reference.

The following code example shows how to use BatchWriteItem.

SDK for C++
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

//! Batch write items from a JSON file. /*! \sa batchWriteItem() \param jsonFilePath: JSON file path. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ /* * The input for this routine is a JSON file that you can download from the following URL: * https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/SampleData.html. * * The JSON data uses the BatchWriteItem API request syntax. The JSON strings are * converted to AttributeValue objects. These AttributeValue objects will then generate * JSON strings when constructing the BatchWriteItem request, essentially outputting * their input. * * This is perhaps an artificial example, but it demonstrates the APIs. */ bool AwsDoc::DynamoDB::batchWriteItem(const Aws::String &jsonFilePath, const Aws::Client::ClientConfiguration &clientConfiguration) { std::ifstream fileStream(jsonFilePath); if (!fileStream) { std::cerr << "Error: could not open file '" << jsonFilePath << "'." << std::endl; } std::stringstream stringStream; stringStream << fileStream.rdbuf(); Aws::Utils::Json::JsonValue jsonValue(stringStream); Aws::DynamoDB::Model::BatchWriteItemRequest batchWriteItemRequest; Aws::Map<Aws::String, Aws::Utils::Json::JsonView> level1Map = jsonValue.View().GetAllObjects(); for (const auto &level1Entry: level1Map) { const Aws::Utils::Json::JsonView &entriesView = level1Entry.second; const Aws::String &tableName = level1Entry.first; // The JSON entries at this level are as follows: // key - table name // value - list of request objects if (!entriesView.IsListType()) { std::cerr << "Error: JSON file entry '" << tableName << "' is not a list." << std::endl; continue; } Aws::Utils::Array<Aws::Utils::Json::JsonView> entries = entriesView.AsArray(); Aws::Vector<Aws::DynamoDB::Model::WriteRequest> writeRequests; if (AwsDoc::DynamoDB::addWriteRequests(tableName, entries, writeRequests)) { batchWriteItemRequest.AddRequestItems(tableName, writeRequests); } } Aws::DynamoDB::DynamoDBClient dynamoClient(clientConfiguration); Aws::DynamoDB::Model::BatchWriteItemOutcome outcome = dynamoClient.BatchWriteItem( batchWriteItemRequest); if (outcome.IsSuccess()) { std::cout << "DynamoDB::BatchWriteItem was successful." << std::endl; } else { std::cerr << "Error with DynamoDB::BatchWriteItem. " << outcome.GetError().GetMessage() << std::endl; } return true; } //! Convert requests in JSON format to a vector of WriteRequest objects. /*! \sa addWriteRequests() \param tableName: Name of the table for the write operations. \param requestsJson: Request data in JSON format. \param writeRequests: Vector to receive the WriteRequest objects. \return bool: Function succeeded. */ bool AwsDoc::DynamoDB::addWriteRequests(const Aws::String &tableName, const Aws::Utils::Array<Aws::Utils::Json::JsonView> &requestsJson, Aws::Vector<Aws::DynamoDB::Model::WriteRequest> &writeRequests) { for (size_t i = 0; i < requestsJson.GetLength(); ++i) { const Aws::Utils::Json::JsonView &requestsEntry = requestsJson[i]; if (!requestsEntry.IsObject()) { std::cerr << "Error: incorrect requestsEntry type " << requestsEntry.WriteReadable() << std::endl; return false; } Aws::Map<Aws::String, Aws::Utils::Json::JsonView> requestsMap = requestsEntry.GetAllObjects(); for (const auto &request: requestsMap) { const Aws::String &requestType = request.first; const Aws::Utils::Json::JsonView &requestJsonView = request.second; if (requestType == "PutRequest") { if (!requestJsonView.ValueExists("Item")) { std::cerr << "Error: item key missing for requests " << requestJsonView.WriteReadable() << std::endl; return false; } Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue> attributes; if (!getAttributeObjectsMap(requestJsonView.GetObject("Item"), attributes)) { std::cerr << "Error getting attributes " << requestJsonView.WriteReadable() << std::endl; return false; } Aws::DynamoDB::Model::PutRequest putRequest; putRequest.SetItem(attributes); writeRequests.push_back( Aws::DynamoDB::Model::WriteRequest().WithPutRequest( putRequest)); } else { std::cerr << "Error: unimplemented request type '" << requestType << "'." << std::endl; } } } return true; } //! Generate a map of AttributeValue objects from JSON records. /*! \sa getAttributeObjectsMap() \param jsonView: JSONView of attribute records. \param writeRequests: Map to receive the AttributeValue objects. \return bool: Function succeeded. */ bool AwsDoc::DynamoDB::getAttributeObjectsMap(const Aws::Utils::Json::JsonView &jsonView, Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue> &attributes) { Aws::Map<Aws::String, Aws::Utils::Json::JsonView> objectsMap = jsonView.GetAllObjects(); for (const auto &entry: objectsMap) { const Aws::String &attributeKey = entry.first; const Aws::Utils::Json::JsonView &attributeJsonView = entry.second; if (!attributeJsonView.IsObject()) { std::cerr << "Error: attribute not an object " << attributeJsonView.WriteReadable() << std::endl; return false; } attributes.emplace(attributeKey, Aws::DynamoDB::Model::AttributeValue(attributeJsonView)); } return true; }
  • For API details, see BatchWriteItem in Amazon SDK for C++ API Reference.

The following code example shows how to use CreateTable.

SDK for C++
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

//! Create an Amazon DynamoDB table. /*! \sa createTable() \param tableName: Name for the DynamoDB table. \param primaryKey: Primary key for the DynamoDB table. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::DynamoDB::createTable(const Aws::String &tableName, const Aws::String &primaryKey, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::DynamoDB::DynamoDBClient dynamoClient(clientConfiguration); std::cout << "Creating table " << tableName << " with a simple primary key: \"" << primaryKey << "\"." << std::endl; Aws::DynamoDB::Model::CreateTableRequest request; Aws::DynamoDB::Model::AttributeDefinition hashKey; hashKey.SetAttributeName(primaryKey); hashKey.SetAttributeType(Aws::DynamoDB::Model::ScalarAttributeType::S); request.AddAttributeDefinitions(hashKey); Aws::DynamoDB::Model::KeySchemaElement keySchemaElement; keySchemaElement.WithAttributeName(primaryKey).WithKeyType( Aws::DynamoDB::Model::KeyType::HASH); request.AddKeySchema(keySchemaElement); Aws::DynamoDB::Model::ProvisionedThroughput throughput; throughput.WithReadCapacityUnits(5).WithWriteCapacityUnits(5); request.SetProvisionedThroughput(throughput); request.SetTableName(tableName); const Aws::DynamoDB::Model::CreateTableOutcome &outcome = dynamoClient.CreateTable( request); if (outcome.IsSuccess()) { std::cout << "Table \"" << outcome.GetResult().GetTableDescription().GetTableName() << " created!" << std::endl; } else { std::cerr << "Failed to create table: " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
  • For API details, see CreateTable in Amazon SDK for C++ API Reference.

The following code example shows how to use DeleteItem.

SDK for C++
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

//! Delete an item from an Amazon DynamoDB table. /*! \sa deleteItem() \param tableName: The table name. \param partitionKey: The partition key. \param partitionValue: The value for the partition key. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::DynamoDB::deleteItem(const Aws::String &tableName, const Aws::String &partitionKey, const Aws::String &partitionValue, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::DynamoDB::DynamoDBClient dynamoClient(clientConfiguration); Aws::DynamoDB::Model::DeleteItemRequest request; request.AddKey(partitionKey, Aws::DynamoDB::Model::AttributeValue().SetS(partitionValue)); request.SetTableName(tableName); const Aws::DynamoDB::Model::DeleteItemOutcome &outcome = dynamoClient.DeleteItem( request); if (outcome.IsSuccess()) { std::cout << "Item \"" << partitionValue << "\" deleted!" << std::endl; } else { std::cerr << "Failed to delete item: " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
  • For API details, see DeleteItem in Amazon SDK for C++ API Reference.

The following code example shows how to use DeleteTable.

SDK for C++
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

//! Delete an Amazon DynamoDB table. /*! \sa deleteTable() \param tableName: The DynamoDB table name. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::DynamoDB::deleteTable(const Aws::String &tableName, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::DynamoDB::DynamoDBClient dynamoClient(clientConfiguration); Aws::DynamoDB::Model::DeleteTableRequest request; request.SetTableName(tableName); const Aws::DynamoDB::Model::DeleteTableOutcome &result = dynamoClient.DeleteTable( request); if (result.IsSuccess()) { std::cout << "Your table \"" << result.GetResult().GetTableDescription().GetTableName() << " was deleted.\n"; } else { std::cerr << "Failed to delete table: " << result.GetError().GetMessage() << std::endl; } return result.IsSuccess(); }
  • For API details, see DeleteTable in Amazon SDK for C++ API Reference.

The following code example shows how to use DescribeTable.

SDK for C++
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

//! Describe an Amazon DynamoDB table. /*! \sa describeTable() \param tableName: The DynamoDB table name. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::DynamoDB::describeTable(const Aws::String &tableName, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::DynamoDB::DynamoDBClient dynamoClient(clientConfiguration); Aws::DynamoDB::Model::DescribeTableRequest request; request.SetTableName(tableName); const Aws::DynamoDB::Model::DescribeTableOutcome &outcome = dynamoClient.DescribeTable( request); if (outcome.IsSuccess()) { const Aws::DynamoDB::Model::TableDescription &td = outcome.GetResult().GetTable(); std::cout << "Table name : " << td.GetTableName() << std::endl; std::cout << "Table ARN : " << td.GetTableArn() << std::endl; std::cout << "Status : " << Aws::DynamoDB::Model::TableStatusMapper::GetNameForTableStatus( td.GetTableStatus()) << std::endl; std::cout << "Item count : " << td.GetItemCount() << std::endl; std::cout << "Size (bytes): " << td.GetTableSizeBytes() << std::endl; const Aws::DynamoDB::Model::ProvisionedThroughputDescription &ptd = td.GetProvisionedThroughput(); std::cout << "Throughput" << std::endl; std::cout << " Read Capacity : " << ptd.GetReadCapacityUnits() << std::endl; std::cout << " Write Capacity: " << ptd.GetWriteCapacityUnits() << std::endl; const Aws::Vector<Aws::DynamoDB::Model::AttributeDefinition> &ad = td.GetAttributeDefinitions(); std::cout << "Attributes" << std::endl; for (const auto &a: ad) std::cout << " " << a.GetAttributeName() << " (" << Aws::DynamoDB::Model::ScalarAttributeTypeMapper::GetNameForScalarAttributeType( a.GetAttributeType()) << ")" << std::endl; } else { std::cerr << "Failed to describe table: " << outcome.GetError().GetMessage(); } return outcome.IsSuccess(); }
  • For API details, see DescribeTable in Amazon SDK for C++ API Reference.

The following code example shows how to use ExecuteStatement.

SDK for C++
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

Use an INSERT statement to add an item.

Aws::DynamoDB::DynamoDBClient dynamoClient(clientConfiguration); // 2. Add a new movie using an "Insert" statement. (ExecuteStatement) Aws::String title; float rating; int year; Aws::String plot; { title = askQuestion( "Enter the title of a movie you want to add to the table: "); year = askQuestionForInt("What year was it released? "); rating = askQuestionForFloatRange("On a scale of 1 - 10, how do you rate it? ", 1, 10); plot = askQuestion("Summarize the plot for me: "); Aws::DynamoDB::Model::ExecuteStatementRequest request; std::stringstream sqlStream; sqlStream << "INSERT INTO \"" << MOVIE_TABLE_NAME << "\" VALUE {'" << TITLE_KEY << "': ?, '" << YEAR_KEY << "': ?, '" << INFO_KEY << "': ?}"; request.SetStatement(sqlStream.str()); // Create the parameter attributes. Aws::Vector<Aws::DynamoDB::Model::AttributeValue> attributes; attributes.push_back(Aws::DynamoDB::Model::AttributeValue().SetS(title)); attributes.push_back(Aws::DynamoDB::Model::AttributeValue().SetN(year)); Aws::DynamoDB::Model::AttributeValue infoMapAttribute; std::shared_ptr<Aws::DynamoDB::Model::AttributeValue> ratingAttribute = Aws::MakeShared<Aws::DynamoDB::Model::AttributeValue>( ALLOCATION_TAG.c_str()); ratingAttribute->SetN(rating); infoMapAttribute.AddMEntry(RATING_KEY, ratingAttribute); std::shared_ptr<Aws::DynamoDB::Model::AttributeValue> plotAttribute = Aws::MakeShared<Aws::DynamoDB::Model::AttributeValue>( ALLOCATION_TAG.c_str()); plotAttribute->SetS(plot); infoMapAttribute.AddMEntry(PLOT_KEY, plotAttribute); attributes.push_back(infoMapAttribute); request.SetParameters(attributes); Aws::DynamoDB::Model::ExecuteStatementOutcome outcome = dynamoClient.ExecuteStatement( request); if (!outcome.IsSuccess()) { std::cerr << "Failed to add a movie: " << outcome.GetError().GetMessage() << std::endl; return false; } }

Use a SELECT statement to get an item.

// 3. Get the data for the movie using a "Select" statement. (ExecuteStatement) { Aws::DynamoDB::Model::ExecuteStatementRequest request; std::stringstream sqlStream; sqlStream << "SELECT * FROM \"" << MOVIE_TABLE_NAME << "\" WHERE " << TITLE_KEY << "=? and " << YEAR_KEY << "=?"; request.SetStatement(sqlStream.str()); Aws::Vector<Aws::DynamoDB::Model::AttributeValue> attributes; attributes.push_back(Aws::DynamoDB::Model::AttributeValue().SetS(title)); attributes.push_back(Aws::DynamoDB::Model::AttributeValue().SetN(year)); request.SetParameters(attributes); Aws::DynamoDB::Model::ExecuteStatementOutcome outcome = dynamoClient.ExecuteStatement( request); if (!outcome.IsSuccess()) { std::cerr << "Failed to retrieve movie information: " << outcome.GetError().GetMessage() << std::endl; return false; } else { // Print the retrieved movie information. const Aws::DynamoDB::Model::ExecuteStatementResult &result = outcome.GetResult(); const Aws::Vector<Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue>> &items = result.GetItems(); if (items.size() == 1) { printMovieInfo(items[0]); } else { std::cerr << "Error: " << items.size() << " movies were retrieved. " << " There should be only one movie." << std::endl; } } }

Use an UPDATE statement to update an item.

// 4. Update the data for the movie using an "Update" statement. (ExecuteStatement) { rating = askQuestionForFloatRange( Aws::String("\nLet's update your movie.\nYou rated it ") + std::to_string(rating) + ", what new rating would you give it? ", 1, 10); Aws::DynamoDB::Model::ExecuteStatementRequest request; std::stringstream sqlStream; sqlStream << "UPDATE \"" << MOVIE_TABLE_NAME << "\" SET " << INFO_KEY << "." << RATING_KEY << "=? WHERE " << TITLE_KEY << "=? AND " << YEAR_KEY << "=?"; request.SetStatement(sqlStream.str()); Aws::Vector<Aws::DynamoDB::Model::AttributeValue> attributes; attributes.push_back(Aws::DynamoDB::Model::AttributeValue().SetN(rating)); attributes.push_back(Aws::DynamoDB::Model::AttributeValue().SetS(title)); attributes.push_back(Aws::DynamoDB::Model::AttributeValue().SetN(year)); request.SetParameters(attributes); Aws::DynamoDB::Model::ExecuteStatementOutcome outcome = dynamoClient.ExecuteStatement( request); if (!outcome.IsSuccess()) { std::cerr << "Failed to update a movie: " << outcome.GetError().GetMessage(); return false; } }

Use a DELETE statement to delete an item.

// 6. Delete the movie using a "Delete" statement. (ExecuteStatement) { Aws::DynamoDB::Model::ExecuteStatementRequest request; std::stringstream sqlStream; sqlStream << "DELETE FROM \"" << MOVIE_TABLE_NAME << "\" WHERE " << TITLE_KEY << "=? and " << YEAR_KEY << "=?"; request.SetStatement(sqlStream.str()); Aws::Vector<Aws::DynamoDB::Model::AttributeValue> attributes; attributes.push_back(Aws::DynamoDB::Model::AttributeValue().SetS(title)); attributes.push_back(Aws::DynamoDB::Model::AttributeValue().SetN(year)); request.SetParameters(attributes); Aws::DynamoDB::Model::ExecuteStatementOutcome outcome = dynamoClient.ExecuteStatement( request); if (!outcome.IsSuccess()) { std::cerr << "Failed to delete the movie: " << outcome.GetError().GetMessage() << std::endl; return false; } }

The following code example shows how to use GetItem.

SDK for C++
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

//! Get an item from an Amazon DynamoDB table. /*! \sa getItem() \param tableName: The table name. \param partitionKey: The partition key. \param partitionValue: The value for the partition key. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::DynamoDB::getItem(const Aws::String &tableName, const Aws::String &partitionKey, const Aws::String &partitionValue, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::DynamoDB::DynamoDBClient dynamoClient(clientConfiguration); Aws::DynamoDB::Model::GetItemRequest request; // Set up the request. request.SetTableName(tableName); request.AddKey(partitionKey, Aws::DynamoDB::Model::AttributeValue().SetS(partitionValue)); // Retrieve the item's fields and values. const Aws::DynamoDB::Model::GetItemOutcome &outcome = dynamoClient.GetItem(request); if (outcome.IsSuccess()) { // Reference the retrieved fields/values. const Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue> &item = outcome.GetResult().GetItem(); if (!item.empty()) { // Output each retrieved field and its value. for (const auto &i: item) std::cout << "Values: " << i.first << ": " << i.second.GetS() << std::endl; } else { std::cout << "No item found with the key " << partitionKey << std::endl; } } else { std::cerr << "Failed to get item: " << outcome.GetError().GetMessage(); } return outcome.IsSuccess(); }
  • For API details, see GetItem in Amazon SDK for C++ API Reference.

The following code example shows how to use ListTables.

SDK for C++
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

//! List the Amazon DynamoDB tables for the current AWS account. /*! \sa listTables() \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::DynamoDB::listTables( const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::DynamoDB::DynamoDBClient dynamoClient(clientConfiguration); Aws::DynamoDB::Model::ListTablesRequest listTablesRequest; listTablesRequest.SetLimit(50); do { const Aws::DynamoDB::Model::ListTablesOutcome &outcome = dynamoClient.ListTables( listTablesRequest); if (!outcome.IsSuccess()) { std::cout << "Error: " << outcome.GetError().GetMessage() << std::endl; return false; } for (const auto &tableName: outcome.GetResult().GetTableNames()) std::cout << tableName << std::endl; listTablesRequest.SetExclusiveStartTableName( outcome.GetResult().GetLastEvaluatedTableName()); } while (!listTablesRequest.GetExclusiveStartTableName().empty()); return true; }
  • For API details, see ListTables in Amazon SDK for C++ API Reference.

The following code example shows how to use PutItem.

SDK for C++
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

//! Put an item in an Amazon DynamoDB table. /*! \sa putItem() \param tableName: The table name. \param artistKey: The artist key. This is the partition key for the table. \param artistValue: The artist value. \param albumTitleKey: The album title key. \param albumTitleValue: The album title value. \param awardsKey: The awards key. \param awardsValue: The awards value. \param songTitleKey: The song title key. \param songTitleValue: The song title value. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::DynamoDB::putItem(const Aws::String &tableName, const Aws::String &artistKey, const Aws::String &artistValue, const Aws::String &albumTitleKey, const Aws::String &albumTitleValue, const Aws::String &awardsKey, const Aws::String &awardsValue, const Aws::String &songTitleKey, const Aws::String &songTitleValue, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::DynamoDB::DynamoDBClient dynamoClient(clientConfiguration); Aws::DynamoDB::Model::PutItemRequest putItemRequest; putItemRequest.SetTableName(tableName); putItemRequest.AddItem(artistKey, Aws::DynamoDB::Model::AttributeValue().SetS( artistValue)); // This is the hash key. putItemRequest.AddItem(albumTitleKey, Aws::DynamoDB::Model::AttributeValue().SetS( albumTitleValue)); putItemRequest.AddItem(awardsKey, Aws::DynamoDB::Model::AttributeValue().SetS(awardsValue)); putItemRequest.AddItem(songTitleKey, Aws::DynamoDB::Model::AttributeValue().SetS(songTitleValue)); const Aws::DynamoDB::Model::PutItemOutcome outcome = dynamoClient.PutItem( putItemRequest); if (outcome.IsSuccess()) { std::cout << "Successfully added Item!" << std::endl; } else { std::cerr << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
  • For API details, see PutItem in Amazon SDK for C++ API Reference.

The following code example shows how to use Query.

SDK for C++
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

//! Perform a query on an Amazon DynamoDB Table and retrieve items. /*! \sa queryItem() \param tableName: The table name. \param partitionKey: The partition key. \param partitionValue: The value for the partition key. \param projectionExpression: The projections expression, which is ignored if empty. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ /* * The partition key attribute is searched with the specified value. By default, all fields and values * contained in the item are returned. If an optional projection expression is * specified on the command line, only the specified fields and values are * returned. */ bool AwsDoc::DynamoDB::queryItems(const Aws::String &tableName, const Aws::String &partitionKey, const Aws::String &partitionValue, const Aws::String &projectionExpression, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::DynamoDB::DynamoDBClient dynamoClient(clientConfiguration); Aws::DynamoDB::Model::QueryRequest request; request.SetTableName(tableName); if (!projectionExpression.empty()) { request.SetProjectionExpression(projectionExpression); } // Set query key condition expression. request.SetKeyConditionExpression(partitionKey + "= :valueToMatch"); // Set Expression AttributeValues. Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue> attributeValues; attributeValues.emplace(":valueToMatch", partitionValue); request.SetExpressionAttributeValues(attributeValues); bool result = true; // "exclusiveStartKey" is used for pagination. Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue> exclusiveStartKey; do { if (!exclusiveStartKey.empty()) { request.SetExclusiveStartKey(exclusiveStartKey); exclusiveStartKey.clear(); } // Perform Query operation. const Aws::DynamoDB::Model::QueryOutcome &outcome = dynamoClient.Query(request); if (outcome.IsSuccess()) { // Reference the retrieved items. const Aws::Vector<Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue>> &items = outcome.GetResult().GetItems(); if (!items.empty()) { std::cout << "Number of items retrieved from Query: " << items.size() << std::endl; // Iterate each item and print. for (const auto &item: items) { std::cout << "******************************************************" << std::endl; // Output each retrieved field and its value. for (const auto &i: item) std::cout << i.first << ": " << i.second.GetS() << std::endl; } } else { std::cout << "No item found in table: " << tableName << std::endl; } exclusiveStartKey = outcome.GetResult().GetLastEvaluatedKey(); } else { std::cerr << "Failed to Query items: " << outcome.GetError().GetMessage(); result = false; break; } } while (!exclusiveStartKey.empty()); return result; }
  • For API details, see Query in Amazon SDK for C++ API Reference.

The following code example shows how to use Scan.

SDK for C++
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

//! Scan an Amazon DynamoDB table. /*! \sa scanTable() \param tableName: Name for the DynamoDB table. \param projectionExpression: An optional projection expression, ignored if empty. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::DynamoDB::scanTable(const Aws::String &tableName, const Aws::String &projectionExpression, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::DynamoDB::DynamoDBClient dynamoClient(clientConfiguration); Aws::DynamoDB::Model::ScanRequest request; request.SetTableName(tableName); if (!projectionExpression.empty()) request.SetProjectionExpression(projectionExpression); Aws::Vector<Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue>> all_items; Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue> last_evaluated_key; // Used for pagination; do { if (!last_evaluated_key.empty()) { request.SetExclusiveStartKey(last_evaluated_key); } const Aws::DynamoDB::Model::ScanOutcome &outcome = dynamoClient.Scan(request); if (outcome.IsSuccess()) { // Reference the retrieved items. const Aws::Vector<Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue>> &items = outcome.GetResult().GetItems(); all_items.insert(all_items.end(), items.begin(), items.end()); last_evaluated_key = outcome.GetResult().GetLastEvaluatedKey(); } else { std::cerr << "Failed to Scan items: " << outcome.GetError().GetMessage() << std::endl; return false; } } while (!last_evaluated_key.empty()); if (!all_items.empty()) { std::cout << "Number of items retrieved from scan: " << all_items.size() << std::endl; // Iterate each item and print. for (const Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue> &itemMap: all_items) { std::cout << "******************************************************" << std::endl; // Output each retrieved field and its value. for (const auto &itemEntry: itemMap) std::cout << itemEntry.first << ": " << itemEntry.second.GetS() << std::endl; } } else { std::cout << "No items found in table: " << tableName << std::endl; } return true; }
  • For API details, see Scan in Amazon SDK for C++ API Reference.

The following code example shows how to use UpdateItem.

SDK for C++
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

//! Update an Amazon DynamoDB table item. /*! \sa updateItem() \param tableName: The table name. \param partitionKey: The partition key. \param partitionValue: The value for the partition key. \param attributeKey: The key for the attribute to be updated. \param attributeValue: The value for the attribute to be updated. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ /* * The example code only sets/updates an attribute value. It processes * the attribute value as a string, even if the value could be interpreted * as a number. Also, the example code does not remove an existing attribute * from the key value. */ bool AwsDoc::DynamoDB::updateItem(const Aws::String &tableName, const Aws::String &partitionKey, const Aws::String &partitionValue, const Aws::String &attributeKey, const Aws::String &attributeValue, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::DynamoDB::DynamoDBClient dynamoClient(clientConfiguration); // *** Define UpdateItem request arguments. // Define TableName argument. Aws::DynamoDB::Model::UpdateItemRequest request; request.SetTableName(tableName); // Define KeyName argument. Aws::DynamoDB::Model::AttributeValue attribValue; attribValue.SetS(partitionValue); request.AddKey(partitionKey, attribValue); // Construct the SET update expression argument. Aws::String update_expression("SET #a = :valueA"); request.SetUpdateExpression(update_expression); // Construct attribute name argument. Aws::Map<Aws::String, Aws::String> expressionAttributeNames; expressionAttributeNames["#a"] = attributeKey; request.SetExpressionAttributeNames(expressionAttributeNames); // Construct attribute value argument. Aws::DynamoDB::Model::AttributeValue attributeUpdatedValue; attributeUpdatedValue.SetS(attributeValue); Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue> expressionAttributeValues; expressionAttributeValues[":valueA"] = attributeUpdatedValue; request.SetExpressionAttributeValues(expressionAttributeValues); // Update the item. const Aws::DynamoDB::Model::UpdateItemOutcome &outcome = dynamoClient.UpdateItem( request); if (outcome.IsSuccess()) { std::cout << "Item was updated" << std::endl; } else { std::cerr << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
  • For API details, see UpdateItem in Amazon SDK for C++ API Reference.

Scenarios

The following code example shows how to:

  • 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.

SDK for C++
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

{ Aws::Client::ClientConfiguration clientConfig; // 1. Create a table with partition: year (N) and sort: title (S). (CreateTable) if (AwsDoc::DynamoDB::createMoviesDynamoDBTable(clientConfig)) { AwsDoc::DynamoDB::dynamodbGettingStartedScenario(clientConfig); // 9. Delete the table. (DeleteTable) AwsDoc::DynamoDB::deleteMoviesDynamoDBTable(clientConfig); } } //! Scenario to modify and query a DynamoDB table. /*! \sa dynamodbGettingStartedScenario() \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::DynamoDB::dynamodbGettingStartedScenario( const Aws::Client::ClientConfiguration &clientConfiguration) { std::cout << std::setfill('*') << std::setw(ASTERISK_FILL_WIDTH) << " " << std::endl; std::cout << "Welcome to the Amazon DynamoDB getting started demo." << std::endl; std::cout << std::setfill('*') << std::setw(ASTERISK_FILL_WIDTH) << " " << std::endl; Aws::DynamoDB::DynamoDBClient dynamoClient(clientConfiguration); // 2. Add a new movie. Aws::String title; float rating; int year; Aws::String plot; { title = askQuestion( "Enter the title of a movie you want to add to the table: "); year = askQuestionForInt("What year was it released? "); rating = askQuestionForFloatRange("On a scale of 1 - 10, how do you rate it? ", 1, 10); plot = askQuestion("Summarize the plot for me: "); Aws::DynamoDB::Model::PutItemRequest putItemRequest; putItemRequest.SetTableName(MOVIE_TABLE_NAME); putItemRequest.AddItem(YEAR_KEY, Aws::DynamoDB::Model::AttributeValue().SetN(year)); putItemRequest.AddItem(TITLE_KEY, Aws::DynamoDB::Model::AttributeValue().SetS(title)); // Create attribute for the info map. Aws::DynamoDB::Model::AttributeValue infoMapAttribute; std::shared_ptr<Aws::DynamoDB::Model::AttributeValue> ratingAttribute = Aws::MakeShared<Aws::DynamoDB::Model::AttributeValue>( ALLOCATION_TAG.c_str()); ratingAttribute->SetN(rating); infoMapAttribute.AddMEntry(RATING_KEY, ratingAttribute); std::shared_ptr<Aws::DynamoDB::Model::AttributeValue> plotAttribute = Aws::MakeShared<Aws::DynamoDB::Model::AttributeValue>( ALLOCATION_TAG.c_str()); plotAttribute->SetS(plot); infoMapAttribute.AddMEntry(PLOT_KEY, plotAttribute); putItemRequest.AddItem(INFO_KEY, infoMapAttribute); Aws::DynamoDB::Model::PutItemOutcome outcome = dynamoClient.PutItem( putItemRequest); if (!outcome.IsSuccess()) { std::cerr << "Failed to add an item: " << outcome.GetError().GetMessage() << std::endl; return false; } } std::cout << "\nAdded '" << title << "' to '" << MOVIE_TABLE_NAME << "'." << std::endl; // 3. Update the rating and plot of the movie by using an update expression. { rating = askQuestionForFloatRange( Aws::String("\nLet's update your movie.\nYou rated it ") + std::to_string(rating) + ", what new rating would you give it? ", 1, 10); plot = askQuestion(Aws::String("You summarized the plot as '") + plot + "'.\nWhat would you say now? "); Aws::DynamoDB::Model::UpdateItemRequest request; request.SetTableName(MOVIE_TABLE_NAME); request.AddKey(TITLE_KEY, Aws::DynamoDB::Model::AttributeValue().SetS(title)); request.AddKey(YEAR_KEY, Aws::DynamoDB::Model::AttributeValue().SetN(year)); std::stringstream expressionStream; expressionStream << "set " << INFO_KEY << "." << RATING_KEY << " =:r, " << INFO_KEY << "." << PLOT_KEY << " =:p"; request.SetUpdateExpression(expressionStream.str()); request.SetExpressionAttributeValues({ {":r", Aws::DynamoDB::Model::AttributeValue().SetN( rating)}, {":p", Aws::DynamoDB::Model::AttributeValue().SetS( plot)} }); request.SetReturnValues(Aws::DynamoDB::Model::ReturnValue::UPDATED_NEW); const Aws::DynamoDB::Model::UpdateItemOutcome &result = dynamoClient.UpdateItem( request); if (!result.IsSuccess()) { std::cerr << "Error updating movie " + result.GetError().GetMessage() << std::endl; return false; } } std::cout << "\nUpdated '" << title << "' with new attributes:" << std::endl; // 4. Put 250 movies in the table from moviedata.json. { std::cout << "Adding movies from a json file to the database." << std::endl; const size_t MAX_SIZE_FOR_BATCH_WRITE = 25; const size_t MOVIES_TO_WRITE = 10 * MAX_SIZE_FOR_BATCH_WRITE; Aws::String jsonString = getMovieJSON(); if (!jsonString.empty()) { Aws::Utils::Json::JsonValue json(jsonString); Aws::Utils::Array<Aws::Utils::Json::JsonView> movieJsons = json.View().AsArray(); Aws::Vector<Aws::DynamoDB::Model::WriteRequest> writeRequests; // To add movies with a cross-section of years, use an appropriate increment // value for iterating through the database. size_t increment = movieJsons.GetLength() / MOVIES_TO_WRITE; for (size_t i = 0; i < movieJsons.GetLength(); i += increment) { writeRequests.push_back(Aws::DynamoDB::Model::WriteRequest()); Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue> putItems = movieJsonViewToAttributeMap( movieJsons[i]); Aws::DynamoDB::Model::PutRequest putRequest; putRequest.SetItem(putItems); writeRequests.back().SetPutRequest(putRequest); if (writeRequests.size() == MAX_SIZE_FOR_BATCH_WRITE) { Aws::DynamoDB::Model::BatchWriteItemRequest request; request.AddRequestItems(MOVIE_TABLE_NAME, writeRequests); const Aws::DynamoDB::Model::BatchWriteItemOutcome &outcome = dynamoClient.BatchWriteItem( request); if (!outcome.IsSuccess()) { std::cerr << "Unable to batch write movie data: " << outcome.GetError().GetMessage() << std::endl; writeRequests.clear(); break; } else { std::cout << "Added batch of " << writeRequests.size() << " movies to the database." << std::endl; } writeRequests.clear(); } } } } std::cout << std::setfill('*') << std::setw(ASTERISK_FILL_WIDTH) << " " << std::endl; // 5. Get a movie by Key (partition + sort). { Aws::String titleToGet("King Kong"); Aws::String answer = askQuestion(Aws::String( "Let's move on...Would you like to get info about '" + titleToGet + "'? (y/n) ")); if (answer == "y") { Aws::DynamoDB::Model::GetItemRequest request; request.SetTableName(MOVIE_TABLE_NAME); request.AddKey(TITLE_KEY, Aws::DynamoDB::Model::AttributeValue().SetS(titleToGet)); request.AddKey(YEAR_KEY, Aws::DynamoDB::Model::AttributeValue().SetN(1933)); const Aws::DynamoDB::Model::GetItemOutcome &result = dynamoClient.GetItem( request); if (!result.IsSuccess()) { std::cerr << "Error " << result.GetError().GetMessage(); } else { const Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue> &item = result.GetResult().GetItem(); if (!item.empty()) { std::cout << "\nHere's what I found:" << std::endl; printMovieInfo(item); } else { std::cout << "\nThe movie was not found in the database." << std::endl; } } } } // 6. Use Query with a key condition expression to return all movies // released in a given year. Aws::String doAgain = "n"; do { Aws::DynamoDB::Model::QueryRequest req; req.SetTableName(MOVIE_TABLE_NAME); // "year" is a DynamoDB reserved keyword and must be replaced with an // expression attribute name. req.SetKeyConditionExpression("#dynobase_year = :valueToMatch"); req.SetExpressionAttributeNames({{"#dynobase_year", YEAR_KEY}}); int yearToMatch = askQuestionForIntRange( "\nLet's get a list of movies released in" " a given year. Enter a year between 1972 and 2018 ", 1972, 2018); Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue> attributeValues; attributeValues.emplace(":valueToMatch", Aws::DynamoDB::Model::AttributeValue().SetN( yearToMatch)); req.SetExpressionAttributeValues(attributeValues); const Aws::DynamoDB::Model::QueryOutcome &result = dynamoClient.Query(req); if (result.IsSuccess()) { const Aws::Vector<Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue>> &items = result.GetResult().GetItems(); if (!items.empty()) { std::cout << "\nThere were " << items.size() << " movies in the database from " << yearToMatch << "." << std::endl; for (const auto &item: items) { printMovieInfo(item); } doAgain = "n"; } else { std::cout << "\nNo movies from " << yearToMatch << " were found in the database" << std::endl; doAgain = askQuestion(Aws::String("Try another year? (y/n) ")); } } else { std::cerr << "Failed to Query items: " << result.GetError().GetMessage() << std::endl; } } while (doAgain == "y"); // 7. Use Scan to return movies released within a range of years. // Show how to paginate data using ExclusiveStartKey. (Scan + FilterExpression) { int startYear = askQuestionForIntRange("\nNow let's scan a range of years " "for movies in the database. Enter a start year: ", 1972, 2018); int endYear = askQuestionForIntRange("\nEnter an end year: ", startYear, 2018); Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue> exclusiveStartKey; do { Aws::DynamoDB::Model::ScanRequest scanRequest; scanRequest.SetTableName(MOVIE_TABLE_NAME); scanRequest.SetFilterExpression( "#dynobase_year >= :startYear AND #dynobase_year <= :endYear"); scanRequest.SetExpressionAttributeNames({{"#dynobase_year", YEAR_KEY}}); Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue> attributeValues; attributeValues.emplace(":startYear", Aws::DynamoDB::Model::AttributeValue().SetN( startYear)); attributeValues.emplace(":endYear", Aws::DynamoDB::Model::AttributeValue().SetN( endYear)); scanRequest.SetExpressionAttributeValues(attributeValues); if (!exclusiveStartKey.empty()) { scanRequest.SetExclusiveStartKey(exclusiveStartKey); } const Aws::DynamoDB::Model::ScanOutcome &result = dynamoClient.Scan( scanRequest); if (result.IsSuccess()) { const Aws::Vector<Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue>> &items = result.GetResult().GetItems(); if (!items.empty()) { std::stringstream stringStream; stringStream << "\nFound " << items.size() << " movies in one scan." << " How many would you like to see? "; size_t count = askQuestionForInt(stringStream.str()); for (size_t i = 0; i < count && i < items.size(); ++i) { printMovieInfo(items[i]); } } else { std::cout << "\nNo movies in the database between " << startYear << " and " << endYear << "." << std::endl; } exclusiveStartKey = result.GetResult().GetLastEvaluatedKey(); if (!exclusiveStartKey.empty()) { std::cout << "Not all movies were retrieved. Scanning for more." << std::endl; } else { std::cout << "All movies were retrieved with this scan." << std::endl; } } else { std::cerr << "Failed to Scan movies: " << result.GetError().GetMessage() << std::endl; } } while (!exclusiveStartKey.empty()); } // 8. Delete a movie. (DeleteItem) { std::stringstream stringStream; stringStream << "\nWould you like to delete the movie " << title << " from the database? (y/n) "; Aws::String answer = askQuestion(stringStream.str()); if (answer == "y") { Aws::DynamoDB::Model::DeleteItemRequest request; request.AddKey(YEAR_KEY, Aws::DynamoDB::Model::AttributeValue().SetN(year)); request.AddKey(TITLE_KEY, Aws::DynamoDB::Model::AttributeValue().SetS(title)); request.SetTableName(MOVIE_TABLE_NAME); const Aws::DynamoDB::Model::DeleteItemOutcome &result = dynamoClient.DeleteItem( request); if (result.IsSuccess()) { std::cout << "\nRemoved \"" << title << "\" from the database." << std::endl; } else { std::cerr << "Failed to delete the movie: " << result.GetError().GetMessage() << std::endl; } } } return true; } //! Routine to convert a JsonView object to an attribute map. /*! \sa movieJsonViewToAttributeMap() \param jsonView: Json view object. \return map: Map that can be used in a DynamoDB request. */ Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue> AwsDoc::DynamoDB::movieJsonViewToAttributeMap( const Aws::Utils::Json::JsonView &jsonView) { Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue> result; if (jsonView.KeyExists(YEAR_KEY)) { result[YEAR_KEY].SetN(jsonView.GetInteger(YEAR_KEY)); } if (jsonView.KeyExists(TITLE_KEY)) { result[TITLE_KEY].SetS(jsonView.GetString(TITLE_KEY)); } if (jsonView.KeyExists(INFO_KEY)) { Aws::Map<Aws::String, const std::shared_ptr<Aws::DynamoDB::Model::AttributeValue>> infoMap; Aws::Utils::Json::JsonView infoView = jsonView.GetObject(INFO_KEY); if (infoView.KeyExists(RATING_KEY)) { std::shared_ptr<Aws::DynamoDB::Model::AttributeValue> attributeValue = std::make_shared<Aws::DynamoDB::Model::AttributeValue>(); attributeValue->SetN(infoView.GetDouble(RATING_KEY)); infoMap.emplace(std::make_pair(RATING_KEY, attributeValue)); } if (infoView.KeyExists(PLOT_KEY)) { std::shared_ptr<Aws::DynamoDB::Model::AttributeValue> attributeValue = std::make_shared<Aws::DynamoDB::Model::AttributeValue>(); attributeValue->SetS(infoView.GetString(PLOT_KEY)); infoMap.emplace(std::make_pair(PLOT_KEY, attributeValue)); } result[INFO_KEY].SetM(infoMap); } return result; } //! Create a DynamoDB table to be used in sample code scenarios. /*! \sa createMoviesDynamoDBTable() \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::DynamoDB::createMoviesDynamoDBTable( const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::DynamoDB::DynamoDBClient dynamoClient(clientConfiguration); bool movieTableAlreadyExisted = false; { Aws::DynamoDB::Model::CreateTableRequest request; Aws::DynamoDB::Model::AttributeDefinition yearAttributeDefinition; yearAttributeDefinition.SetAttributeName(YEAR_KEY); yearAttributeDefinition.SetAttributeType( Aws::DynamoDB::Model::ScalarAttributeType::N); request.AddAttributeDefinitions(yearAttributeDefinition); Aws::DynamoDB::Model::AttributeDefinition titleAttributeDefinition; yearAttributeDefinition.SetAttributeName(TITLE_KEY); yearAttributeDefinition.SetAttributeType( Aws::DynamoDB::Model::ScalarAttributeType::S); request.AddAttributeDefinitions(yearAttributeDefinition); Aws::DynamoDB::Model::KeySchemaElement yearKeySchema; yearKeySchema.WithAttributeName(YEAR_KEY).WithKeyType( Aws::DynamoDB::Model::KeyType::HASH); request.AddKeySchema(yearKeySchema); Aws::DynamoDB::Model::KeySchemaElement titleKeySchema; yearKeySchema.WithAttributeName(TITLE_KEY).WithKeyType( Aws::DynamoDB::Model::KeyType::RANGE); request.AddKeySchema(yearKeySchema); Aws::DynamoDB::Model::ProvisionedThroughput throughput; throughput.WithReadCapacityUnits( PROVISIONED_THROUGHPUT_UNITS).WithWriteCapacityUnits( PROVISIONED_THROUGHPUT_UNITS); request.SetProvisionedThroughput(throughput); request.SetTableName(MOVIE_TABLE_NAME); std::cout << "Creating table '" << MOVIE_TABLE_NAME << "'..." << std::endl; const Aws::DynamoDB::Model::CreateTableOutcome &result = dynamoClient.CreateTable( request); if (!result.IsSuccess()) { if (result.GetError().GetErrorType() == Aws::DynamoDB::DynamoDBErrors::RESOURCE_IN_USE) { std::cout << "Table already exists." << std::endl; movieTableAlreadyExisted = true; } else { std::cerr << "Failed to create table: " << result.GetError().GetMessage(); return false; } } } // Wait for table to become active. if (!movieTableAlreadyExisted) { std::cout << "Waiting for table '" << MOVIE_TABLE_NAME << "' to become active...." << std::endl; if (!AwsDoc::DynamoDB::waitTableActive(MOVIE_TABLE_NAME, clientConfiguration)) { return false; } std::cout << "Table '" << MOVIE_TABLE_NAME << "' created and active." << std::endl; } return true; } //! Delete the DynamoDB table used for sample code scenarios. /*! \sa deleteMoviesDynamoDBTable() \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::DynamoDB::deleteMoviesDynamoDBTable( const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::DynamoDB::DynamoDBClient dynamoClient(clientConfiguration); Aws::DynamoDB::Model::DeleteTableRequest request; request.SetTableName(MOVIE_TABLE_NAME); const Aws::DynamoDB::Model::DeleteTableOutcome &result = dynamoClient.DeleteTable( request); if (result.IsSuccess()) { std::cout << "Your table \"" << result.GetResult().GetTableDescription().GetTableName() << " was deleted.\n"; } else { std::cerr << "Failed to delete table: " << result.GetError().GetMessage() << std::endl; } return result.IsSuccess(); } //! Query a newly created DynamoDB table until it is active. /*! \sa waitTableActive() \param waitTableActive: The DynamoDB table's name. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::DynamoDB::waitTableActive(const Aws::String &tableName, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::DynamoDB::DynamoDBClient dynamoClient(clientConfiguration); // Repeatedly call DescribeTable until table is ACTIVE. const int MAX_QUERIES = 20; Aws::DynamoDB::Model::DescribeTableRequest request; request.SetTableName(tableName); int count = 0; while (count < MAX_QUERIES) { const Aws::DynamoDB::Model::DescribeTableOutcome &result = dynamoClient.DescribeTable( request); if (result.IsSuccess()) { Aws::DynamoDB::Model::TableStatus status = result.GetResult().GetTable().GetTableStatus(); if (Aws::DynamoDB::Model::TableStatus::ACTIVE != status) { std::this_thread::sleep_for(std::chrono::seconds(1)); } else { return true; } } else { std::cerr << "Error DynamoDB::waitTableActive " << result.GetError().GetMessage() << std::endl; return false; } count++; } return false; }

The following code example shows how to:

  • 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.

SDK for C++
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

Aws::Client::ClientConfiguration clientConfig; // 1. Create a table. (CreateTable) if (AwsDoc::DynamoDB::createMoviesDynamoDBTable(clientConfig)) { AwsDoc::DynamoDB::partiqlBatchExecuteScenario(clientConfig); // 7. Delete the table. (DeleteTable) AwsDoc::DynamoDB::deleteMoviesDynamoDBTable(clientConfig); } //! Scenario to modify and query a DynamoDB table using PartiQL batch statements. /*! \sa partiqlBatchExecuteScenario() \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::DynamoDB::partiqlBatchExecuteScenario( const Aws::Client::ClientConfiguration &clientConfiguration) { // 2. Add multiple movies using "Insert" statements. (BatchExecuteStatement) Aws::DynamoDB::DynamoDBClient dynamoClient(clientConfiguration); std::vector<Aws::String> titles; std::vector<float> ratings; std::vector<int> years; std::vector<Aws::String> plots; Aws::String doAgain = "n"; do { Aws::String aTitle = askQuestion( "Enter the title of a movie you want to add to the table: "); titles.push_back(aTitle); int aYear = askQuestionForInt("What year was it released? "); years.push_back(aYear); float aRating = askQuestionForFloatRange( "On a scale of 1 - 10, how do you rate it? ", 1, 10); ratings.push_back(aRating); Aws::String aPlot = askQuestion("Summarize the plot for me: "); plots.push_back(aPlot); doAgain = askQuestion(Aws::String("Would you like to add more movies? (y/n) ")); } while (doAgain == "y"); std::cout << "Adding " << titles.size() << (titles.size() == 1 ? " movie " : " movies ") << "to the table using a batch \"INSERT\" statement." << std::endl; { Aws::Vector<Aws::DynamoDB::Model::BatchStatementRequest> statements( titles.size()); std::stringstream sqlStream; sqlStream << "INSERT INTO \"" << MOVIE_TABLE_NAME << "\" VALUE {'" << TITLE_KEY << "': ?, '" << YEAR_KEY << "': ?, '" << INFO_KEY << "': ?}"; std::string sql(sqlStream.str()); for (size_t i = 0; i < statements.size(); ++i) { statements[i].SetStatement(sql); Aws::Vector<Aws::DynamoDB::Model::AttributeValue> attributes; attributes.push_back( Aws::DynamoDB::Model::AttributeValue().SetS(titles[i])); attributes.push_back(Aws::DynamoDB::Model::AttributeValue().SetN(years[i])); // Create attribute for the info map. Aws::DynamoDB::Model::AttributeValue infoMapAttribute; std::shared_ptr<Aws::DynamoDB::Model::AttributeValue> ratingAttribute = Aws::MakeShared<Aws::DynamoDB::Model::AttributeValue>( ALLOCATION_TAG.c_str()); ratingAttribute->SetN(ratings[i]); infoMapAttribute.AddMEntry(RATING_KEY, ratingAttribute); std::shared_ptr<Aws::DynamoDB::Model::AttributeValue> plotAttribute = Aws::MakeShared<Aws::DynamoDB::Model::AttributeValue>( ALLOCATION_TAG.c_str()); plotAttribute->SetS(plots[i]); infoMapAttribute.AddMEntry(PLOT_KEY, plotAttribute); attributes.push_back(infoMapAttribute); statements[i].SetParameters(attributes); } Aws::DynamoDB::Model::BatchExecuteStatementRequest request; request.SetStatements(statements); Aws::DynamoDB::Model::BatchExecuteStatementOutcome outcome = dynamoClient.BatchExecuteStatement( request); if (!outcome.IsSuccess()) { std::cerr << "Failed to add the movies: " << outcome.GetError().GetMessage() << std::endl; return false; } } std::cout << "Retrieving the movie data with a batch \"SELECT\" statement." << std::endl; // 3. Get the data for multiple movies using "Select" statements. (BatchExecuteStatement) { Aws::Vector<Aws::DynamoDB::Model::BatchStatementRequest> statements( titles.size()); std::stringstream sqlStream; sqlStream << "SELECT * FROM \"" << MOVIE_TABLE_NAME << "\" WHERE " << TITLE_KEY << "=? and " << YEAR_KEY << "=?"; std::string sql(sqlStream.str()); for (size_t i = 0; i < statements.size(); ++i) { statements[i].SetStatement(sql); Aws::Vector<Aws::DynamoDB::Model::AttributeValue> attributes; attributes.push_back( Aws::DynamoDB::Model::AttributeValue().SetS(titles[i])); attributes.push_back(Aws::DynamoDB::Model::AttributeValue().SetN(years[i])); statements[i].SetParameters(attributes); } Aws::DynamoDB::Model::BatchExecuteStatementRequest request; request.SetStatements(statements); Aws::DynamoDB::Model::BatchExecuteStatementOutcome outcome = dynamoClient.BatchExecuteStatement( request); if (outcome.IsSuccess()) { const Aws::DynamoDB::Model::BatchExecuteStatementResult &result = outcome.GetResult(); const Aws::Vector<Aws::DynamoDB::Model::BatchStatementResponse> &responses = result.GetResponses(); for (const Aws::DynamoDB::Model::BatchStatementResponse &response: responses) { const Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue> &item = response.GetItem(); printMovieInfo(item); } } else { std::cerr << "Failed to retrieve the movie information: " << outcome.GetError().GetMessage() << std::endl; return false; } } // 4. Update the data for multiple movies using "Update" statements. (BatchExecuteStatement) for (size_t i = 0; i < titles.size(); ++i) { ratings[i] = askQuestionForFloatRange( Aws::String("\nLet's update your the movie, \"") + titles[i] + ".\nYou rated it " + std::to_string(ratings[i]) + ", what new rating would you give it? ", 1, 10); } std::cout << "Updating the movie with a batch \"UPDATE\" statement." << std::endl; { Aws::Vector<Aws::DynamoDB::Model::BatchStatementRequest> statements( titles.size()); std::stringstream sqlStream; sqlStream << "UPDATE \"" << MOVIE_TABLE_NAME << "\" SET " << INFO_KEY << "." << RATING_KEY << "=? WHERE " << TITLE_KEY << "=? AND " << YEAR_KEY << "=?"; std::string sql(sqlStream.str()); for (size_t i = 0; i < statements.size(); ++i) { statements[i].SetStatement(sql); Aws::Vector<Aws::DynamoDB::Model::AttributeValue> attributes; attributes.push_back( Aws::DynamoDB::Model::AttributeValue().SetN(ratings[i])); attributes.push_back( Aws::DynamoDB::Model::AttributeValue().SetS(titles[i])); attributes.push_back(Aws::DynamoDB::Model::AttributeValue().SetN(years[i])); statements[i].SetParameters(attributes); } Aws::DynamoDB::Model::BatchExecuteStatementRequest request; request.SetStatements(statements); Aws::DynamoDB::Model::BatchExecuteStatementOutcome outcome = dynamoClient.BatchExecuteStatement( request); if (!outcome.IsSuccess()) { std::cerr << "Failed to update movie information: " << outcome.GetError().GetMessage() << std::endl; return false; } } std::cout << "Retrieving the updated movie data with a batch \"SELECT\" statement." << std::endl; // 5. Get the updated data for multiple movies using "Select" statements. (BatchExecuteStatement) { Aws::Vector<Aws::DynamoDB::Model::BatchStatementRequest> statements( titles.size()); std::stringstream sqlStream; sqlStream << "SELECT * FROM \"" << MOVIE_TABLE_NAME << "\" WHERE " << TITLE_KEY << "=? and " << YEAR_KEY << "=?"; std::string sql(sqlStream.str()); for (size_t i = 0; i < statements.size(); ++i) { statements[i].SetStatement(sql); Aws::Vector<Aws::DynamoDB::Model::AttributeValue> attributes; attributes.push_back( Aws::DynamoDB::Model::AttributeValue().SetS(titles[i])); attributes.push_back(Aws::DynamoDB::Model::AttributeValue().SetN(years[i])); statements[i].SetParameters(attributes); } Aws::DynamoDB::Model::BatchExecuteStatementRequest request; request.SetStatements(statements); Aws::DynamoDB::Model::BatchExecuteStatementOutcome outcome = dynamoClient.BatchExecuteStatement( request); if (outcome.IsSuccess()) { const Aws::DynamoDB::Model::BatchExecuteStatementResult &result = outcome.GetResult(); const Aws::Vector<Aws::DynamoDB::Model::BatchStatementResponse> &responses = result.GetResponses(); for (const Aws::DynamoDB::Model::BatchStatementResponse &response: responses) { const Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue> &item = response.GetItem(); printMovieInfo(item); } } else { std::cerr << "Failed to retrieve the movies information: " << outcome.GetError().GetMessage() << std::endl; return false; } } std::cout << "Deleting the movie data with a batch \"DELETE\" statement." << std::endl; // 6. Delete multiple movies using "Delete" statements. (BatchExecuteStatement) { Aws::Vector<Aws::DynamoDB::Model::BatchStatementRequest> statements( titles.size()); std::stringstream sqlStream; sqlStream << "DELETE FROM \"" << MOVIE_TABLE_NAME << "\" WHERE " << TITLE_KEY << "=? and " << YEAR_KEY << "=?"; std::string sql(sqlStream.str()); for (size_t i = 0; i < statements.size(); ++i) { statements[i].SetStatement(sql); Aws::Vector<Aws::DynamoDB::Model::AttributeValue> attributes; attributes.push_back( Aws::DynamoDB::Model::AttributeValue().SetS(titles[i])); attributes.push_back(Aws::DynamoDB::Model::AttributeValue().SetN(years[i])); statements[i].SetParameters(attributes); } Aws::DynamoDB::Model::BatchExecuteStatementRequest request; request.SetStatements(statements); Aws::DynamoDB::Model::BatchExecuteStatementOutcome outcome = dynamoClient.BatchExecuteStatement( request); if (!outcome.IsSuccess()) { std::cerr << "Failed to delete the movies: " << outcome.GetError().GetMessage() << std::endl; return false; } } return true; } //! Create a DynamoDB table to be used in sample code scenarios. /*! \sa createMoviesDynamoDBTable() \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::DynamoDB::createMoviesDynamoDBTable( const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::DynamoDB::DynamoDBClient dynamoClient(clientConfiguration); bool movieTableAlreadyExisted = false; { Aws::DynamoDB::Model::CreateTableRequest request; Aws::DynamoDB::Model::AttributeDefinition yearAttributeDefinition; yearAttributeDefinition.SetAttributeName(YEAR_KEY); yearAttributeDefinition.SetAttributeType( Aws::DynamoDB::Model::ScalarAttributeType::N); request.AddAttributeDefinitions(yearAttributeDefinition); Aws::DynamoDB::Model::AttributeDefinition titleAttributeDefinition; yearAttributeDefinition.SetAttributeName(TITLE_KEY); yearAttributeDefinition.SetAttributeType( Aws::DynamoDB::Model::ScalarAttributeType::S); request.AddAttributeDefinitions(yearAttributeDefinition); Aws::DynamoDB::Model::KeySchemaElement yearKeySchema; yearKeySchema.WithAttributeName(YEAR_KEY).WithKeyType( Aws::DynamoDB::Model::KeyType::HASH); request.AddKeySchema(yearKeySchema); Aws::DynamoDB::Model::KeySchemaElement titleKeySchema; yearKeySchema.WithAttributeName(TITLE_KEY).WithKeyType( Aws::DynamoDB::Model::KeyType::RANGE); request.AddKeySchema(yearKeySchema); Aws::DynamoDB::Model::ProvisionedThroughput throughput; throughput.WithReadCapacityUnits( PROVISIONED_THROUGHPUT_UNITS).WithWriteCapacityUnits( PROVISIONED_THROUGHPUT_UNITS); request.SetProvisionedThroughput(throughput); request.SetTableName(MOVIE_TABLE_NAME); std::cout << "Creating table '" << MOVIE_TABLE_NAME << "'..." << std::endl; const Aws::DynamoDB::Model::CreateTableOutcome &result = dynamoClient.CreateTable( request); if (!result.IsSuccess()) { if (result.GetError().GetErrorType() == Aws::DynamoDB::DynamoDBErrors::RESOURCE_IN_USE) { std::cout << "Table already exists." << std::endl; movieTableAlreadyExisted = true; } else { std::cerr << "Failed to create table: " << result.GetError().GetMessage(); return false; } } } // Wait for table to become active. if (!movieTableAlreadyExisted) { std::cout << "Waiting for table '" << MOVIE_TABLE_NAME << "' to become active...." << std::endl; if (!AwsDoc::DynamoDB::waitTableActive(MOVIE_TABLE_NAME, clientConfiguration)) { return false; } std::cout << "Table '" << MOVIE_TABLE_NAME << "' created and active." << std::endl; } return true; } //! Delete the DynamoDB table used for sample code scenarios. /*! \sa deleteMoviesDynamoDBTable() \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::DynamoDB::deleteMoviesDynamoDBTable( const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::DynamoDB::DynamoDBClient dynamoClient(clientConfiguration); Aws::DynamoDB::Model::DeleteTableRequest request; request.SetTableName(MOVIE_TABLE_NAME); const Aws::DynamoDB::Model::DeleteTableOutcome &result = dynamoClient.DeleteTable( request); if (result.IsSuccess()) { std::cout << "Your table \"" << result.GetResult().GetTableDescription().GetTableName() << " was deleted.\n"; } else { std::cerr << "Failed to delete table: " << result.GetError().GetMessage() << std::endl; } return result.IsSuccess(); } //! Query a newly created DynamoDB table until it is active. /*! \sa waitTableActive() \param waitTableActive: The DynamoDB table's name. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::DynamoDB::waitTableActive(const Aws::String &tableName, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::DynamoDB::DynamoDBClient dynamoClient(clientConfiguration); // Repeatedly call DescribeTable until table is ACTIVE. const int MAX_QUERIES = 20; Aws::DynamoDB::Model::DescribeTableRequest request; request.SetTableName(tableName); int count = 0; while (count < MAX_QUERIES) { const Aws::DynamoDB::Model::DescribeTableOutcome &result = dynamoClient.DescribeTable( request); if (result.IsSuccess()) { Aws::DynamoDB::Model::TableStatus status = result.GetResult().GetTable().GetTableStatus(); if (Aws::DynamoDB::Model::TableStatus::ACTIVE != status) { std::this_thread::sleep_for(std::chrono::seconds(1)); } else { return true; } } else { std::cerr << "Error DynamoDB::waitTableActive " << result.GetError().GetMessage() << std::endl; return false; } count++; } return false; }

The following code example shows how to:

  • 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.

SDK for C++
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

// 1. Create a table. (CreateTable) if (AwsDoc::DynamoDB::createMoviesDynamoDBTable(clientConfig)) { AwsDoc::DynamoDB::partiqlExecuteScenario(clientConfig); // 7. Delete the table. (DeleteTable) AwsDoc::DynamoDB::deleteMoviesDynamoDBTable(clientConfig); } //! Scenario to modify and query a DynamoDB table using single PartiQL statements. /*! \sa partiqlExecuteScenario() \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::DynamoDB::partiqlExecuteScenario( const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::DynamoDB::DynamoDBClient dynamoClient(clientConfiguration); // 2. Add a new movie using an "Insert" statement. (ExecuteStatement) Aws::String title; float rating; int year; Aws::String plot; { title = askQuestion( "Enter the title of a movie you want to add to the table: "); year = askQuestionForInt("What year was it released? "); rating = askQuestionForFloatRange("On a scale of 1 - 10, how do you rate it? ", 1, 10); plot = askQuestion("Summarize the plot for me: "); Aws::DynamoDB::Model::ExecuteStatementRequest request; std::stringstream sqlStream; sqlStream << "INSERT INTO \"" << MOVIE_TABLE_NAME << "\" VALUE {'" << TITLE_KEY << "': ?, '" << YEAR_KEY << "': ?, '" << INFO_KEY << "': ?}"; request.SetStatement(sqlStream.str()); // Create the parameter attributes. Aws::Vector<Aws::DynamoDB::Model::AttributeValue> attributes; attributes.push_back(Aws::DynamoDB::Model::AttributeValue().SetS(title)); attributes.push_back(Aws::DynamoDB::Model::AttributeValue().SetN(year)); Aws::DynamoDB::Model::AttributeValue infoMapAttribute; std::shared_ptr<Aws::DynamoDB::Model::AttributeValue> ratingAttribute = Aws::MakeShared<Aws::DynamoDB::Model::AttributeValue>( ALLOCATION_TAG.c_str()); ratingAttribute->SetN(rating); infoMapAttribute.AddMEntry(RATING_KEY, ratingAttribute); std::shared_ptr<Aws::DynamoDB::Model::AttributeValue> plotAttribute = Aws::MakeShared<Aws::DynamoDB::Model::AttributeValue>( ALLOCATION_TAG.c_str()); plotAttribute->SetS(plot); infoMapAttribute.AddMEntry(PLOT_KEY, plotAttribute); attributes.push_back(infoMapAttribute); request.SetParameters(attributes); Aws::DynamoDB::Model::ExecuteStatementOutcome outcome = dynamoClient.ExecuteStatement( request); if (!outcome.IsSuccess()) { std::cerr << "Failed to add a movie: " << outcome.GetError().GetMessage() << std::endl; return false; } } std::cout << "\nAdded '" << title << "' to '" << MOVIE_TABLE_NAME << "'." << std::endl; // 3. Get the data for the movie using a "Select" statement. (ExecuteStatement) { Aws::DynamoDB::Model::ExecuteStatementRequest request; std::stringstream sqlStream; sqlStream << "SELECT * FROM \"" << MOVIE_TABLE_NAME << "\" WHERE " << TITLE_KEY << "=? and " << YEAR_KEY << "=?"; request.SetStatement(sqlStream.str()); Aws::Vector<Aws::DynamoDB::Model::AttributeValue> attributes; attributes.push_back(Aws::DynamoDB::Model::AttributeValue().SetS(title)); attributes.push_back(Aws::DynamoDB::Model::AttributeValue().SetN(year)); request.SetParameters(attributes); Aws::DynamoDB::Model::ExecuteStatementOutcome outcome = dynamoClient.ExecuteStatement( request); if (!outcome.IsSuccess()) { std::cerr << "Failed to retrieve movie information: " << outcome.GetError().GetMessage() << std::endl; return false; } else { // Print the retrieved movie information. const Aws::DynamoDB::Model::ExecuteStatementResult &result = outcome.GetResult(); const Aws::Vector<Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue>> &items = result.GetItems(); if (items.size() == 1) { printMovieInfo(items[0]); } else { std::cerr << "Error: " << items.size() << " movies were retrieved. " << " There should be only one movie." << std::endl; } } } // 4. Update the data for the movie using an "Update" statement. (ExecuteStatement) { rating = askQuestionForFloatRange( Aws::String("\nLet's update your movie.\nYou rated it ") + std::to_string(rating) + ", what new rating would you give it? ", 1, 10); Aws::DynamoDB::Model::ExecuteStatementRequest request; std::stringstream sqlStream; sqlStream << "UPDATE \"" << MOVIE_TABLE_NAME << "\" SET " << INFO_KEY << "." << RATING_KEY << "=? WHERE " << TITLE_KEY << "=? AND " << YEAR_KEY << "=?"; request.SetStatement(sqlStream.str()); Aws::Vector<Aws::DynamoDB::Model::AttributeValue> attributes; attributes.push_back(Aws::DynamoDB::Model::AttributeValue().SetN(rating)); attributes.push_back(Aws::DynamoDB::Model::AttributeValue().SetS(title)); attributes.push_back(Aws::DynamoDB::Model::AttributeValue().SetN(year)); request.SetParameters(attributes); Aws::DynamoDB::Model::ExecuteStatementOutcome outcome = dynamoClient.ExecuteStatement( request); if (!outcome.IsSuccess()) { std::cerr << "Failed to update a movie: " << outcome.GetError().GetMessage(); return false; } } std::cout << "\nUpdated '" << title << "' with new attributes:" << std::endl; // 5. Get the updated data for the movie using a "Select" statement. (ExecuteStatement) { Aws::DynamoDB::Model::ExecuteStatementRequest request; std::stringstream sqlStream; sqlStream << "SELECT * FROM \"" << MOVIE_TABLE_NAME << "\" WHERE " << TITLE_KEY << "=? and " << YEAR_KEY << "=?"; request.SetStatement(sqlStream.str()); Aws::Vector<Aws::DynamoDB::Model::AttributeValue> attributes; attributes.push_back(Aws::DynamoDB::Model::AttributeValue().SetS(title)); attributes.push_back(Aws::DynamoDB::Model::AttributeValue().SetN(year)); request.SetParameters(attributes); Aws::DynamoDB::Model::ExecuteStatementOutcome outcome = dynamoClient.ExecuteStatement( request); if (!outcome.IsSuccess()) { std::cerr << "Failed to retrieve the movie information: " << outcome.GetError().GetMessage() << std::endl; return false; } else { const Aws::DynamoDB::Model::ExecuteStatementResult &result = outcome.GetResult(); const Aws::Vector<Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue>> &items = result.GetItems(); if (items.size() == 1) { printMovieInfo(items[0]); } else { std::cerr << "Error: " << items.size() << " movies were retrieved. " << " There should be only one movie." << std::endl; } } } std::cout << "Deleting the movie" << std::endl; // 6. Delete the movie using a "Delete" statement. (ExecuteStatement) { Aws::DynamoDB::Model::ExecuteStatementRequest request; std::stringstream sqlStream; sqlStream << "DELETE FROM \"" << MOVIE_TABLE_NAME << "\" WHERE " << TITLE_KEY << "=? and " << YEAR_KEY << "=?"; request.SetStatement(sqlStream.str()); Aws::Vector<Aws::DynamoDB::Model::AttributeValue> attributes; attributes.push_back(Aws::DynamoDB::Model::AttributeValue().SetS(title)); attributes.push_back(Aws::DynamoDB::Model::AttributeValue().SetN(year)); request.SetParameters(attributes); Aws::DynamoDB::Model::ExecuteStatementOutcome outcome = dynamoClient.ExecuteStatement( request); if (!outcome.IsSuccess()) { std::cerr << "Failed to delete the movie: " << outcome.GetError().GetMessage() << std::endl; return false; } } std::cout << "Movie successfully deleted." << std::endl; return true; } //! Create a DynamoDB table to be used in sample code scenarios. /*! \sa createMoviesDynamoDBTable() \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::DynamoDB::createMoviesDynamoDBTable( const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::DynamoDB::DynamoDBClient dynamoClient(clientConfiguration); bool movieTableAlreadyExisted = false; { Aws::DynamoDB::Model::CreateTableRequest request; Aws::DynamoDB::Model::AttributeDefinition yearAttributeDefinition; yearAttributeDefinition.SetAttributeName(YEAR_KEY); yearAttributeDefinition.SetAttributeType( Aws::DynamoDB::Model::ScalarAttributeType::N); request.AddAttributeDefinitions(yearAttributeDefinition); Aws::DynamoDB::Model::AttributeDefinition titleAttributeDefinition; yearAttributeDefinition.SetAttributeName(TITLE_KEY); yearAttributeDefinition.SetAttributeType( Aws::DynamoDB::Model::ScalarAttributeType::S); request.AddAttributeDefinitions(yearAttributeDefinition); Aws::DynamoDB::Model::KeySchemaElement yearKeySchema; yearKeySchema.WithAttributeName(YEAR_KEY).WithKeyType( Aws::DynamoDB::Model::KeyType::HASH); request.AddKeySchema(yearKeySchema); Aws::DynamoDB::Model::KeySchemaElement titleKeySchema; yearKeySchema.WithAttributeName(TITLE_KEY).WithKeyType( Aws::DynamoDB::Model::KeyType::RANGE); request.AddKeySchema(yearKeySchema); Aws::DynamoDB::Model::ProvisionedThroughput throughput; throughput.WithReadCapacityUnits( PROVISIONED_THROUGHPUT_UNITS).WithWriteCapacityUnits( PROVISIONED_THROUGHPUT_UNITS); request.SetProvisionedThroughput(throughput); request.SetTableName(MOVIE_TABLE_NAME); std::cout << "Creating table '" << MOVIE_TABLE_NAME << "'..." << std::endl; const Aws::DynamoDB::Model::CreateTableOutcome &result = dynamoClient.CreateTable( request); if (!result.IsSuccess()) { if (result.GetError().GetErrorType() == Aws::DynamoDB::DynamoDBErrors::RESOURCE_IN_USE) { std::cout << "Table already exists." << std::endl; movieTableAlreadyExisted = true; } else { std::cerr << "Failed to create table: " << result.GetError().GetMessage(); return false; } } } // Wait for table to become active. if (!movieTableAlreadyExisted) { std::cout << "Waiting for table '" << MOVIE_TABLE_NAME << "' to become active...." << std::endl; if (!AwsDoc::DynamoDB::waitTableActive(MOVIE_TABLE_NAME, clientConfiguration)) { return false; } std::cout << "Table '" << MOVIE_TABLE_NAME << "' created and active." << std::endl; } return true; } //! Delete the DynamoDB table used for sample code scenarios. /*! \sa deleteMoviesDynamoDBTable() \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::DynamoDB::deleteMoviesDynamoDBTable( const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::DynamoDB::DynamoDBClient dynamoClient(clientConfiguration); Aws::DynamoDB::Model::DeleteTableRequest request; request.SetTableName(MOVIE_TABLE_NAME); const Aws::DynamoDB::Model::DeleteTableOutcome &result = dynamoClient.DeleteTable( request); if (result.IsSuccess()) { std::cout << "Your table \"" << result.GetResult().GetTableDescription().GetTableName() << " was deleted.\n"; } else { std::cerr << "Failed to delete table: " << result.GetError().GetMessage() << std::endl; } return result.IsSuccess(); } //! Query a newly created DynamoDB table until it is active. /*! \sa waitTableActive() \param waitTableActive: The DynamoDB table's name. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::DynamoDB::waitTableActive(const Aws::String &tableName, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::DynamoDB::DynamoDBClient dynamoClient(clientConfiguration); // Repeatedly call DescribeTable until table is ACTIVE. const int MAX_QUERIES = 20; Aws::DynamoDB::Model::DescribeTableRequest request; request.SetTableName(tableName); int count = 0; while (count < MAX_QUERIES) { const Aws::DynamoDB::Model::DescribeTableOutcome &result = dynamoClient.DescribeTable( request); if (result.IsSuccess()) { Aws::DynamoDB::Model::TableStatus status = result.GetResult().GetTable().GetTableStatus(); if (Aws::DynamoDB::Model::TableStatus::ACTIVE != status) { std::this_thread::sleep_for(std::chrono::seconds(1)); } else { return true; } } else { std::cerr << "Error DynamoDB::waitTableActive " << result.GetError().GetMessage() << std::endl; return false; } count++; } return false; }