-
-
Save leighajarett/a143ca807a05e81972a340b68126146b to your computer and use it in GitHub Desktop.
Reservation_API_samples.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "Reservation_API_samples.ipynb", | |
"provenance": [], | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/leighajarett/a143ca807a05e81972a340b68126146b/reservation_api_samples.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 582 | |
}, | |
"id": "Mcd3bwe7XbR5", | |
"outputId": "5b3c4b7c-c9e8-420b-a2fa-5d1b7e5daed3" | |
}, | |
"source": [ | |
"!pip install google-cloud-bigquery-reservation" | |
], | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"Collecting google-cloud-bigquery-reservation\n", | |
"\u001b[?25l Downloading https://files.pythonhosted.org/packages/b9/0a/67b6737fe0846fa973f4dafbe497ef78ff5d56cc35391d7eea149b4908f7/google_cloud_bigquery_reservation-1.2.0-py2.py3-none-any.whl (57kB)\n", | |
"\r\u001b[K |█████▊ | 10kB 11.1MB/s eta 0:00:01\r\u001b[K |███████████▍ | 20kB 15.9MB/s eta 0:00:01\r\u001b[K |█████████████████▏ | 30kB 19.7MB/s eta 0:00:01\r\u001b[K |██████████████████████▉ | 40kB 16.5MB/s eta 0:00:01\r\u001b[K |████████████████████████████▌ | 51kB 8.8MB/s eta 0:00:01\r\u001b[K |████████████████████████████████| 61kB 4.5MB/s \n", | |
"\u001b[?25hRequirement already satisfied: google-api-core[grpc]<2.0.0dev,>=1.26.0 in /usr/local/lib/python3.7/dist-packages (from google-cloud-bigquery-reservation) (1.26.3)\n", | |
"Collecting proto-plus>=1.4.0\n", | |
"\u001b[?25l Downloading https://files.pythonhosted.org/packages/8c/72/6f3f4cdc5bb0294f8d7f3f8aacb617b4c3cb17554ed78f7e28009162c795/proto_plus-1.19.0-py3-none-any.whl (42kB)\n", | |
"\u001b[K |████████████████████████████████| 51kB 6.1MB/s \n", | |
"\u001b[?25hRequirement already satisfied: packaging>=14.3 in /usr/local/lib/python3.7/dist-packages (from google-cloud-bigquery-reservation) (20.9)\n", | |
"Requirement already satisfied: google-auth<2.0dev,>=1.21.1 in /usr/local/lib/python3.7/dist-packages (from google-api-core[grpc]<2.0.0dev,>=1.26.0->google-cloud-bigquery-reservation) (1.32.1)\n", | |
"Requirement already satisfied: setuptools>=40.3.0 in /usr/local/lib/python3.7/dist-packages (from google-api-core[grpc]<2.0.0dev,>=1.26.0->google-cloud-bigquery-reservation) (57.0.0)\n", | |
"Requirement already satisfied: pytz in /usr/local/lib/python3.7/dist-packages (from google-api-core[grpc]<2.0.0dev,>=1.26.0->google-cloud-bigquery-reservation) (2018.9)\n", | |
"Requirement already satisfied: protobuf>=3.12.0 in /usr/local/lib/python3.7/dist-packages (from google-api-core[grpc]<2.0.0dev,>=1.26.0->google-cloud-bigquery-reservation) (3.17.3)\n", | |
"Requirement already satisfied: requests<3.0.0dev,>=2.18.0 in /usr/local/lib/python3.7/dist-packages (from google-api-core[grpc]<2.0.0dev,>=1.26.0->google-cloud-bigquery-reservation) (2.23.0)\n", | |
"Requirement already satisfied: googleapis-common-protos<2.0dev,>=1.6.0 in /usr/local/lib/python3.7/dist-packages (from google-api-core[grpc]<2.0.0dev,>=1.26.0->google-cloud-bigquery-reservation) (1.53.0)\n", | |
"Requirement already satisfied: six>=1.13.0 in /usr/local/lib/python3.7/dist-packages (from google-api-core[grpc]<2.0.0dev,>=1.26.0->google-cloud-bigquery-reservation) (1.15.0)\n", | |
"Requirement already satisfied: grpcio<2.0dev,>=1.29.0; extra == \"grpc\" in /usr/local/lib/python3.7/dist-packages (from google-api-core[grpc]<2.0.0dev,>=1.26.0->google-cloud-bigquery-reservation) (1.34.1)\n", | |
"Requirement already satisfied: pyparsing>=2.0.2 in /usr/local/lib/python3.7/dist-packages (from packaging>=14.3->google-cloud-bigquery-reservation) (2.4.7)\n", | |
"Requirement already satisfied: cachetools<5.0,>=2.0.0 in /usr/local/lib/python3.7/dist-packages (from google-auth<2.0dev,>=1.21.1->google-api-core[grpc]<2.0.0dev,>=1.26.0->google-cloud-bigquery-reservation) (4.2.2)\n", | |
"Requirement already satisfied: rsa<5,>=3.1.4; python_version >= \"3.6\" in /usr/local/lib/python3.7/dist-packages (from google-auth<2.0dev,>=1.21.1->google-api-core[grpc]<2.0.0dev,>=1.26.0->google-cloud-bigquery-reservation) (4.7.2)\n", | |
"Requirement already satisfied: pyasn1-modules>=0.2.1 in /usr/local/lib/python3.7/dist-packages (from google-auth<2.0dev,>=1.21.1->google-api-core[grpc]<2.0.0dev,>=1.26.0->google-cloud-bigquery-reservation) (0.2.8)\n", | |
"Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.7/dist-packages (from requests<3.0.0dev,>=2.18.0->google-api-core[grpc]<2.0.0dev,>=1.26.0->google-cloud-bigquery-reservation) (2.10)\n", | |
"Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.7/dist-packages (from requests<3.0.0dev,>=2.18.0->google-api-core[grpc]<2.0.0dev,>=1.26.0->google-cloud-bigquery-reservation) (1.24.3)\n", | |
"Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.7/dist-packages (from requests<3.0.0dev,>=2.18.0->google-api-core[grpc]<2.0.0dev,>=1.26.0->google-cloud-bigquery-reservation) (3.0.4)\n", | |
"Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/dist-packages (from requests<3.0.0dev,>=2.18.0->google-api-core[grpc]<2.0.0dev,>=1.26.0->google-cloud-bigquery-reservation) (2021.5.30)\n", | |
"Requirement already satisfied: pyasn1>=0.1.3 in /usr/local/lib/python3.7/dist-packages (from rsa<5,>=3.1.4; python_version >= \"3.6\"->google-auth<2.0dev,>=1.21.1->google-api-core[grpc]<2.0.0dev,>=1.26.0->google-cloud-bigquery-reservation) (0.4.8)\n", | |
"Installing collected packages: proto-plus, google-cloud-bigquery-reservation\n", | |
"Successfully installed google-cloud-bigquery-reservation-1.2.0 proto-plus-1.19.0\n" | |
], | |
"name": "stdout" | |
}, | |
{ | |
"output_type": "display_data", | |
"data": { | |
"application/vnd.colab-display-data+json": { | |
"pip_warning": { | |
"packages": [ | |
"google" | |
] | |
} | |
} | |
}, | |
"metadata": { | |
"tags": [] | |
} | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "zVKEXvjATnrG" | |
}, | |
"source": [ | |
"from google.colab import auth\n", | |
"auth.authenticate_user()\n", | |
"\n", | |
"from google.cloud import bigquery_reservation_v1\n" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "S_2r-ScKbSj6" | |
}, | |
"source": [ | |
"Make sure you already have a slot commitment\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "FH4eLYFpYvq8" | |
}, | |
"source": [ | |
"client=bigquery_reservation_v1.ReservationServiceClient()\n", | |
"project_id = 'leigha-bq-dev'\n", | |
"location = 'US'\n", | |
"req = bigquery_reservation_v1.ListCapacityCommitmentsRequest(\n", | |
" parent=client.common_location_path(project_id, location))" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "jzkc8mJAZs3M", | |
"outputId": "0d4620d4-8457-4860-a1a7-9b629a9840e8" | |
}, | |
"source": [ | |
"client.list_capacity_commitments(request=req)" | |
], | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"ListCapacityCommitmentsPager<capacity_commitments {\n", | |
" name: \"projects/leigha-bq-dev/locations/US/capacityCommitments/4545017615014388977\"\n", | |
" slot_count: 1000\n", | |
" plan: FLEX\n", | |
" state: ACTIVE\n", | |
" commitment_end_time {\n", | |
" seconds: 1626121437\n", | |
" nanos: 344424000\n", | |
" }\n", | |
" failure_status {\n", | |
" }\n", | |
"}\n", | |
">" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 36 | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "HqLYa8ZrbXZS" | |
}, | |
"source": [ | |
"Create a new reservation for 1000 slots" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "3QbRLRusaP3Q", | |
"outputId": "a96a8c04-1bc7-469a-f48c-19f516ae8e0f" | |
}, | |
"source": [ | |
"req =bigquery_reservation_v1.CreateReservationRequest(\n", | |
" parent=client.common_location_path(project_id, location),\n", | |
" reservation_id='load-dashboard',\n", | |
" reservation= bigquery_reservation_v1.Reservation(\n", | |
" slot_capacity=1000,\n", | |
" ignore_idle_slots=False\n", | |
" )\n", | |
" )\n", | |
"\n", | |
"client.create_reservation(request=req)" | |
], | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"name: \"projects/leigha-bq-dev/locations/US/reservations/load-dashboard\"\n", | |
"slot_capacity: 1000" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 65 | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "JjqIVOGEfPHQ" | |
}, | |
"source": [ | |
"Create an assignment for the project" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "YrYgqX9TcBAp", | |
"outputId": "30bb1655-bc12-46cd-ec6a-2363da4f401c" | |
}, | |
"source": [ | |
"req=bigquery_reservation_v1.CreateAssignmentRequest(\n", | |
" parent=client.reservation_path(project_id, location,'load-dashboard'),\n", | |
" assignment=bigquery_reservation_v1.Assignment(\n", | |
" assignee='projects/'+project_id,\n", | |
" job_type=bigquery_reservation_v1.Assignment.JobType(2)\n", | |
" )\n", | |
")\n", | |
"\n", | |
"client.create_assignment(request=req)" | |
], | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"name: \"projects/leigha-bq-dev/locations/US/reservations/load-dashboard/assignments/16022880806055224029\"\n", | |
"job_type: QUERY\n", | |
"assignee: \"projects/leigha-bq-dev\"\n", | |
"state: ACTIVE" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 74 | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "6Sc6YBYQfVZz" | |
}, | |
"source": [ | |
"Delete assignment" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "7StEP2o8ckXy" | |
}, | |
"source": [ | |
"req=bigquery_reservation_v1.DeleteAssignmentRequest(\n", | |
" name=client.assignment_path(project_id,location,\n", | |
" 'load-dashboard','16022880806055224029'))\n", | |
"\n", | |
"client.delete_assignment(request=req)" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "emukkos9gS-W" | |
}, | |
"source": [ | |
"Make sure assignment was successfully deleted" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "H-wc7Zygfop6", | |
"outputId": "7d074e99-684e-4132-a32c-ffd658d2711d" | |
}, | |
"source": [ | |
"req=bigquery_reservation_v1.ListAssignmentsRequest(\n", | |
" parent=client.reservation_path(project_id, location,'load-dashboard'))\n", | |
"client.list_assignments(request=req)" | |
], | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"ListAssignmentsPager<>" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 79 | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "jSmaOeSDgXts" | |
}, | |
"source": [ | |
"Delete the reservation" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "A0yk5-G8gU5f" | |
}, | |
"source": [ | |
"req=bigquery_reservation_v1.DeleteReservationRequest(\n", | |
" name=client.reservation_path(project_id, location,'load-dashboard')\n", | |
")\n", | |
"\n", | |
"client.delete_reservation(request=req)" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "ZulvDuJOgYg1" | |
}, | |
"source": [ | |
"Make sure the reservation was successfully deleted" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "MVK9JOH4fpAi", | |
"outputId": "ecb8f949-0807-4dae-e32e-8096395bf1c1" | |
}, | |
"source": [ | |
"req=bigquery_reservation_v1.ListReservationsRequest(\n", | |
" parent=client.common_location_path(project_id, location)\n", | |
")\n", | |
"\n", | |
"client.list_reservations(request=req)" | |
], | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"ListReservationsPager<reservations {\n", | |
" name: \"projects/leigha-bq-dev/locations/US/reservations/default\"\n", | |
"}\n", | |
">" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 82 | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "wydywHYOgbLe" | |
}, | |
"source": [ | |
"(Optional) Delete the slot commitment" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "kfgiAZILgd7w" | |
}, | |
"source": [ | |
"req=bigquery_reservation_v1.DeleteCapacityCommitmentRequest(\n", | |
" name=\"projects/leigha-bq-dev/locations/US/capacityCommitments/4545017615014388977\"\n", | |
")\n", | |
"\n", | |
"client.delete_capacity_commitment(request=req)" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "opVnjEDOhSV3" | |
}, | |
"source": [ | |
"" | |
], | |
"execution_count": null, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment