Update the size of an existing BI reservation. The BigQuery BI Engine uses BI reservations to run fast, in-memory analysis.

## Code sample

### Java


Before trying this sample, follow the Java setup instructions in the
[BigQuery quickstart using
client libraries](https://docs.cloud.google.com/bigquery/docs/quickstarts/quickstart-client-libraries).


For more information, see the
[BigQuery Java API
reference documentation](https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/overview).


To authenticate to BigQuery, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/bigquery/docs/authentication#client-libs).

    import com.google.api.gax.rpc.https://docs.cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.rpc.ApiException.html;
    import com.google.api.gax.rpc.https://docs.cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.rpc.NotFoundException.html;
    import com.google.cloud.bigquery.reservation.v1.https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryreservation/latest/com.google.cloud.bigquery.reservation.v1.BiReservation.html;
    import com.google.cloud.bigquery.reservation.v1.https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryreservation/latest/com.google.cloud.bigquery.reservation.v1.BiReservationName.html;
    import com.google.cloud.bigquery.reservation.v1.https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryreservation/latest/com.google.cloud.bigquery.reservation.v1.ReservationServiceClient.html;
    import com.google.cloud.bigquery.reservation.v1.https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryreservation/latest/com.google.cloud.bigquery.reservation.v1.UpdateBiReservationRequest.html;
    import com.google.protobuf.https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.FieldMask.html;
    import java.io.IOException;

    public class UpdateBiReservation {

      /**
       * Updates the singleton BigQuery BI Engine reservation size in a given project and location. A
       * singleton BI reservation always exists with a default size of 0 bytes.
       *
       * @param projectId The Google Cloud project ID (for example, "example-project-id").
       * @param location The geographic location of the BI reservation (for example, "us-central1").
       */
      public static void updateBiReservation(String projectId, String location) {
        https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryreservation/latest/com.google.cloud.bigquery.reservation.v1.BiReservationName.html name = https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryreservation/latest/com.google.cloud.bigquery.reservation.v1.BiReservationName.html.of(projectId, location);

        // The size of a BI reservation is measured in bytes. 2 GB is used here.
        https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryreservation/latest/com.google.cloud.bigquery.reservation.v1.BiReservation.html biReservation =
            https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryreservation/latest/com.google.cloud.bigquery.reservation.v1.BiReservation.html.newBuilder().setName(name.https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryreservation/latest/com.google.cloud.bigquery.reservation.v1.BiReservationName.html#com_google_cloud_bigquery_reservation_v1_BiReservationName_toString__()).https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryreservation/latest/com.google.cloud.bigquery.reservation.v1.BiReservation.Builder.html#com_google_cloud_bigquery_reservation_v1_BiReservation_Builder_setSize_long_(2_000_000_000L).build();

        https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.FieldMask.html updateMask = https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.FieldMask.html.newBuilder().https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.FieldMask.Builder.html#com_google_protobuf_FieldMask_Builder_addPaths_java_lang_String_("size").build();

        https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryreservation/latest/com.google.cloud.bigquery.reservation.v1.UpdateBiReservationRequest.html request =
            https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryreservation/latest/com.google.cloud.bigquery.reservation.v1.UpdateBiReservationRequest.html.newBuilder()
                .https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryreservation/latest/com.google.cloud.bigquery.reservation.v1.UpdateBiReservationRequest.Builder.html#com_google_cloud_bigquery_reservation_v1_UpdateBiReservationRequest_Builder_setBiReservation_com_google_cloud_bigquery_reservation_v1_BiReservation_(biReservation)
                .setUpdateMask(updateMask)
                .build();

        try (https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryreservation/latest/com.google.cloud.bigquery.reservation.v1.ReservationServiceClient.html client = https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryreservation/latest/com.google.cloud.bigquery.reservation.v1.ReservationServiceClient.html.create()) {
          https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryreservation/latest/com.google.cloud.bigquery.reservation.v1.BiReservation.html response = client.updateBiReservation(request);
          System.out.printf("Updated BI reservation: %s%n", response.https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryreservation/latest/com.google.cloud.bigquery.reservation.v1.BiReservation.html#com_google_cloud_bigquery_reservation_v1_BiReservation_getName__());
          System.out.printf("New size is %d bytes.%n", response.https://docs.cloud.google.com/java/docs/reference/google-cloud-bigqueryreservation/latest/com.google.cloud.bigquery.reservation.v1.BiReservation.html#com_google_cloud_bigquery_reservation_v1_BiReservation_getSize__());
        } catch (https://docs.cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.rpc.NotFoundException.html err) {
          System.err.printf(
              "error: BI reservation not found in location '%s' for project '%s': %s%n",
              location, projectId, err.getMessage());
        } catch (https://docs.cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.rpc.ApiException.html | IOException err) {
          System.err.printf(
              "error: failed to update BI reservation in location '%s' for project '%s': %s%n",
              location, projectId, err.getMessage());
        }
      }

### Node.js


Before trying this sample, follow the Node.js setup instructions in the
[BigQuery quickstart using
client libraries](https://docs.cloud.google.com/bigquery/docs/quickstarts/quickstart-client-libraries).


For more information, see the
[BigQuery Node.js API
reference documentation](https://googleapis.dev/nodejs/bigquery/latest/index.html).


To authenticate to BigQuery, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/bigquery/docs/authentication#client-libs).

    const {ReservationServiceClient} =
      require('https://docs.cloud.google.com/nodejs/docs/reference/bigquery-reservation/latest/overview.html').v1;
    const {status} = require('@grpc/grpc-js');

    const client = new https://docs.cloud.google.com/nodejs/docs/reference/bigquery-reservation/latest/overview.html();

    /**
     * Updates a BI reservation.
     * A singleton BI reservation always exists with a default size of 0. To reserve
     * BI capacity, you must update the reservation to an amount greater than 0. To
     * release BI capacity, set the reservation size to 0.
     *
     * @param {string} projectId Google Cloud Project ID, for example 'example-project-id'
     * @param {string} location Google Cloud Location, for example 'US'
     */
    async function updateBiReservation(projectId, location = 'US') {
      const biReservation = {
        name: client.https://docs.cloud.google.com/nodejs/docs/reference/bigquery-reservation/latest/bigquery-reservation/v1.reservationserviceclient.html(projectId, location),
        // Set the reservation size in bytes. This example sets it to 2 GiB.
        size: 2 * 1024 * 1024 * 1024,
      };
      const updateMask = {
        paths: ['size'],
      };

      const request = {
        biReservation,
        updateMask,
      };

      try {
        const [updatedReservation] = await client.updateBiReservation(request);
        console.log(`Updated BI reservation: ${updatedReservation.name}`);
        console.log(`  Size: ${updatedReservation.size} bytes`);
      } catch (err) {
        if (err.code === status.NOT_FOUND) {
          console.log(
            `BI Reservation not found for project ${projectId} in location ${location}.`,
          );
        } else {
          console.error('Error updating BI reservation:', err);
        }
      }
    }

### Python


Before trying this sample, follow the Python setup instructions in the
[BigQuery quickstart using
client libraries](https://docs.cloud.google.com/bigquery/docs/quickstarts/quickstart-client-libraries).


For more information, see the
[BigQuery Python API
reference documentation](https://docs.cloud.google.com/python/docs/reference/bigquery/latest).


To authenticate to BigQuery, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/bigquery/docs/authentication#client-libs).

    from google.api_core import exceptions
    from google.cloud import https://docs.cloud.google.com/python/docs/reference/bigqueryreservation/latest
    from google.protobuf import field_mask_pb2

    client = https://docs.cloud.google.com/python/docs/reference/bigqueryreservation/latest.https://docs.cloud.google.com/python/docs/reference/bigqueryreservation/latest/google.cloud.bigquery_reservation_v1.services.reservation_service.ReservationServiceClient.html()


    def update_bi_reservation(project_id: str, location: str):
        """Updates a BI reservation.

        A singleton BI reservation always exists with default size 0.
        To reserve BI capacity, update the reservation to an amount
        greater than 0. To release BI capacity, set the reservation size to 0.

        Args:
            project_id: The Google Cloud project ID.
            location: The geographic location of the BI reservation, for example, us-central1.
        """

        name = client.https://docs.cloud.google.com/python/docs/reference/bigqueryreservation/latest/google.cloud.bigquery_reservation_v1.services.reservation_service.ReservationServiceClient.html#google_cloud_bigquery_reservation_v1_services_reservation_service_ReservationServiceClient_bi_reservation_path(project=project_id, location=location)
        bi_reservation = https://docs.cloud.google.com/python/docs/reference/bigqueryreservation/latest.https://docs.cloud.google.com/python/docs/reference/bigqueryreservation/latest/google.cloud.bigquery_reservation_v1.types.html.https://docs.cloud.google.com/python/docs/reference/bigqueryreservation/latest/google.cloud.bigquery_reservation_v1.types.BiReservation.html()
        bi_reservation.name = name

        # The size of a BI reservation is measured in bytes. 2 GB is used here.
        bi_reservation.size = 2 * 10**9
        update_mask = field_mask_pb2.FieldMask(paths=["size"])

        try:
            response = client.https://docs.cloud.google.com/python/docs/reference/bigqueryreservation/latest/google.cloud.bigquery_reservation_v1.services.reservation_service.ReservationServiceClient.html#google_cloud_bigquery_reservation_v1_services_reservation_service_ReservationServiceClient_update_bi_reservation(
                bi_reservation=bi_reservation, update_mask=update_mask
            )
            print(f"Updated BI reservation: {response.name}")
            print(f"New size is {response.size} bytes.")
        except exceptions.NotFound:
            print(
                f"BI reservation not found for project '{project_id}' in location '{location}'."
            )

## What's next


To search and filter code samples for other Google Cloud products, see the
[Google Cloud sample browser](https://docs.cloud.google.com/docs/samples?product=bigquery).