Memperbarui pemesanan

Perbarui pemesanan BigQuery yang ada untuk mengubah propertinya, seperti kapasitas slotnya.

Contoh kode

Node.js

Sebelum mencoba contoh ini, ikuti petunjuk penyiapan Node.js di Panduan memulai BigQuery menggunakan library klien. Untuk mengetahui informasi selengkapnya, lihat Dokumentasi referensi BigQuery Node.js API.

Untuk melakukan autentikasi ke BigQuery, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, lihat Menyiapkan autentikasi untuk library klien.

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

const client = new ReservationServiceClient();

/**
 * Updates an existing reservation resource.
 * A reservation is a mechanism used to guarantee slots to users. This sample
 * shows how to change the slot capacity of a reservation.
 * @param {string} projectId Google Cloud project ID. for example 'example-project-id'.
 * @param {string} location Google Cloud location. for example 'us-central1'.
 * @param {string} reservationId ID of the reservation to update. for example 'example-reservation'.
 */
async function updateReservation(
  projectId,
  location = 'us-central1',
  reservationId = 'example-group-reservation',
) {
  const request = {
    reservation: {
      name: client.reservationPath(projectId, location, reservationId),
      slotCapacity: 150,
    },
    updateMask: {
      paths: ['slot_capacity'],
    },
  };

  try {
    const [updatedReservation] = await client.updateReservation(request);
    console.log(`Updated reservation: ${updatedReservation.name}`);
    console.log(`  New slot capacity: ${updatedReservation.slotCapacity}`);
  } catch (err) {
    if (err.code === status.NOT_FOUND) {
      console.log(
        `Reservation "${reservationId}" not found in project "${projectId}" location "${location}".`,
      );
    } else {
      console.error(`Error updating reservation "${reservationId}":`, err);
    }
  }
}

Python

Sebelum mencoba contoh ini, ikuti petunjuk penyiapan Python di Panduan memulai BigQuery menggunakan library klien. Untuk mengetahui informasi selengkapnya, lihat Dokumentasi referensi BigQuery Python API.

Untuk melakukan autentikasi ke BigQuery, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, lihat Menyiapkan autentikasi untuk library klien.

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


def update_reservation(project_id: str, location: str, reservation_id: str):
    """Updates a reservation's slot capacity.

    A reservation must exist before it can be updated.

    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 update.
    """
    client = bigquery_reservation_v1.ReservationServiceClient()

    reservation = bigquery_reservation_v1.types.Reservation()
    reservation.name = client.reservation_path(project_id, location, reservation_id)
    reservation.slot_capacity = 200
    update_mask = field_mask_pb2.FieldMask(paths=["slot_capacity"])

    try:
        updated_reservation = client.update_reservation(
            reservation=reservation, update_mask=update_mask
        )
        print(f"Updated reservation: {updated_reservation.name}")
        print(f"New slot capacity: {updated_reservation.slot_capacity}")
    except exceptions.NotFound:
        print(f"Reservation '{reservation_id}' was not found in location '{location}'.")

Langkah berikutnya

Untuk menelusuri dan memfilter contoh kode untuk produk Google Cloud lainnya, lihat Google Cloud browser contoh.