演示如何检索操作状态。
代码示例
Java
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
# 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("Operation status: {}".format(op))
后续步骤
如需搜索并过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器。