取得遷移工作流程

從 BigQuery Migration Service 擷取特定遷移工作流程。

程式碼範例

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

public class GetMigrationWorkflow {

  /**
   * Gets a previously created migration workflow. A migration workflow is a collection of tasks
   * that orchestrates a data warehouse migration to BigQuery.
   *
   * @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 to retrieve (for example,
   *     "12345678-abcd-4372-a567-0e02b2c3d479").
   */
  public static void getMigrationWorkflow(String projectId, String location, String workflowId) {
    MigrationWorkflowName name = MigrationWorkflowName.of(projectId, location, workflowId);

    GetMigrationWorkflowRequest request =
        GetMigrationWorkflowRequest.newBuilder().setName(name.toString()).build();

    try (MigrationServiceClient client = MigrationServiceClient.create()) {
      MigrationWorkflow workflow = client.getMigrationWorkflow(request);
      System.out.printf("Migration workflow found: %s%n", workflow.getName());
      System.out.printf("  Display Name: %s%n", workflow.getDisplayName());
      System.out.printf("  State: %s%n", workflow.getState());
    } catch (NotFoundException err) {
      System.out.printf("Migration workflow not found: %s%n", name);
    } catch (ApiException | IOException err) {
      System.err.printf(
          "error: failed to get migration workflow '%s': %s%n", name, 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();

/**
 * Gets a previously created migration workflow.
 *
 * A migration workflow is a collection of tasks that you can create to support
 * data warehouse migration to BigQuery.
 *
 * @param {string} projectId The Google Cloud 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 getMigrationWorkflow(
  projectId,
  location = 'us',
  workflowId = '12345678-abcd-4372-a567-0e02b2c3d479',
) {
  const name = client.migrationWorkflowPath(projectId, location, workflowId);
  const request = {
    name,
  };

  try {
    const [workflow] = await client.getMigrationWorkflow(request);
    console.log(`Migration workflow found: ${workflow.name}`);
    console.log(`  Display Name: ${workflow.displayName}`);
    console.log(`  State: ${workflow.state}`);
  } catch (err) {
    if (err.code === status.NOT_FOUND) {
      console.error(`Migration workflow not found: ${name}`);
    } else {
      console.error('Error getting migration workflow:', 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 get_migration_workflow(project_id: str, location: str, workflow_id: str) -> None:
    """Gets a previously created migration workflow.

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

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

    try:
        workflow = client.get_migration_workflow(name=name)
        print(f"Successfully retrieved migration workflow: {workflow.name}")
        print(f"Display Name: {workflow.display_name}")
        print(f"State: {workflow.state.name}")
    except NotFound:
        print(f"Migration workflow not found: {name}")

後續步驟

如要搜尋及篩選其他 Google Cloud 產品的程式碼範例,請參閱Google Cloud 範例瀏覽工具。