Obtén una fuente de datos

Recupera información de una fuente de datos específica, como Google Ads o YouTube, que admite el Servicio de transferencia de datos de BigQuery.

Muestra de código

Java

Antes de probar este ejemplo, sigue las instrucciones de configuración para Java incluidas en la guía de inicio rápido de BigQuery sobre cómo usar bibliotecas cliente. Para obtener más información, consulta la documentación de referencia de la API de BigQuery para Java.

Para autenticarte en BigQuery, configura las credenciales predeterminadas de la aplicación. Si deseas obtener más información, consulta Configura la autenticación para bibliotecas cliente.

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

public class GetDataSource {

  /**
   * Retrieves a data source definition by name.
   *
   * @param projectId The Google Cloud project ID (for example, "example-project-id").
   * @param dataSourceId The data source ID (for example, "google_cloud_storage").
   */
  public static void getDataSource(String projectId, String dataSourceId) {
    DataSourceName name = DataSourceName.ofProjectDataSourceName(projectId, dataSourceId);

    try (DataTransferServiceClient client = DataTransferServiceClient.create()) {
      DataSource response = client.getDataSource(name);

      System.out.printf("Retrieved data source: %s%n", response.getName());
      System.out.printf("Display Name: %s%n", response.getDisplayName());
      System.out.printf("Description: %s%n", response.getDescription());
      System.out.printf("Client ID: %s%n", response.getClientId());
      System.out.printf("Supports custom schedule: %s%n", response.getSupportsCustomSchedule());
    } catch (NotFoundException err) {
      System.err.printf("error: data source '%s' not found: %s%n", name, err.getMessage());
    } catch (ApiException | IOException err) {
      System.err.printf("error: failed to get data source '%s': %s%n", name, err.getMessage());
    }
  }

Node.js

Antes de probar este ejemplo, sigue las instrucciones de configuración para Node.js incluidas en la guía de inicio rápido de BigQuery sobre cómo usar bibliotecas cliente. Para obtener más información, consulta la documentación de referencia de la API de BigQuery para Node.js.

Para autenticarte en BigQuery, configura las credenciales predeterminadas de la aplicación. Si deseas obtener más información, consulta Configura la autenticación para bibliotecas cliente.

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

const client = new DataTransferServiceClient();

/**
 * Retrieves a supported data source and returns its settings.
 * This sample shows how to get a data source's details, to understand its parameters and capabilities before creating a transfer.
 *
 * @param {string} projectId The project ID to use, for example 'example-project-id'
 * @param {string} location The location to use, for example 'us-central1'
 * @param {string} dataSourceId The data source ID to use, for example 'google_cloud_storage'
 */
async function getDataSource(projectId, location, dataSourceId) {
  const name = client.projectLocationDataSourcePath(
    projectId,
    location,
    dataSourceId,
  );

  const request = {
    name,
  };

  try {
    const [dataSource] = await client.getDataSource(request);
    console.log(`Data source ${dataSource.name} retrieved:`);
    console.log(`  Display Name: ${dataSource.displayName}`);
    console.log(`  Description: ${dataSource.description}`);
    console.log(`  Data Source ID: ${dataSource.dataSourceId}`);
  } catch (err) {
    if (err.code === status.NOT_FOUND) {
      console.error(
        `Data source ${dataSourceId} not found in project ${projectId} in location ${location}.`,
      );
    } else {
      console.error(`Error getting data source ${dataSourceId}:`, err);
    }
  }
}

Python

Antes de probar este ejemplo, sigue las instrucciones de configuración para Python incluidas en la guía de inicio rápido de BigQuery sobre cómo usar bibliotecas cliente. Para obtener más información, consulta la documentación de referencia de la API de BigQuery para Python.

Para autenticarte en BigQuery, configura las credenciales predeterminadas de la aplicación. Si deseas obtener más información, consulta Configura la autenticación para bibliotecas cliente.

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

client = bigquery_datatransfer_v1.DataTransferServiceClient()


def get_data_source(project_id: str, data_source_id: str) -> None:
    """Retrieves a data source definition by name.

    This sample shows how to get a data source definition, which includes its
    display name, description, and other properties.

    Args:
        project_id: The Google Cloud project ID.
        data_source_id: The data source ID, for example, 'google_cloud_storage'.
    """
    name = client.data_source_path(project=project_id, data_source=data_source_id)

    try:
        response = client.get_data_source(name=name)
        print(f"Retrieved data source: {response.name}")
        print(f"Display Name: {response.display_name}")
        print(f"Description: {response.description}")
        print(f"Client ID: {response.client_id}")
        print(f"Supports custom schedule: {response.supports_custom_schedule}")
    except google.api_core.exceptions.NotFound:
        print(f"Error: Data source '{name}' was not found.")

¿Qué sigue?

Si quieres buscar y filtrar muestras de código para otros productos de Google Cloud , consulta el navegador de muestras deGoogle Cloud .