Creare un'assegnazione di prenotazione

Crea un'assegnazione per mappare un progetto, una cartella o un'organizzazione a una prenotazione BigQuery.

Esempio di codice

Node.js

Prima di provare questo esempio, segui le istruzioni di configurazione di Node.js nella guida rapida di BigQuery per l'utilizzo delle librerie client. Per saperne di più, consulta la documentazione di riferimento dell'API BigQuery Node.js.

Per eseguire l'autenticazione in BigQuery, configura le Credenziali predefinite dell'applicazione. Per saperne di più, vedi Configurare l'autenticazione per le librerie client.

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

Prima di provare questo esempio, segui le istruzioni di configurazione di Python nella guida rapida di BigQuery per l'utilizzo delle librerie client. Per saperne di più, consulta la documentazione di riferimento dell'API BigQuery Python.

Per eseguire l'autenticazione in BigQuery, configura le Credenziali predefinite dell'applicazione. Per saperne di più, vedi Configurare l'autenticazione per le librerie client.

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.")

Passaggi successivi

Per cercare e filtrare gli esempi di codice per altri prodotti Google Cloud , consulta il browser degli esempi diGoogle Cloud .