Recupero dello stato dell'operazione

Dimostra il recupero di uno stato di un'operazione.

Esempio di codice

Java

Per eseguire l'autenticazione su AutoML Tables, configura le Credenziali predefinite dell'applicazione. Per maggiori informazioni, consulta Configurare l'autenticazione per un ambiente di sviluppo locale.

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

Per eseguire l'autenticazione su AutoML Tables, configura le Credenziali predefinite dell'applicazione. Per maggiori informazioni, consulta Configurare l'autenticazione per un ambiente di sviluppo locale.

# 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}")

Passaggi successivi

Per cercare e filtrare esempi di codice per altri prodotti Google Cloud, consulta il browser di esempio Google Cloud.