|
| 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_create_manifest_biglake_table] |
| 16 | +import sys |
| 17 | + |
| 18 | +from google.api_core import exceptions |
| 19 | +from google.cloud import bigquery |
| 20 | + |
| 21 | + |
| 22 | +def create_manifest_biglake_table( |
| 23 | + project_id: str, |
| 24 | + dataset_id: str, |
| 25 | + connection_id: str, |
| 26 | + manifest_uri: str, |
| 27 | +) -> None: |
| 28 | + """Creates a BigLake external table backed by a newline-delimited manifest file. |
| 29 | +
|
| 30 | + Args: |
| 31 | + project_id: Google Cloud project ID (for example, 'example-project'). |
| 32 | + dataset_id: BigQuery dataset ID where the BigLake table is created |
| 33 | + (for example, 'example_dataset'). |
| 34 | + connection_id: Fully qualified BigQuery Cloud Resource connection ID |
| 35 | + (for example, 'example-project.us.example_biglake_conn'). |
| 36 | + manifest_uri: Cloud Storage URI of the SymLinkTextInputFormat manifest file |
| 37 | + (for example, 'gs://example-bucket/manifests/symlink.manifest'). |
| 38 | + """ |
| 39 | + client = bigquery.Client(project=project_id) |
| 40 | + # Use a fixed table identifier inside the caller-supplied dataset. |
| 41 | + table_id = "example_manifest_biglake_table" |
| 42 | + table_ref = f"{project_id}.{dataset_id}.{table_id}" |
| 43 | + # Setting file_set_spec_type to NEW_LINE_DELIMITED_MANIFEST tells BigQuery to resolve the |
| 44 | + # Parquet data files listed inside the manifest URI via the BigLake connection. |
| 45 | + sql = f""" |
| 46 | + CREATE EXTERNAL TABLE `{table_ref}` |
| 47 | + WITH CONNECTION `{connection_id}` |
| 48 | + OPTIONS ( |
| 49 | + format = 'PARQUET', |
| 50 | + uris = ['{manifest_uri}'], |
| 51 | + file_set_spec_type = 'NEW_LINE_DELIMITED_MANIFEST' |
| 52 | + ); |
| 53 | + """ |
| 54 | + |
| 55 | + try: |
| 56 | + print(f"Creating manifest-backed BigLake table: {table_ref}") |
| 57 | + query_job = client.query(sql) |
| 58 | + query_job.result() |
| 59 | + except exceptions.AlreadyExists as err: |
| 60 | + print( |
| 61 | + f"error: BigLake table '{table_id}' already exists in dataset '{dataset_id}' " |
| 62 | + f"for project '{project_id}' (connection '{connection_id}', " |
| 63 | + f"manifest '{manifest_uri}'): {err.message}", |
| 64 | + file=sys.stderr, |
| 65 | + ) |
| 66 | + return |
| 67 | + except exceptions.GoogleAPICallError as err: |
| 68 | + print( |
| 69 | + f"error: failed to create BigLake table '{table_id}' in dataset '{dataset_id}' " |
| 70 | + f"for project '{project_id}' (connection '{connection_id}', " |
| 71 | + f"manifest '{manifest_uri}'): {err.message}", |
| 72 | + file=sys.stderr, |
| 73 | + ) |
| 74 | + return |
| 75 | + |
| 76 | + print(f"Resource ID: {table_ref}") |
| 77 | + print(f"Connection ID: {connection_id}") |
| 78 | + print("File Set Spec Type: NEW_LINE_DELIMITED_MANIFEST") |
| 79 | + print(f"Job ID: {query_job.job_id}") |
| 80 | + print(f"Manifest URI: {manifest_uri}") |
| 81 | + print(f"Statement Type: {query_job.statement_type}") |
| 82 | + |
| 83 | + |
| 84 | +# [END bigquery_create_manifest_biglake_table] |
0 commit comments