BigQuery 예약 만들기

지정된 프로젝트와 위치에 예약을 만듭니다. 예약은 조직의 워크로드에 할당할 수 있는 전용 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.
 * A reservation provides computational resource guarantees, in the form of
 * slots, to users. A slot is a unit of computational power in BigQuery.
 *
 * @param {string} projectId Google Cloud project ID, for example 'example-project-id'.
 * @param {string} location The geographic location where the reservation should reside, for example 'us-central1'.
 * @param {string} reservationId The ID of the reservation to create, for example 'example-reservation'.
 */
async function createReservation(
  projectId,
  location = 'us-central1',
  reservationId = 'example-reservation',
) {
  const parent = `projects/${projectId}/locations/${location}`;
  const request = {
    parent,
    reservationId,
    reservation: {
      slotCapacity: 100,
      ignoreIdleSlots: false,
    },
  };

  try {
    const [createdReservation] = await client.createReservation(request);
    console.log(`Created reservation: ${createdReservation.name}`);
    console.log(`  Slot capacity: ${createdReservation.slotCapacity}`);
  } catch (err) {
    if (err.code === status.ALREADY_EXISTS) {
      console.log(
        `Reservation ${reservationId} already exists in project ${projectId} at location ${location}.`,
      );
    } else {
      console.error(`Error creating reservation ${reservationId}:`, err);
    }
  }
}

Python

이 샘플을 사용해 보기 전에 BigQuery 빠른 시작: 클라이언트 라이브러리 사용Python 설정 안내를 따르세요. 자세한 내용은 BigQuery Python API 참고 문서를 확인하세요.

BigQuery에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 클라이언트 라이브러리의 인증 설정을 참조하세요.

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

client = bigquery_reservation_v1.ReservationServiceClient()


def create_reservation(project_id: str, location: str, reservation_id: str):
    """Creates a reservation.

    A reservation is a mechanism used to guarantee slots to users.

    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 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 = bigquery_reservation_v1.Reservation(
        slot_capacity=100,
        ignore_idle_slots=True,
    )

    try:
        response = client.create_reservation(
            parent=parent,
            reservation_id=reservation_id,
            reservation=reservation,
        )
        print(f"Created reservation: {response.name}")
    except AlreadyExists:
        print(
            f"Reservation '{reservation_id}' already exists in location '{location}'."
        )

다음 단계

다른 Google Cloud 제품의 코드 샘플을 검색하고 필터링하려면 Google Cloud 샘플 브라우저를 참조하세요.