SIGN IN SIGN UP

Welcome to the AWS Code Examples Repository. This repo contains code examples used in the AWS documentation, AWS SDK Developer Guides, and more. For more information, see the Readme.md file below.

0 0 1 Java
2024-03-04 19:40:50 -05:00
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
/**
* Before running this C++ code example, set up your development environment, including your credentials.
*
* For more information, see the following documentation topic:
*
* https://docs.aws.amazon.com/sdk-for-cpp/v1/developer-guide/getting-started.html
*
* For information on the structure of the code examples and how to build and run the examples, see
* https://docs.aws.amazon.com/sdk-for-cpp/v1/developer-guide/getting-started-code-examples.html.
*
**/
#include <aws/core/Aws.h>
#include <aws/iot/IoTClient.h>
#include <aws/iot/model/SearchIndexRequest.h>
#include <iostream>
#include "iot_samples.h"
// snippet-start:[cpp.example_code.iot.SearchIndex]
//! Query the AWS IoT fleet index.
//! For query information, see https://docs.aws.amazon.com/iot/latest/developerguide/query-syntax.html
/*!
\param: query: The query string.
\param clientConfiguration: AWS client configuration.
\return bool: Function succeeded.
*/
bool AwsDoc::IoT::searchIndex(const Aws::String &query,
const Aws::Client::ClientConfiguration &clientConfiguration) {
Aws::IoT::IoTClient iotClient(clientConfiguration);
Aws::IoT::Model::SearchIndexRequest request;
request.SetQueryString(query);
Aws::Vector<Aws::IoT::Model::ThingDocument> allThingDocuments;
Aws::String nextToken; // Used for pagination.
do {
if (!nextToken.empty()) {
request.SetNextToken(nextToken);
}
Aws::IoT::Model::SearchIndexOutcome outcome = iotClient.SearchIndex(request);
if (outcome.IsSuccess()) {
const Aws::IoT::Model::SearchIndexResult &result = outcome.GetResult();
allThingDocuments.insert(allThingDocuments.end(),
result.GetThings().cbegin(),
result.GetThings().cend());
nextToken = result.GetNextToken();
}
else {
std::cerr << "Error in SearchIndex: " << outcome.GetError().GetMessage()
<< std::endl;
return false;
}
} while (!nextToken.empty());
std::cout << allThingDocuments.size() << " thing document(s) found." << std::endl;
for (const auto thingDocument: allThingDocuments) {
std::cout << " Thing name: " << thingDocument.GetThingName() << "."
<< std::endl;
}
return true;
}
// snippet-end:[cpp.example_code.iot.SearchIndex]
/*
*
* main function
*
* Usage: 'run_search_index <query>'
*
*/
#ifndef EXCLUDE_ACTION_MAIN
int main(int argc, char **argv) {
if (argc != 2) {
std::cout << "Usage: 'run_search_index <query>'" << std::endl;
return 1;
}
Aws::SDKOptions options;
Aws::InitAPI(options);
{
const Aws::String query(argv[1]);
Aws::Client::ClientConfiguration clientConfig;
// Optional: Set to the AWS Region (overrides config file).
// clientConfig.region = "us-east-1";
AwsDoc::IoT::searchIndex(query, clientConfig);
}
Aws::ShutdownAPI(options);
return 0;
}
#endif // EXCLUDE_ACTION_MAIN