D'autres exemples de AWS SDK sont disponibles dans le référentiel AWS Doc SDK Examples
Les traductions sont fournies par des outils de traduction automatique. En cas de conflit entre le contenu d'une traduction et celui de la version originale en anglais, la version anglaise prévaudra.
Utilisation SearchIndex avec un AWS SDK ou une CLI
Les exemples de code suivants illustrent comment utiliser SearchIndex.
- .NET
-
- SDK pour .NET (v4)
-
Note
Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS
. /// <summary> /// Searches for IoT Things using the search index. /// </summary> /// <param name="queryString">The search query string.</param> /// <returns>List of Things that match the search criteria, or empty list if search failed.</returns> public async Task<List<ThingDocument>> SearchIndexAsync(string queryString) { try { // First, try to perform the search var request = new SearchIndexRequest { QueryString = queryString }; var response = await _amazonIoT.SearchIndexAsync(request); _logger.LogInformation($"Search found {response.Things.Count} Things"); return response.Things; } catch (Amazon.IoT.Model.IndexNotReadyException ex) { _logger.LogWarning($"Search index not ready, setting up indexing configuration: {ex.Message}"); return await SetupIndexAndRetrySearchAsync(queryString); } catch (Amazon.IoT.Model.ResourceNotFoundException ex) when (ex.Message.Contains("index") || ex.Message.Contains("Index")) { _logger.LogWarning($"Search index not configured, setting up indexing configuration: {ex.Message}"); return await SetupIndexAndRetrySearchAsync(queryString); } catch (Amazon.IoT.Model.ThrottlingException ex) { _logger.LogWarning($"Request throttled, please try again later: {ex.Message}"); return new List<ThingDocument>(); } catch (Exception ex) { _logger.LogError($"Couldn't search index. Here's why: {ex.Message}"); return new List<ThingDocument>(); } } /// <summary> /// Sets up the indexing configuration and retries the search after waiting for the index to be ready. /// </summary> /// <param name="queryString">The search query string.</param> /// <returns>List of Things that match the search criteria, or empty list if setup/search failed.</returns> private async Task<List<ThingDocument>> SetupIndexAndRetrySearchAsync(string queryString) { try { // Update indexing configuration to REGISTRY mode _logger.LogInformation("Setting up IoT search indexing configuration..."); await _amazonIoT.UpdateIndexingConfigurationAsync( new UpdateIndexingConfigurationRequest() { ThingIndexingConfiguration = new ThingIndexingConfiguration() { ThingIndexingMode = ThingIndexingMode.REGISTRY } }); _logger.LogInformation("Indexing configuration updated. Waiting for index to be ready..."); // Wait for the index to be set up - this can take some time const int maxRetries = 10; const int retryDelaySeconds = 10; for (int attempt = 1; attempt <= maxRetries; attempt++) { try { _logger.LogInformation($"Waiting for index to be ready (attempt {attempt}/{maxRetries})..."); await Task.Delay(TimeSpan.FromSeconds(retryDelaySeconds)); // Try to get the current indexing configuration to see if it's ready var configResponse = await _amazonIoT.GetIndexingConfigurationAsync(new GetIndexingConfigurationRequest()); if (configResponse.ThingIndexingConfiguration?.ThingIndexingMode == ThingIndexingMode.REGISTRY) { // Try the search again var request = new SearchIndexRequest { QueryString = queryString }; var response = await _amazonIoT.SearchIndexAsync(request); _logger.LogInformation($"Search found {response.Things.Count} Things after index setup"); return response.Things; } } catch (Amazon.IoT.Model.IndexNotReadyException) { // Index still not ready, continue waiting _logger.LogInformation("Index still not ready, continuing to wait..."); continue; } catch (Amazon.IoT.Model.InvalidRequestException ex) when (ex.Message.Contains("index") || ex.Message.Contains("Index")) { // Index still not ready, continue waiting _logger.LogInformation("Index still not ready, continuing to wait..."); continue; } } _logger.LogWarning("Timeout waiting for search index to be ready after configuration update"); return new List<ThingDocument>(); } catch (Exception ex) { _logger.LogError($"Couldn't set up search index configuration. Here's why: {ex.Message}"); return new List<ThingDocument>(); } }-
Pour plus de détails sur l'API, reportez-vous SearchIndexà la section Référence des AWS SDK pour .NET API.
-
- C++
-
- SDK pour C++
-
Note
Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS
. //! 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; }-
Pour plus de détails sur l'API, reportez-vous SearchIndexà la section Référence des AWS SDK pour C++ API.
-
- CLI
-
- AWS CLI
-
Pour interroger l’index des objets
L’exemple
search-indexsuivant interroge l’indexAWS_Thingspour les objets de typeLightBulb.aws iot search-index \ --index-name"AWS_Things"\ --query-string"thingTypeName:LightBulb"Sortie :
{ "things": [ { "thingName": "MyLightBulb", "thingId": "40da2e73-c6af-406e-b415-15acae538797", "thingTypeName": "LightBulb", "thingGroupNames": [ "LightBulbs", "DeadBulbs" ], "attributes": { "model": "123", "wattage": "75" }, "connectivity": { "connected": false } }, { "thingName": "ThirdBulb", "thingId": "615c8455-33d5-40e8-95fd-3ee8b24490af", "thingTypeName": "LightBulb", "attributes": { "model": "123", "wattage": "75" }, "connectivity": { "connected": false } }, { "thingName": "MyOtherLightBulb", "thingId": "6dae0d3f-40c1-476a-80c4-1ed24ba6aa11", "thingTypeName": "LightBulb", "attributes": { "model": "123", "wattage": "75" }, "connectivity": { "connected": false } } ] }Pour plus d’informations, consultez Gestion de l’indexation d’objet dans le Guide du développeur AWS IoT.
-
Pour plus de détails sur l'API, voir SearchIndex
la section Référence des AWS CLI commandes.
-
- Java
-
- SDK pour Java 2.x
-
Note
Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS
. /** * Searches for IoT Things asynchronously based on a query string. * * @param queryString The query string to search for Things. * * This method initiates an asynchronous request to search for IoT Things. * If the request is successful and Things are found, it prints their IDs. * If no Things are found, it prints a message indicating so. * If an exception occurs, it prints the error message. */ public void searchThings(String queryString) { SearchIndexRequest searchIndexRequest = SearchIndexRequest.builder() .queryString(queryString) .build(); CompletableFuture<SearchIndexResponse> future = getAsyncClient().searchIndex(searchIndexRequest); future.whenComplete((searchIndexResponse, ex) -> { if (searchIndexResponse != null) { // Process the result. if (searchIndexResponse.things().isEmpty()) { System.out.println("No things found."); } else { searchIndexResponse.things().forEach(thing -> System.out.println("Thing id found using search is " + thing.thingId())); } } else { Throwable cause = ex != null ? ex.getCause() : null; if (cause instanceof IotException) { System.err.println(((IotException) cause).awsErrorDetails().errorMessage()); } else if (cause != null) { System.err.println("Unexpected error: " + cause.getMessage()); } else { System.err.println("Failed to search for IoT Things."); } } }); future.join(); }-
Pour plus de détails sur l'API, reportez-vous SearchIndexà la section Référence des AWS SDK for Java 2.x API.
-
- Kotlin
-
- SDK pour Kotlin
-
Note
Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS
. suspend fun searchThings(queryStringVal: String?) { val searchIndexRequest = SearchIndexRequest { queryString = queryStringVal } IotClient.fromEnvironment { region = "us-east-1" }.use { iotClient -> val searchIndexResponse = iotClient.searchIndex(searchIndexRequest) if (searchIndexResponse.things?.isEmpty() == true) { println("No things found.") } else { searchIndexResponse.things ?.forEach { thing -> println("Thing id found using search is ${thing.thingId}") } } } }-
Pour plus de détails sur l'API, reportez-vous SearchIndex
à la section AWS SDK pour la référence de l'API Kotlin.
-