Recupera un'attività secondaria di migrazione

Recupera i dettagli di un subtask di migrazione specifico da BigQuery Migration Service. Questo è utile per controllare lo stato o la configurazione di un componente all'interno di un workflow di migrazione più ampio.

Esempio di codice

Java

Prima di provare questo esempio, segui le istruzioni di configurazione di Java nella guida rapida di BigQuery per l'utilizzo delle librerie client. Per saperne di più, consulta la documentazione di riferimento dell'API BigQuery Java.

Per eseguire l'autenticazione in BigQuery, configura le Credenziali predefinite dell'applicazione. Per saperne di più, vedi Configura l'autenticazione per le librerie client.

import com.google.api.gax.rpc.ApiException;
import com.google.api.gax.rpc.NotFoundException;
import com.google.cloud.bigquery.migration.v2.GetMigrationSubtaskRequest;
import com.google.cloud.bigquery.migration.v2.MigrationServiceClient;
import com.google.cloud.bigquery.migration.v2.MigrationSubtask;
import com.google.cloud.bigquery.migration.v2.MigrationSubtaskName;
import java.io.IOException;

public class GetMigrationSubtask {

  /**
   * Gets a previously created migration subtask. A migration subtask is a single unit of work that
   * is part of a migration workflow.
   *
   * @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 that contains the subtask (for example,
   *     "12345678-abcd-4372-a567-0e02b2c3d479").
   * @param subtaskId The ID of the migration subtask to retrieve (for example,
   *     "98765432-dcba-4370-971e-7ff74afce823").
   */
  public static void getMigrationSubtask(
      String projectId, String location, String workflowId, String subtaskId) {
    MigrationSubtaskName name =
        MigrationSubtaskName.of(projectId, location, workflowId, subtaskId);

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

    try (MigrationServiceClient client = MigrationServiceClient.create()) {
      MigrationSubtask subtask = client.getMigrationSubtask(request);
      System.out.printf("Migration subtask found: %s%n", subtask.getName());
      System.out.printf("  Type: %s%n", subtask.getType());
      System.out.printf("  State: %s%n", subtask.getState());
    } catch (NotFoundException err) {
      System.out.printf("Migration subtask not found: %s%n", name);
    } catch (ApiException | IOException err) {
      System.err.printf(
          "error: failed to get migration subtask '%s': %s%n", name, err.getMessage());
    }
  }

Node.js

Prima di provare questo esempio, segui le istruzioni di configurazione di Node.js nella guida rapida di BigQuery per l'utilizzo delle librerie client. Per saperne di più, consulta la documentazione di riferimento dell'API BigQuery Node.js.

Per eseguire l'autenticazione in BigQuery, configura le Credenziali predefinite dell'applicazione. Per saperne di più, vedi Configura l'autenticazione per le librerie client.

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

Prima di provare questo esempio, segui le istruzioni di configurazione di Python nella guida rapida di BigQuery per l'utilizzo delle librerie client. Per saperne di più, consulta la documentazione di riferimento dell'API BigQuery Python.

Per eseguire l'autenticazione in BigQuery, configura le Credenziali predefinite dell'applicazione. Per saperne di più, vedi Configura l'autenticazione per le librerie client.

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

Passaggi successivi

Per cercare e filtrare gli esempi di codice per altri prodotti Google Cloud , consulta il browser degli esempi diGoogle Cloud .