列出遷移工作流程

列出指定專案中 BigQuery Migration API 的所有遷移工作流程。

程式碼範例

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.ListMigrationWorkflowsRequest;
import com.google.cloud.bigquery.migration.v2.LocationName;
import com.google.cloud.bigquery.migration.v2.MigrationServiceClient;
import com.google.cloud.bigquery.migration.v2.MigrationWorkflow;
import java.io.IOException;

public class ListMigrationWorkflows {

  /**
   * Lists migration workflows in a given project and location.
   *
   * @param projectId The Google Cloud project ID (for example, "example-project-id").
   * @param location The geographic location of the migration workflows (for example, "us").
   */
  public static void listMigrationWorkflows(String projectId, String location) {
    LocationName parent = LocationName.of(projectId, location);

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

    try (MigrationServiceClient client = MigrationServiceClient.create()) {
      System.out.printf("Workflows in parent '%s':%n", parent);
      int count = 0;
      for (MigrationWorkflow workflow : client.listMigrationWorkflows(request).iterateAll()) {
        System.out.printf("  %s%n", workflow.getName());
        System.out.printf("    Display Name: %s%n", workflow.getDisplayName());
        System.out.printf("    State: %s%n", workflow.getState());
        count++;
      }

      if (count == 0) {
        System.out.println("No migration workflows found.");
      }
    } catch (NotFoundException err) {
      System.out.printf("The parent resource '%s' was not found.%n", parent);
    } catch (ApiException | IOException err) {
      System.err.printf(
          "error: failed to list migration workflows in '%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 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 範例瀏覽工具。