取得預留項目群組

依名稱從 BigQuery Reservation API 擷取特定預留項目群組資源。

程式碼範例

Java

在試用這個範例之前,請先按照「使用用戶端程式庫的 BigQuery 快速入門導覽課程」中的 Java 設定操作說明進行操作。詳情請參閱 BigQuery Java API 參考文件。

如要向 BigQuery 進行驗證,請設定應用程式預設憑證。詳情請參閱「設定用戶端程式庫的驗證機制」。

import com.google.api.gax.rpc.ApiException;
import com.google.api.gax.rpc.NotFoundException;
import com.google.cloud.bigquery.reservation.v1.GetReservationGroupRequest;
import com.google.cloud.bigquery.reservation.v1.ReservationGroup;
import com.google.cloud.bigquery.reservation.v1.ReservationGroupName;
import com.google.cloud.bigquery.reservation.v1.ReservationServiceClient;
import java.io.IOException;

public class GetReservationGroup {

  /**
   * Retrieves information about a specified BigQuery reservation group. 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 group (for example, "us-central1").
   * @param reservationGroupId The ID of the reservation group to retrieve (for example,
   *     "example-reservation-group").
   */
  public static void getReservationGroup(
      String projectId, String location, String reservationGroupId) {
    ReservationGroupName name =
        ReservationGroupName.of(projectId, location, reservationGroupId);

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

    try (ReservationServiceClient client = ReservationServiceClient.create()) {
      ReservationGroup reservationGroup = client.getReservationGroup(request);
      System.out.printf("Retrieved reservation group: %s%n", reservationGroup.getName());
    } catch (NotFoundException err) {
      System.err.printf(
          "error: reservation group '%s' not found in location '%s' for project '%s': %s%n",
          reservationGroupId, location, projectId, err.getMessage());
    } catch (ApiException | IOException err) {
      System.err.printf(
          "error: failed to get reservation group '%s' in location '%s' for project '%s': %s%n",
          reservationGroupId, location, projectId, err.getMessage());
    }
  }

Node.js

在試用這個範例之前,請先按照「使用用戶端程式庫的 BigQuery 快速入門導覽課程」中的 Node.js 設定操作說明進行操作。詳情請參閱 BigQuery Node.js API 參考文件。

如要向 BigQuery 進行驗證,請設定應用程式預設憑證。詳情請參閱「設定用戶端程式庫的驗證機制」。

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

const client = new ReservationServiceClient();

/**
 * Gets the specified reservation group.
 *
 * A reservation group is a container for reservations.
 *
 * @param projectId Google Cloud Project ID, for example 'example-project-id'.
 * @param location Google Cloud Location, for example 'us-central1'.
 * @param reservationGroupId the ID of the reservation group to get, for example 'example-reservation-group-id'.
 */
async function getReservationGroup(
  projectId,
  location = 'us-central1',
  reservationGroupId = 'example-group-reservation',
) {
  const request = {
    name: client.reservationGroupPath(projectId, location, reservationGroupId),
  };

  try {
    const [reservationGroup] = await client.getReservationGroup(request);
    console.log(`Got reservation group: ${reservationGroup.name}`);
  } catch (err) {
    if (err.code === status.NOT_FOUND) {
      console.log(
        `Reservation group '${reservationGroupId}' not found in project '${projectId}' at location '${location}'.`,
      );
    } else {
      console.error(
        `Error getting reservation group '${reservationGroupId}':`,
        err,
      );
    }
  }
}

Python

在試用這個範例之前,請先按照「使用用戶端程式庫的 BigQuery 快速入門導覽課程」中的 Python 設定操作說明進行操作。詳情請參閱 BigQuery Python API 參考文件。

如要向 BigQuery 進行驗證,請設定應用程式預設憑證。詳情請參閱「設定用戶端程式庫的驗證機制」。

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


client = bigquery_reservation_v1.ReservationServiceClient()


def get_reservation_group(project_id: str, location: str, reservation_group_id: str):
    """Gets information about a reservation group.

    A reservation group is a container for reservations.

    Args:
        project_id: The Google Cloud project ID.
        location: The geographic location of the reservation group, for example, us-central1.
        reservation_group_id: The ID of the reservation group to retrieve.
    """
    name = client.reservation_group_path(project_id, location, reservation_group_id)

    try:
        reservation_group = client.get_reservation_group(name=name)
        print(f"Retrieved reservation group: {reservation_group.name}")
    except exceptions.NotFound:
        print(f"Reservation group '{name}' not found.")

後續步驟

如要搜尋及篩選其他 Google Cloud 產品的程式碼範例,請參閱Google Cloud 範例瀏覽工具。