|
| 1 | +# Copyright 2026 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 | +# https://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 | +# [START bigquery_query_biglake_gcs_temp_table] |
| 16 | +import sys |
| 17 | + |
| 18 | +from google.api_core import exceptions |
| 19 | +from google.cloud import bigquery |
| 20 | + |
| 21 | + |
| 22 | +def query_biglake_gcs_temp_table( |
| 23 | + project_id: str, |
| 24 | + connection_id: str, |
| 25 | +) -> None: |
| 26 | + """Configures and queries a temporary BigLake table backed by Cloud Storage. |
| 27 | +
|
| 28 | + Args: |
| 29 | + project_id: Google Cloud project ID (for example, 'example-project'). |
| 30 | + connection_id: Fully qualified BigQuery Cloud Resource connection ID |
| 31 | + (for example, 'projects/example-project/locations/us/connections/example-conn'). |
| 32 | + """ |
| 33 | + client = bigquery.Client(project=project_id) |
| 34 | + temp_table_name = "example_us_states_temp" |
| 35 | + source_uri = "gs://cloud-samples-data/bigquery/us-states/us-states.csv" |
| 36 | + prefix_filter = "W%" |
| 37 | + |
| 38 | + # Bind the Cloud Resource connection and explicit schema to a temporary CSV ExternalConfig. |
| 39 | + external_config = bigquery.ExternalConfig("CSV") |
| 40 | + external_config.source_uris = [source_uri] |
| 41 | + external_config.connection_id = connection_id |
| 42 | + external_config.schema = [ |
| 43 | + bigquery.SchemaField("name", "STRING"), |
| 44 | + bigquery.SchemaField("post_abbr", "STRING"), |
| 45 | + ] |
| 46 | + external_config.csv_options.skip_leading_rows = 1 |
| 47 | + |
| 48 | + job_config = bigquery.QueryJobConfig( |
| 49 | + table_definitions={temp_table_name: external_config}, |
| 50 | + query_parameters=[ |
| 51 | + bigquery.ScalarQueryParameter("prefix_filter", "STRING", prefix_filter), |
| 52 | + ], |
| 53 | + ) |
| 54 | + sql = f""" |
| 55 | + SELECT |
| 56 | + name, |
| 57 | + post_abbr |
| 58 | + FROM `{temp_table_name}` |
| 59 | + WHERE post_abbr LIKE @prefix_filter |
| 60 | + ORDER BY post_abbr ASC |
| 61 | + LIMIT 10; |
| 62 | + """ |
| 63 | + |
| 64 | + try: |
| 65 | + print(f"Querying temporary BigLake table {temp_table_name} via {connection_id}") |
| 66 | + query_job = client.query(sql, job_config=job_config) |
| 67 | + rows = list(query_job.result()) |
| 68 | + except exceptions.NotFound as err: |
| 69 | + print( |
| 70 | + f"error: BigLake connection '{connection_id}' or URI '{source_uri}' " |
| 71 | + f"not found in project '{project_id}': {err.message}", |
| 72 | + file=sys.stderr, |
| 73 | + ) |
| 74 | + return |
| 75 | + except exceptions.GoogleAPICallError as err: |
| 76 | + print( |
| 77 | + f"error: failed to query temporary BigLake table via connection " |
| 78 | + f"'{connection_id}' in project '{project_id}': {err.message}", |
| 79 | + file=sys.stderr, |
| 80 | + ) |
| 81 | + return |
| 82 | + |
| 83 | + print(f"Resource ID: {connection_id}") |
| 84 | + print(f"Job ID: {query_job.job_id}") |
| 85 | + print(f"Rows Returned: {len(rows)}") |
| 86 | + print(f"Source URI: {source_uri}") |
| 87 | + |
| 88 | + |
| 89 | +# [END bigquery_query_biglake_gcs_temp_table] |
0 commit comments