예약 할당 만들기

프로젝트, 폴더 또는 조직을 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 an assignment to assign a project to a reservation.
 * A reservation assignment lets a project submit jobs of a certain type
 * using slots from the specified reservation.
 *
 * @param {string} projectId Google Cloud Project ID that owns the reservation, for example 'example-project-id'.
 * @param {string} location Google Cloud Location, for example 'us-central1'.
 * @param {string} reservationId The ID of the reservation to which the project will be assigned, for example 'example-reservation'.
 */
async function createAssignment(
  projectId,
  location = 'us-central1',
  reservationId = 'example-reservation',
) {
  const parent = client.reservationPath(projectId, location, reservationId);
  const request = {
    parent,
    assignment: {
      assignee: `projects/${projectId}`,
      jobType: 'QUERY',
    },
  };

  try {
    const [assignment] = await client.createAssignment(request);
    console.log(`Created assignment: ${assignment.name}`);
    console.log(`  Assignee: ${assignment.assignee}`);
    console.log(`  Job Type: ${assignment.jobType}`);
  } catch (err) {
    if (err.code === status.ALREADY_EXISTS) {
      console.log(
        `Assignment for project ${projectId} to reservation ${reservationId} already exists.`,
      );
    } else {
      console.error('Error creating assignment:', 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_assignment(project_id: str, location: str, reservation_id: str):
    """Creates a new assignment for a reservation.

    This shows how to create an assignment that gives a project access to
    a reservation.

    Args:
        project_id: The Google Cloud project ID of the reservation owner.
        location: The geographic location of the reservation, for example, "US".
        reservation_id: The ID of the reservation to create the assignment in.
    """

    parent = client.reservation_path(project_id, location, reservation_id)
    assignee = f"projects/{project_id}"
    assignment = bigquery_reservation_v1.Assignment(
        assignee=assignee,
        job_type=bigquery_reservation_v1.Assignment.JobType.QUERY,
    )

    try:
        created_assignment = client.create_assignment(
            parent=parent, assignment=assignment
        )
        print(f"Created assignment: {created_assignment.name}")
    except exceptions.AlreadyExists:
        print(f"Assignment for project {project_id} and job type QUERY already exists.")

다음 단계

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