Menu
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 (Get) 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

Copy
#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

Copy
Aws::Client::ClientConfiguration clientConfig; Aws::DynamoDB::DynamoDBClient dynamoClient(clientConfig); Aws::DynamoDB::Model::GetItemRequest req; if (!projection.empty()) req.SetProjectionExpression(projection); Aws::DynamoDB::Model::AttributeValue haskKey; haskKey.SetS(name); req.AddKey("Name", haskKey); req.SetTableName(table); const Aws::DynamoDB::Model::GetItemOutcome& result = dynamoClient.GetItem(req); if (result.IsSuccess()) { const Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue>& item = result.GetResult().GetItem(); if (item.size() > 0) { 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 a New 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

Copy
#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

Copy
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

Copy
#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

Copy
Aws::Client::ClientConfiguration clientConfig; Aws::DynamoDB::DynamoDBClient dynamoClient(clientConfig); Aws::DynamoDB::Model::UpdateItemRequest uir; uir.SetTableName(table); Aws::DynamoDB::Model::AttributeValue av; av.SetS(name); uir.AddKey("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]); Aws::DynamoDB::Model::AttributeValueUpdate avu; avu.SetValue(val); uir.AddAttributeUpdates(flds[0], avu); } else { std::cout << "Invalid argument: " << arg << std::endl << USAGE; return 1; } } const Aws::DynamoDB::Model::UpdateItemOutcome& result = dynamoClient.UpdateItem(uir); if (!result.IsSuccess()) { std::cout << result.GetError().GetMessage() << std::endl; return 1; } std::cout << "Done!" << std::endl;

See the complete example.

More Info