予約グループを作成する

特定のプロジェクトとロケーションに予約グループを作成します。予約グループは、BigQuery 予約のコンテナです。

コードサンプル

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();

/**
 * Creates a new reservation group.
 * A reservation group is a container for reservations. This can be useful for
 * managing reservations for different teams or workloads.
 *
 * @param projectId Google Cloud project ID, for example 'example-project-id'.
 * @param location The Google Cloud location where the reservation group will be created, for example 'us-central1'.
 * @param reservationGroupId The ID to use for the reservation group, for example 'example-reservation-group'.
 */
async function createReservationGroup(projectId,
  location = 'us-central1',
  reservationGroupId = 'example-group-reservation',) {
  const request = {
    parent: `projects/${projectId}/locations/${location}`,
    reservationGroupId,
    reservationGroup: {},
  };

  try {
    const [reservationGroup] = await client.createReservationGroup(request);
    console.log(`Created reservation group: ${reservationGroup.name}`);
  } catch (err) {
    if (err.code === status.ALREADY_EXISTS) {
      console.log(
        `Reservation group '${reservationGroupId}' already exists in project '${projectId}' in location '${location}'.`,
      );
    } else {
      console.error('Error creating reservation group:', 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 create_reservation_group(project_id: str, location: str, reservation_group_id: str):
    """Creates a reservation group.

    A reservation group is a container for reservations.

    Args:
        project_id: The Google Cloud project ID.
        location: The geographic location where the reservation group should be created.
        reservation_group_id: The ID of the reservation group to create.
            It must only contain lower case alphanumeric characters or dashes.
            It must start with a letter and must not end with a dash.
            Its maximum length is 64 characters.
    """
    parent = f"projects/{project_id}/locations/{location}"
    reservation_group = bigquery_reservation_v1.types.ReservationGroup()

    request = bigquery_reservation_v1.CreateReservationGroupRequest(
        parent=parent,
        reservation_group_id=reservation_group_id,
        reservation_group=reservation_group,
    )

    try:
        response = client.create_reservation_group(request=request)
        print(f"Created reservation group: {response.name}")
    except exceptions.AlreadyExists:
        full_reservation_group_name = client.reservation_group_path(
            project_id, location, reservation_group_id
        )
        print(f"Reservation group '{full_reservation_group_name}' already exists.")

次のステップ

他の Google Cloud プロダクトのコードサンプルを検索およびフィルタするには、Google Cloud サンプル ブラウザをご覧ください。