מחיקת הזמנה ב-BigQuery

מוחקת הקצאה מ-BigQuery Reservation API. אסור שיהיו הקצאות כלשהן בהזמנה.

דוגמת קוד

Java

לפני שמנסים את הדוגמה הזו, צריך לפעול לפי הוראות ההגדרה של Java במדריך למתחילים של BigQuery באמצעות ספריות לקוח. למידע נוסף, קראו את מאמרי העזרה של BigQuery Java API.

כדי לבצע אימות ב-BigQuery, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לספריות לקוח.

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

לפני שמנסים את הדוגמה הזו, צריך לפעול לפי הוראות ההגדרה של Node.js במדריך למתחילים של BigQuery באמצעות ספריות לקוח. למידע נוסף, קראו את מאמרי העזרה של BigQuery Node.js API.

כדי לבצע אימות ב-BigQuery, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לספריות לקוח.

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

לפני שמנסים את הדוגמה הזו, צריך לפעול לפי הוראות ההגדרה של Python במדריך למתחילים של BigQuery באמצעות ספריות לקוח. למידע נוסף, קראו את מאמרי העזרה של BigQuery Python API.

כדי לבצע אימות ב-BigQuery, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לספריות לקוח.

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

המאמרים הבאים

כדי לחפש ולסנן דוגמאות קוד למוצרים אחרים של Google Cloud , ראו דפדפן לדוגמה שלGoogle Cloud .