Menghapus reservasi BigQuery

Menghapus pemesanan dari BigQuery Reservation API. Pemesanan tidak boleh memiliki penetapan apa pun.

Contoh kode

Java

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

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

import com.google.api.gax.rpc.ApiException;
import com.google.api.gax.rpc.NotFoundException;
import com.google.cloud.bigquery.reservation.v1.DeleteReservationRequest;
import com.google.cloud.bigquery.reservation.v1.ReservationName;
import com.google.cloud.bigquery.reservation.v1.ReservationServiceClient;
import java.io.IOException;

public class DeleteReservation {

  /**
   * Deletes a specified BigQuery reservation. A reservation can only be deleted if it has no
   * active assignments.
   *
   * @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 delete (for example, "example-reservation").
   */
  public static void deleteReservation(String projectId, String location, String reservationId) {
    ReservationName name = ReservationName.of(projectId, location, reservationId);

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

    try (ReservationServiceClient client = ReservationServiceClient.create()) {
      client.deleteReservation(request);
      System.out.printf("Deleted reservation: %s%n", name);
    } 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 delete reservation '%s' in location '%s' for project '%s': %s%n",
          reservationId, location, projectId, err.getMessage());
    }
  }

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('google-gax');

const client = new ReservationServiceClient();

/**
 * Deletes a reservation.
 * A reservation provides computational resource guarantees, in the form of slots, to users.
 * @param {string} projectId The Google Cloud 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 delete, for example 'example-reservation'.
 */
async function deleteReservation(
  projectId,
  location = 'us-central1',
  reservationId = 'example-reservation',
) {
  // Construct the fully-qualified path for the reservation.
  const name = client.reservationPath(projectId, location, reservationId);

  const request = {
    name,
  };

  try {
    await client.deleteReservation(request);
    console.log(`Deleted reservation: ${reservationId}`);
  } catch (err) {
    if (err.code === status.NOT_FOUND) {
      console.log(
        `Reservation ${reservationId} not found in project ${projectId} location ${location}.`,
      );
    } else {
      console.error(`Error deleting 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.exceptions import NotFound
from google.cloud import bigquery_reservation_v1

client = bigquery_reservation_v1.ReservationServiceClient()


def delete_reservation(project_id: str, location: str, reservation_id: str):
    """Deletes a reservation.

    A reservation can only be deleted if it has no assignments.

    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 delete.
    """
    name = client.reservation_path(project_id, location, reservation_id)

    try:
        client.delete_reservation(name=name)
        print(f"Deleted reservation: {name}")
    except NotFound:
        print(f"Reservation '{name}' not found.")

Langkah berikutnya

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