|
| 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_external_bigtable] |
| 16 | +import sys |
| 17 | + |
| 18 | +from google.api_core import exceptions |
| 19 | +from google.cloud import bigquery |
| 20 | + |
| 21 | + |
| 22 | +def query_external_bigtable( |
| 23 | + project_id: str, |
| 24 | + bigtable_instance_id: str, |
| 25 | + bigtable_table_id: str, |
| 26 | +) -> None: |
| 27 | + """Configures and queries a temporary BigQuery external table backed by Cloud Bigtable. |
| 28 | +
|
| 29 | + Args: |
| 30 | + project_id: Google Cloud project ID (for example, 'example-project'). |
| 31 | + bigtable_instance_id: Cloud Bigtable instance ID hosting the source table |
| 32 | + (for example, 'example-bt-instance'). |
| 33 | + bigtable_table_id: Cloud Bigtable table ID containing the column family |
| 34 | + (for example, 'example_bt_table'). |
| 35 | + """ |
| 36 | + client = bigquery.Client(project=project_id) |
| 37 | + temp_table_name = "example_bigtable_temp" |
| 38 | + source_uri = ( |
| 39 | + f"https://googleapis.com/bigtable/projects/{project_id}" |
| 40 | + f"/instances/{bigtable_instance_id}/tables/{bigtable_table_id}" |
| 41 | + ) |
| 42 | + |
| 43 | + # Map the Cloud Bigtable column family into a temporary table definition for a single query job. |
| 44 | + external_config = bigquery.ExternalConfig("BIGTABLE") |
| 45 | + external_config.source_uris = [source_uri] |
| 46 | + external_config.bigtable_options.read_rowkey_as_string = True |
| 47 | + family = bigquery.BigtableColumnFamily() |
| 48 | + family.family_id = "stats_summary" |
| 49 | + family.type_ = "STRING" |
| 50 | + family.encoding = "TEXT" |
| 51 | + external_config.bigtable_options.column_families = [family] |
| 52 | + |
| 53 | + job_config = bigquery.QueryJobConfig( |
| 54 | + table_definitions={temp_table_name: external_config} |
| 55 | + ) |
| 56 | + sql = f""" |
| 57 | + SELECT |
| 58 | + rowkey, |
| 59 | + stats_summary |
| 60 | + FROM `{temp_table_name}` |
| 61 | + ORDER BY rowkey ASC |
| 62 | + LIMIT 10; |
| 63 | + """ |
| 64 | + |
| 65 | + try: |
| 66 | + print(f"Querying temporary Cloud Bigtable table mapped to {source_uri}") |
| 67 | + query_job = client.query(sql, job_config=job_config) |
| 68 | + rows = list(query_job.result()) |
| 69 | + except exceptions.NotFound as err: |
| 70 | + print( |
| 71 | + f"error: Cloud Bigtable table '{bigtable_table_id}' in instance " |
| 72 | + f"'{bigtable_instance_id}' not found for project '{project_id}': {err.message}", |
| 73 | + file=sys.stderr, |
| 74 | + ) |
| 75 | + return |
| 76 | + except exceptions.GoogleAPICallError as err: |
| 77 | + print( |
| 78 | + f"error: failed to query Cloud Bigtable table '{bigtable_table_id}' in " |
| 79 | + f"instance '{bigtable_instance_id}' for project '{project_id}': {err.message}", |
| 80 | + file=sys.stderr, |
| 81 | + ) |
| 82 | + return |
| 83 | + |
| 84 | + print(f"Resource ID: {source_uri}") |
| 85 | + print(f"Job ID: {query_job.job_id}") |
| 86 | + print(f"Rows Returned: {len(rows)}") |
| 87 | + print(f"Temporary Table: {temp_table_name}") |
| 88 | + |
| 89 | + |
| 90 | +# [END bigquery_query_external_bigtable] |
0 commit comments