取得遷移子工作

從 BigQuery 遷移服務取得特定遷移子工作的詳細資料。這項功能有助於檢查大型遷移工作流程中某個元件的狀態或設定。

程式碼範例

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 subtask.
 *
 * A migration subtask is a single unit of work that is part of a migration workflow.
 *
 * @param {string} projectId The Google Cloud project ID (for example, 'example-project-id').
 * @param {string} location The Google Cloud region where the migration workflow is located (for example, 'us').
 * @param {string} workflowId The ID of the migration workflow that contains the subtask (for example, '12345678-abcd-4372-a567-0e02b2c3d479').
 * @param {string} subtaskId The ID of the migration subtask to retrieve (for example, '98765432-dcba-4370-971e-7ff74afce823').
 */
async function getMigrationSubtask(
  projectId,
  location = 'us',
  workflowId = '12345678-abcd-4372-a567-0e02b2c3d479',
  subtaskId = '98765432-dcba-4370-971e-7ff74afce823',
) {
  const name = client.migrationSubtaskPath(
    projectId,
    location,
    workflowId,
    subtaskId,
  );
  const request = {
    name,
  };

  try {
    const [subtask] = await client.getMigrationSubtask(request);

    console.log(`Migration subtask found: ${subtask.name}`);
    console.log(`  Type: ${subtask.type}`);
    console.log(`  State: ${subtask.state}`);
  } catch (err) {
    if (err.code === status.NOT_FOUND) {
      console.error(`Migration subtask not found: ${name}`);
    } else {
      console.error('Error getting migration subtask:', 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_subtask(
    project_id: str, location: str, workflow_id: str, subtask_id: str
) -> None:
    """Gets a previously created migration subtask.

    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.
        subtask_id: The ID of the migration subtask.
    """

    name = client.migration_subtask_path(project_id, location, workflow_id, subtask_id)

    try:
        subtask = client.get_migration_subtask(name=name)
        print(f"Migration subtask found: {subtask.name}")
        print(f"State: {subtask.state.name}")
        print(f"Type: {subtask.type}")
    except NotFound:
        print(f"Migration subtask not found: {name}")

後續步驟

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