|
| 1 | +# Copyright 2020 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | + |
| 16 | +def create_view(override_values={}): |
| 17 | +# [START bigquery_create_view] |
| 18 | +from google.cloud import bigquery |
| 19 | + |
| 20 | +client = bigquery.Client() |
| 21 | + |
| 22 | +view_id = "my-project.my_dataset.my_view" |
| 23 | +source_id = "my-project.my_dataset.my_table" |
| 24 | +# [END bigquery_create_view] |
| 25 | +# To facilitate testing, we replace values with alternatives |
| 26 | +# provided by the testing harness. |
| 27 | +view_id = override_values.get("view_id", view_id) |
| 28 | +source_id = override_values.get("source_id", source_id) |
| 29 | +# [START bigquery_create_view] |
| 30 | +view = bigquery.Table(view_id) |
| 31 | + |
| 32 | +# The source table in this example is created from a CSV file in Google |
| 33 | +# Cloud Storage located at |
| 34 | +# `gs://cloud-samples-data/bigquery/us-states/us-states.csv`. It contains |
| 35 | +# 50 US states, while the view returns only those states with names |
| 36 | +# starting with the letter 'W'. |
| 37 | +view.view_query = f"SELECT name, post_abbr FROM `{source_id}` WHERE name LIKE 'W%'" |
| 38 | + |
| 39 | +# Make an API request to create the view. |
| 40 | +view = client.create_table(view) |
| 41 | +print(f"Created {view.table_type}: {str(view.reference)}") |
| 42 | +# [END bigquery_create_view] |
| 43 | +return view |
| 44 | + |
| 45 | + |
| 46 | +def get_view(override_values={}): |
| 47 | +# [START bigquery_get_view] |
| 48 | +from google.cloud import bigquery |
| 49 | + |
| 50 | +client = bigquery.Client() |
| 51 | + |
| 52 | +view_id = "my-project.my_dataset.my_view" |
| 53 | +# [END bigquery_get_view] |
| 54 | +# To facilitate testing, we replace values with alternatives |
| 55 | +# provided by the testing harness. |
| 56 | +view_id = override_values.get("view_id", view_id) |
| 57 | +# [START bigquery_get_view] |
| 58 | +# Make an API request to get the table resource. |
| 59 | +view = client.get_table(view_id) |
| 60 | + |
| 61 | +# Display view properties |
| 62 | +print(f"Retrieved {view.table_type}: {str(view.reference)}") |
| 63 | +print(f"View Query:\n{view.view_query}") |
| 64 | +# [END bigquery_get_view] |
| 65 | +return view |
| 66 | + |
| 67 | + |
| 68 | +def update_view(override_values={}): |
| 69 | +# [START bigquery_update_view_query] |
| 70 | +from google.cloud import bigquery |
| 71 | + |
| 72 | +client = bigquery.Client() |
| 73 | + |
| 74 | +view_id = "my-project.my_dataset.my_view" |
| 75 | +source_id = "my-project.my_dataset.my_table" |
| 76 | +# [END bigquery_update_view_query] |
| 77 | +# To facilitate testing, we replace values with alternatives |
| 78 | +# provided by the testing harness. |
| 79 | +view_id = override_values.get("view_id", view_id) |
| 80 | +source_id = override_values.get("source_id", source_id) |
| 81 | +# [START bigquery_update_view_query] |
| 82 | +view = bigquery.Table(view_id) |
| 83 | + |
| 84 | +# The source table in this example is created from a CSV file in Google |
| 85 | +# Cloud Storage located at |
| 86 | +# `gs://cloud-samples-data/bigquery/us-states/us-states.csv`. It contains |
| 87 | +# 50 US states, while the view returns only those states with names |
| 88 | +# starting with the letter 'M'. |
| 89 | +view.view_query = f"SELECT name, post_abbr FROM `{source_id}` WHERE name LIKE 'M%'" |
| 90 | + |
| 91 | +# Make an API request to update the query property of the view. |
| 92 | +view = client.update_table(view, ["view_query"]) |
| 93 | +print(f"Updated {view.table_type}: {str(view.reference)}") |
| 94 | +# [END bigquery_update_view_query] |
| 95 | +return view |
| 96 | + |
| 97 | + |
| 98 | +def grant_access(override_values={}): |
| 99 | +# [START bigquery_grant_view_access] |
| 100 | +from google.cloud import bigquery |
| 101 | + |
| 102 | +client = bigquery.Client() |
| 103 | + |
| 104 | +# To use a view, the analyst requires ACLs to both the view and the source |
| 105 | +# table. Create an authorized view to allow an analyst to use a view |
| 106 | +# without direct access permissions to the source table. |
| 107 | +view_dataset_id = "my-project.my_view_dataset" |
| 108 | +# [END bigquery_grant_view_access] |
| 109 | +# To facilitate testing, we replace values with alternatives |
| 110 | +# provided by the testing harness. |
| 111 | +view_dataset_id = override_values.get("view_dataset_id", view_dataset_id) |
| 112 | +# [START bigquery_grant_view_access] |
| 113 | +# Make an API request to get the view dataset ACLs. |
| 114 | +view_dataset = client.get_dataset(view_dataset_id) |
| 115 | + |
| 116 | +analyst_group_email = "[email protected]" |
| 117 | +# [END bigquery_grant_view_access] |
| 118 | +# To facilitate testing, we replace values with alternatives |
| 119 | +# provided by the testing harness. |
| 120 | +analyst_group_email = override_values.get( |
| 121 | +"analyst_group_email", analyst_group_email |
| 122 | +) |
| 123 | +# [START bigquery_grant_view_access] |
| 124 | +access_entries = view_dataset.access_entries |
| 125 | +access_entries.append( |
| 126 | +bigquery.AccessEntry("READER", "groupByEmail", analyst_group_email) |
| 127 | +) |
| 128 | +view_dataset.access_entries = access_entries |
| 129 | + |
| 130 | +# Make an API request to update the ACLs property of the view dataset. |
| 131 | +view_dataset = client.update_dataset(view_dataset, ["access_entries"]) |
| 132 | +print(f"Access to view: {view_dataset.access_entries}") |
| 133 | + |
| 134 | +# Group members of "[email protected]" now have access to the view, |
| 135 | +# but they require access to the source table to use it. To remove this |
| 136 | +# restriction, authorize the view to access the source dataset. |
| 137 | +source_dataset_id = "my-project.my_source_dataset" |
| 138 | +# [END bigquery_grant_view_access] |
| 139 | +# To facilitate testing, we replace values with alternatives |
| 140 | +# provided by the testing harness. |
| 141 | +source_dataset_id = override_values.get("source_dataset_id", source_dataset_id) |
| 142 | +# [START bigquery_grant_view_access] |
| 143 | +# Make an API request to set the source dataset ACLs. |
| 144 | +source_dataset = client.get_dataset(source_dataset_id) |
| 145 | + |
| 146 | +view_reference = { |
| 147 | +"projectId": "my-project", |
| 148 | +"datasetId": "my_view_dataset", |
| 149 | +"tableId": "my_authorized_view", |
| 150 | +} |
| 151 | +# [END bigquery_grant_view_access] |
| 152 | +# To facilitate testing, we replace values with alternatives |
| 153 | +# provided by the testing harness. |
| 154 | +view_reference = override_values.get("view_reference", view_reference) |
| 155 | +# [START bigquery_grant_view_access] |
| 156 | +access_entries = source_dataset.access_entries |
| 157 | +access_entries.append(bigquery.AccessEntry(None, "view", view_reference)) |
| 158 | +source_dataset.access_entries = access_entries |
| 159 | + |
| 160 | +# Make an API request to update the ACLs property of the source dataset. |
| 161 | +source_dataset = client.update_dataset(source_dataset, ["access_entries"]) |
| 162 | +print(f"Access to source: {source_dataset.access_entries}") |
| 163 | +# [END bigquery_grant_view_access] |
| 164 | +return view_dataset, source_dataset |
0 commit comments