Last active
July 15, 2020 05:51
-
-
Save Rjerk/837615a8a0d82a7a7c9bc99343f2e4ee to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #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/KeySchemaElement.h> | |
| #include <aws/dynamodb/model/ProvisionedThroughput.h> | |
| #include <aws/dynamodb/model/ScalarAttributeType.h> | |
| #include <aws/dynamodb/model/CreateTableRequest.h> | |
| #include <aws/dynamodb/model/PutItemRequest.h> | |
| #include <aws/dynamodb/model/PutItemResult.h> | |
| #include <aws/dynamodb/model/UpdateItemRequest.h> | |
| #include <aws/dynamodb/model/UpdateItemResult.h> | |
| #include <aws/core/utils/UnreferencedParam.h> | |
| #include <aws/dynamodb/DynamoDBErrors.h> | |
| using Aws::String; | |
| using Aws::DynamoDB::Model::AttributeDefinition; | |
| using Aws::DynamoDB::Model::AttributeValue; | |
| using Aws::DynamoDB::Model::KeySchemaElement; | |
| using Aws::DynamoDB::Model::CreateTableRequest; | |
| using Aws::DynamoDB::Model::PutItemRequest; | |
| using Aws::DynamoDB::Model::UpdateItemRequest; | |
| pthread_cond_t service_cond = PTHREAD_COND_INITIALIZER; | |
| pthread_mutex_t service_mutex = PTHREAD_MUTEX_INITIALIZER; | |
| void ErrorType(Aws::DynamoDB::DynamoDBErrors type) { | |
| switch (type) { | |
| case Aws::DynamoDB::DynamoDBErrors::INCOMPLETE_SIGNATURE: | |
| std::cout << "The request signature does not conform to AWS standards." << std::endl; | |
| break; | |
| default: | |
| std::cout << "Other DynamoDBErrors ..." << std::endl; | |
| } | |
| } | |
| void UpdateItemOutcomeReceived(const Aws::DynamoDB::DynamoDBClient* sender, | |
| const Aws::DynamoDB::Model::UpdateItemRequest& request, | |
| const Aws::DynamoDB::Model::UpdateItemOutcome& outcome, | |
| const std::shared_ptr<const Aws::Client::AsyncCallerContext>& context) | |
| { | |
| AWS_UNREFERENCED_PARAM(sender); | |
| AWS_UNREFERENCED_PARAM(request); | |
| AWS_UNREFERENCED_PARAM(context); | |
| if (!outcome.IsSuccess()) { | |
| std::cout << "UpdateItem failed. " << std::endl; | |
| std::cout << "ExceptionName: " << outcome.GetError().GetExceptionName() << std::endl; | |
| std::cout << "ShoudRetry: " << outcome.GetError().ShouldRetry() << std::endl; | |
| std::cout << "Message: " << outcome.GetError().GetMessage() << std::endl; | |
| // print `Aws::DynamoDB::DynamoDBErrors::INCOMPLETE_SIGNATURE` | |
| ErrorType(outcome.GetError().GetErrorType()); | |
| return; | |
| } | |
| std::cout << "Item was updated" << std::endl; | |
| pthread_cond_signal(&service_cond); | |
| } | |
| int main(int argc, char** argv) | |
| { | |
| Aws::SDKOptions options; | |
| options.loggingOptions.logLevel = Aws::Utils::Logging::LogLevel::Debug; | |
| Aws::InitAPI(options); | |
| { | |
| Aws::Client::ClientConfiguration clientConfig; | |
| clientConfig.endpointOverride = "http://localhost:8000"; | |
| clientConfig.scheme = Aws::Http::Scheme::HTTP; | |
| clientConfig.maxConnections = 256; | |
| Aws::DynamoDB::DynamoDBClient dynamoClient(clientConfig); | |
| const String table{"new-table"}; | |
| /* | |
| // create a table | |
| CreateTableRequest req1; | |
| Aws::Vector<AttributeDefinition> ad_vec { | |
| AttributeDefinition{}.WithAttributeName("uid").WithAttributeType(Aws::DynamoDB::Model::ScalarAttributeType::S), | |
| AttributeDefinition{}.WithAttributeName("bi").WithAttributeType(Aws::DynamoDB::Model::ScalarAttributeType::S), | |
| }; | |
| Aws::Vector<KeySchemaElement> ks_vec { | |
| KeySchemaElement{}.WithAttributeName("uid").WithKeyType(Aws::DynamoDB::Model::KeyType::HASH), | |
| KeySchemaElement{}.WithAttributeName("bi").WithKeyType(Aws::DynamoDB::Model::KeyType::RANGE), | |
| }; | |
| req1.WithTableName(table) | |
| .WithKeySchema(ks_vec) | |
| .WithAttributeDefinitions(ad_vec) | |
| .WithProvisionedThroughput(Aws::DynamoDB::Model::ProvisionedThroughput{}.WithReadCapacityUnits(5).WithWriteCapacityUnits(5)); | |
| const auto& result1 = dynamoClient.CreateTable(req1); | |
| if (result1.IsSuccess()) { | |
| std::cout << "Table " << result1.GetResult().GetTableDescription().GetTableName() << " was created!\n"; | |
| } else { | |
| std::cout << "Failed to create table:" << result1.GetError().GetMessage(); | |
| return -1; | |
| } | |
| // put items in table | |
| PutItemRequest req2; | |
| Aws::Map<String, AttributeValue> put_items = { | |
| { "uid", AttributeValue("user1") }, | |
| { "bi", AttributeValue("bi1") }, | |
| { "size", AttributeValue().SetN(0) } | |
| }; | |
| req2.WithTableName(table) | |
| .WithItem(put_items); | |
| const auto& result2 = dynamoClient.PutItem(req2); | |
| if (result2.IsSuccess()) { | |
| std::cout << "Put item success!\n"; | |
| } else { | |
| std::cout << "Put item failed: " << result2.GetError() << std::endl; | |
| return -1; | |
| } | |
| */ | |
| // update items | |
| UpdateItemRequest req3; | |
| Aws::Map<String, AttributeValue> key_map { | |
| { "uid", AttributeValue("user1") }, | |
| { "bi", AttributeValue("bi1") }, | |
| }; | |
| Aws::Map<String, String> expr_names = { | |
| { "#size", "size" }, | |
| }; | |
| Aws::Map<String, AttributeValue> expr_vals { | |
| { ":count", AttributeValue().SetN(1024) }, | |
| }; | |
| req3.WithTableName(table) | |
| .WithKey(key_map) | |
| .WithUpdateExpression("SET #size = #size + :count") | |
| .WithExpressionAttributeNames(expr_names) | |
| .WithExpressionAttributeValues(expr_vals); | |
| // UpdateItemAsync failed if missing pthread_mutex_lock | |
| auto updateItemHandler = std::bind(&UpdateItemOutcomeReceived, | |
| std::placeholders::_1, std::placeholders::_2, | |
| std::placeholders::_3, std::placeholders::_4); | |
| pthread_mutex_lock(&service_mutex); | |
| dynamoClient.UpdateItemAsync(req3, updateItemHandler); | |
| pthread_cond_wait(&service_cond, &service_mutex); | |
| pthread_mutex_unlock(&service_mutex); | |
| // // UpdateItem success | |
| // const auto& result3 = dynamoClient.UpdateItem(req3); | |
| // if (!result3.IsSuccess()) { | |
| // std::cout << result3.GetError().GetMessage() << std::endl; | |
| // return 1; | |
| // } else { | |
| // std::cout << "Item was updated" << std::endl; | |
| // } | |
| } | |
| Aws::ShutdownAPI(options); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment