Lister toutes les sous-tâches de migration pour un workflow

Liste toutes les sous-tâches de migration pour un workflow de migration spécifié. Utilisez-le pour surveiller l'état des étapes individuelles d'une migration BigQuery plus importante.

Exemple de code

Node.js

Avant d'essayer cet exemple, suivez les instructions de configuration pour Node.js du guide de démarrage rapide de BigQuery : Utiliser les bibliothèques clientes. Pour en savoir plus, consultez la documentation de référence de l'API BigQuery pour Node.js.

Pour vous authentifier auprès de BigQuery, configurez le service Identifiants par défaut de l'application. Pour en savoir plus, consultez la page Configurer l'authentification pour les bibliothèques clientes.

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

Avant d'essayer cet exemple, suivez les instructions de configuration pour Python du guide de démarrage rapide de BigQuery : Utiliser les bibliothèques clientes. Pour en savoir plus, consultez la documentation de référence de l'API BigQuery pour Python.

Pour vous authentifier auprès de BigQuery, configurez le service Identifiants par défaut de l'application. Pour en savoir plus, consultez la page Configurer l'authentification pour les bibliothèques clientes.

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}")

Étape suivante

Pour rechercher et filtrer des exemples de code pour d'autres produits Google Cloud , consultez l'explorateur d'exemplesGoogle Cloud .