Ottenere una prenotazione

Recupera una prenotazione BigQuery specificata. Utilizza questa opzione per controllare i dettagli di una prenotazione, ad esempio la capacità degli slot e se ignora gli slot inattivi.

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.reservation.v1.GetReservationRequest;
import com.google.cloud.bigquery.reservation.v1.Reservation;
import com.google.cloud.bigquery.reservation.v1.ReservationName;
import com.google.cloud.bigquery.reservation.v1.ReservationServiceClient;
import java.io.IOException;

public class GetReservation {

  /**
   * Retrieves information about a specified BigQuery reservation. A reservation provides
   * computational resource guarantees, in the form of slots, to users.
   *
   * @param projectId The Google Cloud project ID (for example, "example-project-id").
   * @param location The geographic location of the reservation (for example, "us-central1").
   * @param reservationId The ID of the reservation to retrieve (for example,
   *     "example-reservation").
   */
  public static void getReservation(String projectId, String location, String reservationId) {
    ReservationName name = ReservationName.of(projectId, location, reservationId);

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

    try (ReservationServiceClient client = ReservationServiceClient.create()) {
      Reservation reservation = client.getReservation(request);
      System.out.printf("Retrieved reservation: %s%n", reservation.getName());
      System.out.printf("  Slot capacity: %d%n", reservation.getSlotCapacity());
      System.out.printf("  Ignore idle slots: %b%n", reservation.getIgnoreIdleSlots());
    } catch (NotFoundException err) {
      System.err.printf(
          "error: reservation '%s' not found in location '%s' for project '%s': %s%n",
          reservationId, location, projectId, err.getMessage());
    } catch (ApiException | IOException err) {
      System.err.printf(
          "error: failed to get reservation '%s' in location '%s' for project '%s': %s%n",
          reservationId, location, projectId, 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 {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

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

Passaggi successivi

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