-
Notifications
You must be signed in to change notification settings - Fork 4
/
get_item.cpp
executable file
·100 lines (88 loc) · 3.65 KB
/
get_item.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//snippet-sourcedescription:[GetItem.cpp demonstrates how to retrieve an item from an Amazon DynamoDB table.]
//snippet-keyword:[AWS SDK for C++]
//snippet-keyword:[Code Sample]
//snippet-service:[Amazon DynamoDB]
//snippet-sourcetype:[full-example]
//snippet-sourcedate:[11/30/2021]
//snippet-sourceauthor:[scmacdon - aws]
/*
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
//snippet-start:[dynamodb.cpp.get_item.inc]
#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>
//snippet-end:[dynamodb.cpp.get_item.inc]
/*
Get an item from a DynamoDB table.
To run this C++ code example, ensure that you have setup your development environment, including your credentials.
For information, see this documentation topic:
https://docs.aws.amazon.com/sdk-for-cpp/v1/developer-guide/getting-started.html
*/
int main(int argc, char** argv)
{
const std::string USAGE =
"Usage:\n"
" run_get_item <tableName> <pk> <pkval> <sk> <skval>\n"
"Where:\n"
" tableName - the Amazon DynamoDB table from which an item is retrieved (for example, Music). \n"
" pk - the key used in the Amazon DynamoDB table (for example, Artist). \n"
" pkval - the key value that represents the item to get (for example, Acme Band).\n"
" sk - the key used in the Amazon DynamoDB table (for example, SongTitle). \n"
" skval - the key value that represents the item to get (for example, Happy Day).\n"
"Examples:\n"
" run_get_item Music Artist Lanka SongTitle Lanka520520\n";
if (argc < 6)
{
std::cout << USAGE;
return 1;
}
Aws::SDKOptions options;
Aws::InitAPI(options);
{
const Aws::String table = (argv[1]);
const Aws::String key_pk = (argv[2]);
const Aws::String keyval_pk = (argv[3]);
const Aws::String key_sk = (argv[4]);
const Aws::String keyval_sk = (argv[5]);
// snippet-start:[dynamodb.cpp.get_item.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(keyval_pk);
req.AddKey(key_pk, hashKey);
hashKey.SetS(keyval_sk);
req.AddKey(key_sk, hashKey);
// 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 << "Values: " << i.first << ": " << i.second.GetS() << std::endl;
}
else
{
std::cout << "No item found with the key " << key_pk << std::endl;
}
}
else
{
std::cout << "Failed to get item: " << result.GetError().GetMessage();
}
// snippet-end:[dynamodb.cpp.get_item.code]
}
Aws::ShutdownAPI(options);
return 0;
}