Cómo obtener una reserva

Recupera una reserva de BigQuery especificada. Úsalo para consultar los detalles de una reserva, como su capacidad de ranuras y si ignora las ranuras inactivas.

Muestra de código

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 {ReservationServiceClient} =
  require('@google-cloud/bigquery-reservation').v1;
const {status} = require('@grpc/grpc-js');

const client = new ReservationServiceClient();

/**
 * Returns information about the reservation.
 * A reservation provides computational resource guarantees, in the form of
 * slots, to users. A slot is a unit of computational power in BigQuery.
 *
 * @param {string} projectId Google Cloud Project ID, for example 'example-project-id'.
 * @param {string} location The geographic location where the reservation resides, for example 'us-central1'.
 * @param {string} reservationId The ID of the reservation to retrieve, for example 'example-reservation'.
 */
async function getReservation(
  projectId,
  location = 'us-central1',
  reservationId = 'example-reservation',
) {
  const request = {
    name: client.reservationPath(projectId, location, reservationId),
  };

  try {
    const [reservation] = await client.getReservation(request);
    console.log(`Got reservation: ${reservation.name}`);
    console.log(`  Slot capacity: ${reservation.slotCapacity}`);
    console.log(`  Ignore idle slots: ${reservation.ignoreIdleSlots}`);
  } catch (err) {
    if (err.code === status.NOT_FOUND) {
      console.log(
        `Reservation '${reservationId}' not found in project '${projectId}' location '${location}'.`,
      );
    } else {
      console.error('Error getting reservation:', 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.

from google.api_core import exceptions
from google.cloud import bigquery_reservation_v1

client = bigquery_reservation_v1.ReservationServiceClient()


def get_reservation(project_id: str, location: str, reservation_id: str):
    """Gets information about a reservation.

    A reservation is a mechanism used to guarantee slots to users.

    Args:
        project_id: The Google Cloud project ID.
        location: The geographic location of the reservation, for example, "us-central1".
        reservation_id: The ID of the reservation to retrieve.
    """

    name = client.reservation_path(project_id, location, reservation_id)

    try:
        reservation = client.get_reservation(name=name)
        print(f"Retrieved reservation: {reservation.name}")
        print(f"\tSlot capacity: {reservation.slot_capacity}")
        print(f"\tIgnore idle slots: {reservation.ignore_idle_slots}")
    except exceptions.NotFound:
        print(f"Reservation '{reservation_id}' 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 .