Last active
July 1, 2020 07:26
-
-
Save Rjerk/023c2caac02a682945bfa97d76f21cd8 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
| 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; | |
| } | |
| int main(int argc, char** argv) | |
| { | |
| const std::string USAGE = "\n" | |
| "Usage:\n" | |
| " update_item <tableName> <partitionKey> <rangeKey> \n\n" | |
| "Where:\n" | |
| " tableName - name of the table to put the item in\n" | |
| " partitionKey - the key value to update\n" | |
| " rangeKey - attribute=updated value\n\n" | |
| "Examples:\n" | |
| " update_item bucket_stats liulan .dir.bucket1.1\n"; | |
| if (argc < 4) | |
| { | |
| std::cout << USAGE; | |
| return 1; | |
| } | |
| Aws::SDKOptions options; | |
| options.loggingOptions.logLevel = Aws::Utils::Logging::LogLevel::Trace; | |
| Aws::InitAPI(options); | |
| { | |
| const Aws::String tableName(argv[1]); | |
| const Aws::String uid(argv[2]); | |
| const Aws::String bi(argv[3]); | |
| Aws::Client::ClientConfiguration clientConfig; | |
| clientConfig.endpointOverride = "http://localhost:8000"; | |
| clientConfig.scheme = Aws::Http::Scheme::HTTP; | |
| clientConfig.maxConnections = 256; | |
| Aws::DynamoDB::DynamoDBClient dynamoClient(clientConfig); | |
| // *** Define UpdateItem request arguments | |
| // Define TableName argument | |
| Aws::DynamoDB::Model::UpdateItemRequest request; | |
| // using String = Aws::String; | |
| // using Aws::DynamoDB::Model::AttributeValue; | |
| using Aws::String; | |
| using Aws::DynamoDB::Model::AttributeValue; | |
| Aws::Map<Aws::String, AttributeValue> keyMap { | |
| { "uid", AttributeValue(uid) }, | |
| { "bi", AttributeValue(bi) }, | |
| }; | |
| // Construct the SET update expression argument | |
| String update_expression("SET " | |
| "sc.#sc.#total = sc.#sc.#total + :size, " | |
| "sc.#sc.#total_r = sc.#sc.#total_r + :size, " | |
| "sc.#sc.#entries = sc.#sc.#entries + :num, " | |
| "sc.#sc.#actual = sc.#sc.#actual + :size" | |
| ); | |
| std::cout << update_expression << std::endl; | |
| Aws::Map<String, String> expressionAttributeNames; | |
| expressionAttributeNames["#sc"] = "standard"; | |
| expressionAttributeNames["#total"] = "total_size"; | |
| expressionAttributeNames["#total_r"] = "total_size_rounded"; | |
| expressionAttributeNames["#entries"] = "num_entries"; | |
| expressionAttributeNames["#actual"] = "actual_size"; | |
| // Construct attribute value argument | |
| Aws::Map<String, AttributeValue> expressionAttributeValues { | |
| { ":size", AttributeValue().SetN(1024) }, | |
| { ":num", AttributeValue().SetN(1) }, | |
| }; | |
| request.WithTableName(tableName) | |
| .WithKey(keyMap) | |
| .WithExpressionAttributeNames(expressionAttributeNames) | |
| .WithExpressionAttributeValues(expressionAttributeValues) | |
| .WithUpdateExpression(update_expression); | |
| // // Update the item works fine. | |
| // const Aws::DynamoDB::Model::UpdateItemOutcome& result = dynamoClient.UpdateItem(request); | |
| // if (!result.IsSuccess()) { | |
| // std::cout << result.GetError().GetMessage() << std::endl; | |
| // return 1; | |
| // } else { | |
| // std::cout << "Item was updated" << std::endl; | |
| // } | |
| std::function<void (const Aws::DynamoDB::DynamoDBClient *, | |
| const Aws::DynamoDB::Model::UpdateItemRequest&, | |
| const Aws::DynamoDB::Model::UpdateItemOutcome&, | |
| const std::shared_ptr<const Aws::Client::AsyncCallerContext>&)> updateItemHandler = UpdateItemOutcomeReceived; | |
| // UpdateItemAsync get error type: Aws::DynamoDB::DynamoDBErrors::INCOMPLETE_SIGNATURE | |
| dynamoClient.UpdateItemAsync(request, updateItemHandler); | |
| } | |
| Aws::ShutdownAPI(options); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment