Übertragungsvorgänge auflisten

Listet alle Ausführungen für eine BigQuery Data Transfer Service-Übertragungskonfiguration auf.

Weitere Informationen

Eine ausführliche Dokumentation, die dieses Codebeispiel enthält, finden Sie hier:

Codebeispiel

Java

Bevor Sie dieses Beispiel anwenden, folgen Sie den Schritten zur Einrichtung von Java in der BigQuery-Kurzanleitung zur Verwendung von Clientbibliotheken. Weitere Angaben finden Sie in der Referenzdokumentation zur BigQuery Java API.

Richten Sie zur Authentifizierung bei BigQuery die Standardanmeldedaten für Anwendungen ein. Weitere Informationen finden Sie unter Authentifizierung für Clientbibliotheken einrichten.

import com.google.api.gax.rpc.ApiException;
import com.google.cloud.bigquery.datatransfer.v1.DataTransferServiceClient;
import com.google.cloud.bigquery.datatransfer.v1.ListTransferRunsRequest;
import java.io.IOException;

// Sample to get run history from transfer config.
public class RunHistory {

  public static void main(String[] args) throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    String configId = "MY_CONFIG_ID";
    // i.e projects/{project_id}/transferConfigs/{config_id}` or
    // `projects/{project_id}/locations/{location_id}/transferConfigs/{config_id}`
    runHistory(configId);
  }

  public static void runHistory(String configId) throws IOException {
    try (DataTransferServiceClient dataTransferServiceClient = DataTransferServiceClient.create()) {
      ListTransferRunsRequest request =
          ListTransferRunsRequest.newBuilder().setParent(configId).build();
      dataTransferServiceClient
          .listTransferRuns(request)
          .iterateAll()
          .forEach(run -> System.out.print("Success! Run ID :" + run.getName() + "\n"));
    } catch (ApiException ex) {
      System.out.println("Run history not found due to error." + ex.toString());
    }
  }
}

Node.js

Bevor Sie dieses Beispiel ausprobieren, folgen Sie der Node.js-Einrichtungsanleitung in der BigQuery-Kurzanleitung zur Verwendung von Clientbibliotheken. Weitere Angaben finden Sie in der Referenzdokumentation zur BigQuery Node.js API.

Richten Sie zur Authentifizierung bei BigQuery die Standardanmeldedaten für Anwendungen ein. Weitere Informationen finden Sie unter Authentifizierung für Clientbibliotheken einrichten.

const {
  DataTransferServiceClient,
} = require('@google-cloud/bigquery-data-transfer');
const {status} = require('@grpc/grpc-js');

const client = new DataTransferServiceClient();

/**
 * Lists transfer runs for a given transfer configuration.
 *
 * This sample shows how to list all the transfer runs for a specific transfer
 * configuration, to monitor and audit data transfer jobs.
 *
 * @param {string} projectId The Google Cloud project ID, for example 'example-project-id'.
 * @param {string} location The Google Cloud region, for example 'us-central1'.
 * @param {string} configId The BigQuery Transfer Service config ID, for example 'example-config-id'.
 */
async function listTransferRuns(projectId, location, configId) {
  const parent = `projects/${projectId}/locations/${location}/transferConfigs/${configId}`;
  const request = {
    parent,
  };

  try {
    const [transferRuns] = await client.listTransferRuns(request);
    if (transferRuns.length === 0) {
      console.error(
        `No transfer runs found in project ${projectId} for ${location}.`,
      );
      return;
    }
    console.log(`Transfer runs for config '${configId}':`);
    for (const run of transferRuns) {
      console.log(`\nTransfer Run: ${run.name}`);
      console.log(`  Data Source Id: ${run.dataSourceId}`);
      if (run.runTime && run.runTime.seconds) {
        console.log(
          `  Run time: ${new Date(run.runTime.seconds * 1000).toISOString()}`,
        );
      }
      console.log(`  State: ${run.state}`);
    }
  } catch (err) {
    if (err.code === status.NOT_FOUND) {
      console.error(`Transfer run not found: ${parent}`);
    } else {
      console.error('Error listing transfer runs:', err);
    }
  }
}

Python

Bevor Sie dieses Beispiel ausprobieren, folgen Sie der Python-Einrichtungsanleitung in der BigQuery-Kurzanleitung zur Verwendung von Clientbibliotheken. Weitere Angaben finden Sie in der Referenzdokumentation zur BigQuery Python API.

Richten Sie zur Authentifizierung bei BigQuery die Standardanmeldedaten für Anwendungen ein. Weitere Informationen finden Sie unter Authentifizierung für Clientbibliotheken einrichten.

import google.api_core.exceptions
from google.cloud import bigquery_datatransfer_v1

client = bigquery_datatransfer_v1.DataTransferServiceClient()


def list_transfer_runs(project_id: str, transfer_config_id: str, location: str) -> None:
    """Lists transfer runs for a given transfer configuration.

    This method retrieves a history of runs for a specific data transfer job.

    Args:
        project_id: The Google Cloud project ID.
        transfer_config_id: The data transfer configuration ID.
        location: The geographic location of the transfer configuration (e.g., 'us-central1').
    """

    parent = client.transfer_config_path(
        project=f"{project_id}/locations/{location}",
        transfer_config=transfer_config_id,
    )
    request = bigquery_datatransfer_v1.ListTransferRunsRequest(
        parent=parent,
    )

    try:
        pager = client.list_transfer_runs(request=request)
        for run in pager:
            print(f"  Run: {run.name}, State: {run.state.name}")
    except google.api_core.exceptions.NotFound:
        print(
            f"Error: Transfer configuration not found: '{parent}'."
            "Please verify that the project ID, location, and transfer config ID are correct."
        )

Weitere Informationen

Wenn Sie nach Codebeispielen für andere Produkte von Google Cloud suchen und filtern möchten, können Sie den Beispielbrowser fürGoogle Cloud verwenden.