Mencantumkan grup reservasi

Mencantumkan semua grup pemesanan BigQuery dalam project dan lokasi yang ditentukan.

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.ListReservationGroupsRequest;
import com.google.cloud.bigquery.reservation.v1.LocationName;
import com.google.cloud.bigquery.reservation.v1.ReservationGroup;
import com.google.cloud.bigquery.reservation.v1.ReservationServiceClient;
import java.io.IOException;

public class ListReservationGroups {

  /**
   * Lists all BigQuery reservation groups in a given project and location. A reservation group is
   * a container for reservations.
   *
   * @param projectId The Google Cloud project ID (for example, "example-project-id").
   * @param location The geographic location of the reservation groups (for example, "us-central1").
   */
  public static void listReservationGroups(String projectId, String location) {
    LocationName parent = LocationName.of(projectId, location);

    ListReservationGroupsRequest request =
        ListReservationGroupsRequest.newBuilder().setParent(parent.toString()).build();

    try (ReservationServiceClient client = ReservationServiceClient.create()) {
      System.out.printf("Listed reservation groups for parent: '%s'%n", parent);
      for (ReservationGroup group : client.listReservationGroups(request).iterateAll()) {
        System.out.printf("  Reservation group: %s%n", group.getName());
      }
    } catch (NotFoundException err) {
      System.err.printf(
          "error: parent resource '%s' not found: %s%n", parent, err.getMessage());
    } catch (ApiException | IOException err) {
      System.err.printf(
          "error: failed to list reservation groups in '%s': %s%n", parent, 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('@grpc/grpc-js');

const client = new ReservationServiceClient();

/**
 * Lists all reservation groups for a project in a specified location.
 * A reservation group is a container for reservations.
 *
 * @param {string} projectId Google Cloud project ID (for example, 'example-project-id').
 * @param {string} location Google Cloud location (for example, 'us-central1').
 */
async function listReservationGroups(projectId, location = 'us-central1') {
  const request = {
    parent: `projects/${projectId}/locations/${location}`,
  };

  try {
    const [reservationGroups] = await client.listReservationGroups(request);

    if (reservationGroups.length === 0) {
      console.log(
        `No reservation groups found in project ${projectId} at location ${location}.`,
      );
      return;
    }

    console.log(
      `Reservation groups in project ${projectId} at location ${location}:`,
    );
    for (const group of reservationGroups) {
      console.log(`  ${group.name}`);
    }
  } catch (err) {
    if (err.code === status.NOT_FOUND) {
      console.log(`Project or location not found: ${request.parent}`);
    } else {
      console.error('Error listing reservation groups:', 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.

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

client = bigquery_reservation_v1.ReservationServiceClient()


def list_reservation_groups(project_id: str, location: str):
    """Lists all reservation groups for the project in the specified location.

    A reservation group is a container for reservations. This sample shows
    how to list all reservation groups within a specific project and location.

    Args:
        project_id: The Google Cloud project ID.
        location: The geographic location of the reservation groups, for example, "us-central1".
    """
    parent = f"projects/{project_id}/locations/{location}"

    try:
        page_result = client.list_reservation_groups(parent=parent)
        print(f"Listed reservation groups for parent: '{parent}'")
        for group in page_result:
            print(f"  Reservation group: {group.name}")
    except google.api_core.exceptions.NotFound:
        print(f"Parent resource '{parent}' was not found.")
    except google.api_core.exceptions.GoogleAPICallError as e:
        print(
            f"Could not list reservation groups. Please check your permissions. Error: {e}"
        )

Langkah berikutnya

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