Obtenir l'état de l'opération

Découvrez comment récupérer l'état d'une opération.

Exemple de code

Java

Pour vous authentifier auprès d'AutoML Tables, configurez les Identifiants par défaut de l'application. Pour en savoir plus, consultez Configurer l'authentification pour un environnement de développement local.

import com.google.cloud.automl.v1beta1.AutoMlClient;
import com.google.longrunning.Operation;
import java.io.IOException;

class GetOperationStatus {

  static void getOperationStatus() throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    String operationFullId = "projects/[projectId]/locations/us-central1/operations/[operationId]";
    getOperationStatus(operationFullId);
  }

  // Get the status of an operation
  static void getOperationStatus(String operationFullId) throws IOException {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the "close" method on the client to safely clean up any remaining background resources.
    try (AutoMlClient client = AutoMlClient.create()) {
      // Get the latest state of a long-running operation.
      Operation operation = client.getOperationsClient().getOperation(operationFullId);

      // Display operation details.
      System.out.println("Operation details:");
      System.out.format("\tName: %s%n", operation.getName());
      System.out.format("\tMetadata Type Url: %s%n", operation.getMetadata().getTypeUrl());
      System.out.format("\tDone: %s%n", operation.getDone());
      if (operation.hasResponse()) {
        System.out.format("\tResponse Type Url: %s%n", operation.getResponse().getTypeUrl());
      }
      if (operation.hasError()) {
        System.out.println("\tResponse:");
        System.out.format("\t\tError code: %s%n", operation.getError().getCode());
        System.out.format("\t\tError message: %s%n", operation.getError().getMessage());
      }
    }
  }
}

Python

Pour vous authentifier auprès d'AutoML Tables, configurez les Identifiants par défaut de l'application. Pour en savoir plus, consultez Configurer l'authentification pour un environnement de développement local.

# TODO(developer): Uncomment and set the following variables
# operation_full_id =
#   'projects/<projectId>/locations/<region>/operations/<operationId>'

from google.cloud import automl_v1beta1 as automl

client = automl.TablesClient()

# Get the latest state of a long-running operation.
op = client.auto_ml_client._transport.operations_client.get_operation(
    operation_full_id
)

print(f"Operation status: {op}")

Étapes suivantes

Pour rechercher et filtrer des exemples de code pour d'autres produits Google Cloud, consultez l'exemple de navigateur Google Cloud.