-
Notifications
You must be signed in to change notification settings - Fork 4
/
put_item.cpp
executable file
·128 lines (106 loc) · 5.16 KB
/
put_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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//snippet-sourcedescription:[put_item.cpp demonstrates how to put an item into 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.put_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/PutItemRequest.h>
#include <aws/dynamodb/model/PutItemResult.h>
#include <iostream>
//snippet-end:[dynamodb.cpp.put_item.inc]
/*
Put an item in 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"
" <tableName> <key> <keyVal> <albumtitle> <albumtitleval> <awards> <awardsval> <Songtitle> <songtitleval>\n\n"
"Where:\n"
" tableName - the Amazon DynamoDB table in which an item is placed (for example, Music3).\n"
" key - the key used in the Amazon DynamoDB table (for example, Artist).\n"
" keyval - the key value that represents the item to get (for example, Famous Band).\n"
" albumTitle - album title (for example, AlbumTitle).\n"
" AlbumTitleValue - the name of the album (for example, Songs About Life ).\n"
" Awards - the awards column (for example, Awards).\n"
" AwardVal - the value of the awards (for example, 10).\n"
" SongTitle - the song title (for example, SongTitle).\n"
" SongTitleVal - the value of the song title (for example, Happy Day).\n"
"**Warning** This program will place an item that you specify into a table!\n";
if (argc < 9)
{
std::cout << USAGE;
return 1;
}
Aws::SDKOptions options;
Aws::InitAPI(options);
{
const Aws::String table = (argv[1]);
const Aws::String key = (argv[2]);
const Aws::String keyVal = (argv[3]);
const Aws::String albumTitle = (argv[4]);
const Aws::String AlbumTitleValue = (argv[5]);
const Aws::String Awards = (argv[6]);
const Aws::String AwardVal = (argv[7]);
const Aws::String SongTitle = (argv[8]);
const Aws::String SongTitleVal = (argv[9]);
const Aws::String SponsorTitle = "Sponsor";
Aws::Vector<std::shared_ptr<Aws::DynamoDB::Model::AttributeValue>> SponsorVals;
//Aws::Vector<Aws::DynamoDB::Model::AttributeValue> animalsVal;
std::shared_ptr<Aws::DynamoDB::Model::AttributeValue> SponsorPtr = Aws::MakeShared<Aws::DynamoDB::Model::AttributeValue>( SponsorTitle.c_str() );
SponsorPtr->SetS("dog");
SponsorVals.push_back(std::make_shared<Aws::DynamoDB::Model::AttributeValue>(*SponsorPtr));
SponsorPtr->SetS("cat");
SponsorVals.push_back(std::make_shared<Aws::DynamoDB::Model::AttributeValue>(*SponsorPtr));
SponsorPtr->SetS("mouse");
SponsorVals.push_back(std::make_shared<Aws::DynamoDB::Model::AttributeValue>(*SponsorPtr));
SponsorPtr->SetS("stoat");
SponsorVals.push_back(std::make_shared<Aws::DynamoDB::Model::AttributeValue>(*SponsorPtr));
SponsorPtr->SetS("snake");
SponsorVals.push_back(std::make_shared<Aws::DynamoDB::Model::AttributeValue>(*SponsorPtr));
// snippet-start:[dynamodb.cpp.put_item.code]
Aws::Client::ClientConfiguration clientConfig;
Aws::DynamoDB::DynamoDBClient dynamoClient(clientConfig);
Aws::DynamoDB::Model::PutItemRequest putItemRequest;
putItemRequest.SetTableName(table);
Aws::DynamoDB::Model::AttributeValue av;
av.SetS(keyVal);
Aws::DynamoDB::Model::AttributeValue album;
album.SetS(AlbumTitleValue);
Aws::DynamoDB::Model::AttributeValue awards;
awards.SetS(AwardVal);
Aws::DynamoDB::Model::AttributeValue song;
song.SetS(SongTitleVal);
Aws::DynamoDB::Model::AttributeValue sponsor;
sponsor.SetL(SponsorVals);
// Add all AttributeValue objects.
putItemRequest.AddItem(key, av);
putItemRequest.AddItem(albumTitle, album);
putItemRequest.AddItem(Awards, awards);
putItemRequest.AddItem(SongTitle, song);
putItemRequest.AddItem(SponsorTitle, sponsor);
const Aws::DynamoDB::Model::PutItemOutcome result = dynamoClient.PutItem(putItemRequest);
if (!result.IsSuccess())
{
std::cout << result.GetError().GetMessage() << std::endl;
return 1;
}
std::cout << "Successfully added Item!" << std::endl;
// snippet-end:[dynamodb.cpp.put_item.code]
}
Aws::ShutdownAPI(options);
return 0;
}