Get the operation status

Demonstrate retrieving an operation status.

Code sample

Java

To authenticate to AutoML Tables, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

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

To authenticate to AutoML Tables, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

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

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.