|
| 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_upgrade_manifest_biglake_table] |
| 16 | +import sys |
| 17 | + |
| 18 | +from google.api_core import exceptions |
| 19 | +from google.cloud import bigquery |
| 20 | + |
| 21 | + |
| 22 | +def upgrade_manifest_biglake_table( |
| 23 | + project_id: str, |
| 24 | + dataset_id: str, |
| 25 | + table_id: str, |
| 26 | + connection_id: str, |
| 27 | + manifest_uri: str, |
| 28 | +) -> None: |
| 29 | + """Upgrades a manifest external table to a BigLake table with metadata caching. |
| 30 | +
|
| 31 | + Args: |
| 32 | + project_id: Google Cloud project ID (for example, 'example-project'). |
| 33 | + dataset_id: BigQuery dataset ID containing the external table |
| 34 | + (for example, 'example_dataset'). |
| 35 | + table_id: BigQuery external table ID to upgrade |
| 36 | + (for example, 'example_manifest_table'). |
| 37 | + connection_id: Fully qualified BigQuery Cloud Resource connection ID |
| 38 | + (for example, 'example-project.us.example_biglake_conn'). |
| 39 | + manifest_uri: Cloud Storage URI of the SymLinkTextInputFormat manifest file |
| 40 | + (for example, 'gs://example-bucket/manifests/symlink.manifest'). |
| 41 | + """ |
| 42 | + client = bigquery.Client(project=project_id) |
| 43 | + table_ref = f"{project_id}.{dataset_id}.{table_id}" |
| 44 | + # Upgrading the manifest external table with a Cloud Resource connection and AUTOMATIC metadata |
| 45 | + # caching accelerates open-table-format reads and enforces BigLake governance. |
| 46 | + sql = f""" |
| 47 | + CREATE OR REPLACE EXTERNAL TABLE `{table_ref}` |
| 48 | + WITH CONNECTION `{connection_id}` |
| 49 | + OPTIONS ( |
| 50 | + format = 'PARQUET', |
| 51 | + uris = ['{manifest_uri}'], |
| 52 | + file_set_spec_type = 'NEW_LINE_DELIMITED_MANIFEST', |
| 53 | + max_staleness = INTERVAL 1 HOUR, |
| 54 | + metadata_cache_mode = 'AUTOMATIC' |
| 55 | + ); |
| 56 | + SELECT |
| 57 | + id, |
| 58 | + label, |
| 59 | + score |
| 60 | + FROM `{table_ref}` |
| 61 | + ORDER BY id ASC |
| 62 | + LIMIT 10; |
| 63 | + """ |
| 64 | + |
| 65 | + try: |
| 66 | + print( |
| 67 | + f"Upgrading manifest external table to BigLake with metadata caching: {table_ref}" |
| 68 | + ) |
| 69 | + query_job = client.query(sql) |
| 70 | + rows = list(query_job.result()) |
| 71 | + except exceptions.NotFound as err: |
| 72 | + print( |
| 73 | + f"error: manifest external table '{table_id}' or connection '{connection_id}' " |
| 74 | + f"not found in dataset '{dataset_id}' for project '{project_id}' " |
| 75 | + f"(manifest '{manifest_uri}'): {err.message}", |
| 76 | + file=sys.stderr, |
| 77 | + ) |
| 78 | + return |
| 79 | + except exceptions.GoogleAPICallError as err: |
| 80 | + print( |
| 81 | + f"error: failed to upgrade manifest external table '{table_id}' in dataset " |
| 82 | + f"'{dataset_id}' for project '{project_id}' (connection '{connection_id}', " |
| 83 | + f"manifest '{manifest_uri}'): {err.message}", |
| 84 | + file=sys.stderr, |
| 85 | + ) |
| 86 | + return |
| 87 | + |
| 88 | + print(f"Resource ID: {table_ref}") |
| 89 | + print(f"Connection ID: {connection_id}") |
| 90 | + print(f"Job ID: {query_job.job_id}") |
| 91 | + print(f"Manifest URI: {manifest_uri}") |
| 92 | + print("Metadata Cache Mode: AUTOMATIC") |
| 93 | + print(f"Rows Returned: {len(rows)}") |
| 94 | + |
| 95 | + |
| 96 | +# [END bigquery_upgrade_manifest_biglake_table] |
0 commit comments