列出迁移工作流

列出指定项目中 BigQuery Migration API 的所有迁移工作流。

代码示例

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 workflows in a project.
 *
 * This is useful to get an overview of all migration workflows in a project and location.
 *
 * @param {string} projectId The Google Cloud project ID (for example, 'example-project-id')
 * @param {string} location The Google Cloud location (for example, 'us')
 */
async function listMigrationWorkflows(projectId, location = 'us') {
  const request = {
    parent: client.locationPath(projectId, location),
  };

  try {
    const [workflows] = await client.listMigrationWorkflows(request);

    if (workflows.length === 0) {
      console.error(
        `No workflows found in project ${projectId} at location ${location}.`,
      );
      return;
    }

    console.log('Workflows:');
    for (const workflow of workflows) {
      console.log(`  ${workflow.name}`);
      console.log(`    Display Name: ${workflow.displayName}`);
      console.log(`    State: ${workflow.state}`);
    }
  } catch (err) {
    if (err.code === status.NOT_FOUND) {
      console.error(`Project ${projectId} or location ${location} not found.`);
    } else {
      console.error('Error listing migration workflows:', err);
    }
  }
}

Python

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

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

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

client = bigquery_migration_v2.MigrationServiceClient()


def list_migration_workflows(project_id: str, location: str) -> None:
    """Lists migration workflows in a given project and location.

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

    parent = f"projects/{project_id}/locations/{location}"

    try:
        workflow_pager = client.list_migration_workflows(parent=parent)

        print(f"Workflows in parent '{parent}':")
        count = 0
        for workflow in workflow_pager:
            print(f"- {workflow.name}")
            count += 1

        if not count:
            print("No migration workflows found.")

    except exceptions.NotFound:
        print(f"The parent resource '{parent}' was not found.")
        print("Please check that the project ID and location are correct.")

后续步骤

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