列出工作流的所有迁移子任务

列出指定迁移工作流的所有迁移子任务。使用此功能可监控大型 BigQuery 迁移中各个步骤的状态。

代码示例

Java

试用此示例之前,请按照 BigQuery 快速入门:使用客户端库中的 Java 设置说明进行操作。 如需了解详情,请参阅 BigQuery Java API 参考文档。

如需向 BigQuery 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为客户端库设置身份验证。

import com.google.api.gax.rpc.ApiException;
import com.google.api.gax.rpc.NotFoundException;
import com.google.cloud.bigquery.migration.v2.ListMigrationSubtasksRequest;
import com.google.cloud.bigquery.migration.v2.MigrationServiceClient;
import com.google.cloud.bigquery.migration.v2.MigrationSubtask;
import com.google.cloud.bigquery.migration.v2.MigrationWorkflowName;
import java.io.IOException;

public class ListMigrationSubtasks {

  /**
   * Lists migration subtasks for a given workflow.
   *
   * @param projectId The Google Cloud project ID (for example, "example-project-id").
   * @param location The geographic location of the migration workflow (for example, "us").
   * @param workflowId The ID of the migration workflow (for example,
   *     "12345678-abcd-4372-a567-0e02b2c3d479").
   */
  public static void listMigrationSubtasks(String projectId, String location, String workflowId) {
    MigrationWorkflowName parent = MigrationWorkflowName.of(projectId, location, workflowId);

    ListMigrationSubtasksRequest request =
        ListMigrationSubtasksRequest.newBuilder().setParent(parent.toString()).build();

    try (MigrationServiceClient client = MigrationServiceClient.create()) {
      System.out.printf("Subtasks for workflow '%s':%n", parent);
      int count = 0;
      for (MigrationSubtask subtask : client.listMigrationSubtasks(request).iterateAll()) {
        System.out.printf("  Subtask Name: %s%n", subtask.getName());
        System.out.printf("    Type: %s%n", subtask.getType());
        System.out.printf("    State: %s%n", subtask.getState());
        count++;
      }

      if (count == 0) {
        System.out.println("No migration subtasks found.");
      }
    } catch (NotFoundException err) {
      System.out.printf("Migration workflow not found: %s%n", parent);
    } catch (ApiException | IOException err) {
      System.err.printf(
          "error: failed to list migration subtasks for '%s': %s%n", parent, err.getMessage());
    }
  }

Node.js

试用此示例之前,请按照 BigQuery 快速入门:使用客户端库中的 Node.js 设置说明进行操作。 如需了解详情,请参阅 BigQuery Node.js API 参考文档。

如需向 BigQuery 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为客户端库设置身份验证。

const {MigrationServiceClient} = require('@google-cloud/bigquery-migration').v2;
const {status} = require('@grpc/grpc-js');

const client = new MigrationServiceClient();

/**
 * Lists all migration subtasks for a given workflow.
 *
 * This is useful to track the progress of a migration by by examining its
 * individual steps.
 *
 * @param {string} projectId The Google Cloud project ID (for example, 'example-project-id').
 * @param {string} location The Google Cloud location of the workflow (for example, 'us').
 * @param {string} workflowId The ID of the migration workflow (for example, '12345678-abcd-4372-a567-0e02b2c3d479').
 */
async function listMigrationSubtasks(projectId, location, workflowId) {
  const parent = client.migrationWorkflowPath(projectId, location, workflowId);
  const request = {
    parent,
  };

  try {
    const [subtasks] = await client.listMigrationSubtasks(request);

    if (subtasks.length === 0) {
      console.error(`No subtasks found for workflow ${parent}.`);
      return;
    }

    console.log(`Subtasks for workflow: ${parent}`);
    for (const subtask of subtasks) {
      console.log(`  Subtask Name: ${subtask.name}`);
      console.log(`    Type: ${subtask.type}`);
      console.log(`    State: ${subtask.state}`);
    }
  } catch (err) {
    if (err.code === status.NOT_FOUND) {
      console.error(`Workflow not found: ${parent}`);
    } else {
      console.error('Error listing migration subtasks:', err);
    }
  }
}

Python

试用此示例之前,请按照 BigQuery 快速入门:使用客户端库中的 Python 设置说明进行操作。 如需了解详情,请参阅 BigQuery Python API 参考文档。

如需向 BigQuery 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为客户端库设置身份验证。

from google.api_core.exceptions import NotFound
from google.cloud import bigquery_migration_v2

client = bigquery_migration_v2.MigrationServiceClient()


def list_migration_subtasks(project_id: str, location: str, workflow_id: str) -> None:
    """Lists migration subtasks for a given workflow.

    Args:
        project_id: The Google Cloud project ID.
        location: The location of the migration workflow, for example, "us".
        workflow_id: The ID of the migration workflow.
    """

    parent = client.migration_workflow_path(project_id, location, workflow_id)

    try:
        print(f"Listing migration subtasks for workflow: {workflow_id}")
        subtasks = client.list_migration_subtasks(parent=parent)
        for subtask in subtasks:
            print(f"  Subtask Name: {subtask.name}")
            print(f"    Type: {subtask.type_}")
            print(f"    State: {subtask.state.name}")
    except NotFound:
        print(f"Migration workflow not found: {parent}")

后续步骤

如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅Google Cloud 示例浏览器。