Aggiornare una prenotazione BI

Aggiorna le dimensioni di una prenotazione BI esistente. BigQuery BI Engine utilizza le prenotazioni BI per eseguire analisi in memoria rapide.

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.BiReservation;
import com.google.cloud.bigquery.reservation.v1.BiReservationName;
import com.google.cloud.bigquery.reservation.v1.ReservationServiceClient;
import com.google.cloud.bigquery.reservation.v1.UpdateBiReservationRequest;
import com.google.protobuf.FieldMask;
import java.io.IOException;

public class UpdateBiReservation {

  /**
   * Updates the singleton BigQuery BI Engine reservation size in a given project and location. A
   * singleton BI reservation always exists with a default size of 0 bytes.
   *
   * @param projectId The Google Cloud project ID (for example, "example-project-id").
   * @param location The geographic location of the BI reservation (for example, "us-central1").
   */
  public static void updateBiReservation(String projectId, String location) {
    BiReservationName name = BiReservationName.of(projectId, location);

    // The size of a BI reservation is measured in bytes. 2 GB is used here.
    BiReservation biReservation =
        BiReservation.newBuilder().setName(name.toString()).setSize(2_000_000_000L).build();

    FieldMask updateMask = FieldMask.newBuilder().addPaths("size").build();

    UpdateBiReservationRequest request =
        UpdateBiReservationRequest.newBuilder()
            .setBiReservation(biReservation)
            .setUpdateMask(updateMask)
            .build();

    try (ReservationServiceClient client = ReservationServiceClient.create()) {
      BiReservation response = client.updateBiReservation(request);
      System.out.printf("Updated BI reservation: %s%n", response.getName());
      System.out.printf("New size is %d bytes.%n", response.getSize());
    } catch (NotFoundException err) {
      System.err.printf(
          "error: BI reservation not found in location '%s' for project '%s': %s%n",
          location, projectId, err.getMessage());
    } catch (ApiException | IOException err) {
      System.err.printf(
          "error: failed to update BI reservation in location '%s' for project '%s': %s%n",
          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();

/**
 * Updates a BI reservation.
 * A singleton BI reservation always exists with a default size of 0. To reserve
 * BI capacity, you must update the reservation to an amount greater than 0. To
 * release BI capacity, set the reservation size to 0.
 *
 * @param {string} projectId Google Cloud Project ID, for example 'example-project-id'
 * @param {string} location Google Cloud Location, for example 'US'
 */
async function updateBiReservation(projectId, location = 'US') {
  const biReservation = {
    name: client.biReservationPath(projectId, location),
    // Set the reservation size in bytes. This example sets it to 2 GiB.
    size: 2 * 1024 * 1024 * 1024,
  };
  const updateMask = {
    paths: ['size'],
  };

  const request = {
    biReservation,
    updateMask,
  };

  try {
    const [updatedReservation] = await client.updateBiReservation(request);
    console.log(`Updated BI reservation: ${updatedReservation.name}`);
    console.log(`  Size: ${updatedReservation.size} bytes`);
  } catch (err) {
    if (err.code === status.NOT_FOUND) {
      console.log(
        `BI Reservation not found for project ${projectId} in location ${location}.`,
      );
    } else {
      console.error('Error updating BI 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
from google.protobuf import field_mask_pb2

client = bigquery_reservation_v1.ReservationServiceClient()


def update_bi_reservation(project_id: str, location: str):
    """Updates a BI reservation.

    A singleton BI reservation always exists with default size 0.
    To reserve BI capacity, update the reservation to an amount
    greater than 0. To release BI capacity, set the reservation size to 0.

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

    name = client.bi_reservation_path(project=project_id, location=location)
    bi_reservation = bigquery_reservation_v1.types.BiReservation()
    bi_reservation.name = name

    # The size of a BI reservation is measured in bytes. 2 GB is used here.
    bi_reservation.size = 2 * 10**9
    update_mask = field_mask_pb2.FieldMask(paths=["size"])

    try:
        response = client.update_bi_reservation(
            bi_reservation=bi_reservation, update_mask=update_mask
        )
        print(f"Updated BI reservation: {response.name}")
        print(f"New size is {response.size} bytes.")
    except exceptions.NotFound:
        print(
            f"BI reservation not found for project '{project_id}' in location '{location}'."
        )

Passaggi successivi

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