AWS SDK for C++
Developer Guide

Working with Items in DynamoDB

In DynamoDB, an item is a collection of attributes, each of which has a name and a value. An attribute value can be a scalar, set, or document type. For more information, see Naming Rules and Data Types in the Amazon DynamoDB Developer Guide.

Retrieve an Item from a Table

Call the DynamoDB client GetItem method. Pass it a GetItemRequest object with the table name and primary key value of the item you want. It returns a GetItemResult object.

You can use the returned GetItemResult object's GetItem() method to retrieve an Aws::Map of key Aws::String and value AttributeValue pairs associated with the item.

Includes

#include <aws/core/Aws.h> #include <aws/core/utils/Outcome.h> #include <aws/dynamodb/DynamoDBClient.h> #include <aws/dynamodb/model/AttributeDefinition.h> #include <aws/dynamodb/model/GetItemRequest.h> #include <iostream>

Code

Aws::Client::ClientConfiguration clientConfig; Aws::DynamoDB::DynamoDBClient dynamoClient(clientConfig); Aws::DynamoDB::Model::GetItemRequest req; // Set up the request req.SetTableName(table); Aws::DynamoDB::Model::AttributeValue hashKey; hashKey.SetS(name); req.AddKey("Name", hashKey); if (!projection.empty()) req.SetProjectionExpression(projection); // Retrieve the item's fields and values const Aws::DynamoDB::Model::GetItemOutcome& result = dynamoClient.GetItem(req); if (result.IsSuccess()) { // Reference the retrieved fields/values const Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue>& item = result.GetResult().GetItem(); if (item.size() > 0) { // 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 with the key " << name << std::endl; } } else { std::cout << "Failed to get item: " << result.GetError().GetMessage(); }

See the complete example on GitHub.

Add an Item to a Table

Create key Aws::String and value AttributeValue pairs that represent each item. These must include values for the table's primary key fields. If the item identified by the primary key already exists, its fields are updated by the request. Add them to the PutItemRequest using the AddItem method.

Includes

#include <aws/core/Aws.h> #include <aws/core/utils/Outcome.h> #include <aws/dynamodb/DynamoDBClient.h> #include <aws/dynamodb/model/AttributeDefinition.h> #include <aws/dynamodb/model/PutItemRequest.h> #include <aws/dynamodb/model/PutItemResult.h> #include <iostream>

Code

Aws::Client::ClientConfiguration clientConfig; Aws::DynamoDB::DynamoDBClient dynamoClient(clientConfig); Aws::DynamoDB::Model::PutItemRequest pir; pir.SetTableName(table); Aws::DynamoDB::Model::AttributeValue av; av.SetS(name); pir.AddItem("Name", av); for (int x = 3; x < argc; x++) { const Aws::String arg(argv[x]); const Aws::Vector<Aws::String>& flds = Aws::Utils::StringUtils::Split(arg, '='); if (flds.size() == 2) { Aws::DynamoDB::Model::AttributeValue val; val.SetS(flds[1]); pir.AddItem(flds[0], val); } else { std::cout << "Invalid argument: " << arg << std::endl << USAGE; return 1; } } const Aws::DynamoDB::Model::PutItemOutcome result = dynamoClient.PutItem(pir); if (!result.IsSuccess()) { std::cout << result.GetError().GetMessage() << std::endl; return 1; } std::cout << "Done!" << std::endl;

See the complete example on GitHub.

Update an Existing Item in a Table

You can update an attribute for an item that already exists in a table by using the DynamoDBClient's UpdateItem method, providing a table name, primary key value, and fields to update and their corresponding value.

Imports

#include <aws/core/Aws.h> #include <aws/core/utils/Outcome.h> #include <aws/dynamodb/DynamoDBClient.h> #include <aws/dynamodb/model/UpdateItemRequest.h> #include <aws/dynamodb/model/UpdateItemResult.h> #include <iostream>

Code

Aws::Client::ClientConfiguration clientConfig; Aws::DynamoDB::DynamoDBClient dynamoClient(clientConfig); // *** 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(keyValue); request.AddKey("Name", attribValue); // Construct the SET update expression argument Aws::String update_expression("SET #a = :valueA"); request.SetUpdateExpression(update_expression); // Parse the attribute name and value. Syntax: "name=value" auto parsed = Aws::Utils::StringUtils::Split(attributeNameAndValue, '='); // parsed[0] == attribute name, parsed[1] == attribute value if (parsed.size() != 2) { std::cout << "Invalid argument syntax: " << attributeNameAndValue << USAGE; return 1; } // Construct attribute name argument // Note: Setting the ExpressionAttributeNames argument is required only // when the name is a reserved word, such as "default". Otherwise, the // name can be included in the update_expression, as in // "SET MyAttributeName = :valueA" Aws::Map<Aws::String, Aws::String> expressionAttributeNames; expressionAttributeNames["#a"] = parsed[0]; request.SetExpressionAttributeNames(expressionAttributeNames); // Construct attribute value argument Aws::DynamoDB::Model::AttributeValue attributeUpdatedValue; attributeUpdatedValue.SetS(parsed[1]); Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue> expressionAttributeValues; expressionAttributeValues[":valueA"] = attributeUpdatedValue; request.SetExpressionAttributeValues(expressionAttributeValues); // Update the item const Aws::DynamoDB::Model::UpdateItemOutcome& result = dynamoClient.UpdateItem(request); if (!result.IsSuccess()) { std::cout << result.GetError().GetMessage() << std::endl; return 1; } std::cout << "Item was updated" << std::endl;

See the complete example.

More Info