|
| 1 | +# Copyright 2022 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 | + |
| 16 | +def create_table_external_data_configuration( |
| 17 | + table_id: str, |
| 18 | +) -> None: |
| 19 | + """Create a table using an external data source""" |
| 20 | + orig_table_id = table_id |
| 21 | + # [START bigquery_create_table_external_data_configuration] |
| 22 | + # [START bigquery_create_external_table_definition] |
| 23 | + from google.cloud import bigquery |
| 24 | + |
| 25 | + # Construct a BigQuery client object. |
| 26 | + client = bigquery.Client() |
| 27 | + |
| 28 | + # TODO(developer): Set table_id to the ID of the table to create. |
| 29 | + table_id = "your-project.your_dataset.your_table_name" |
| 30 | + # [END bigquery_create_table_external_data_configuration] |
| 31 | + table_id = orig_table_id |
| 32 | + # [START bigquery_create_table_external_data_configuration] |
| 33 | + |
| 34 | + # TODO(developer): Set the external source format of your table. |
| 35 | + # Note that the set of allowed values for external data sources is |
| 36 | + # different than the set used for loading data (see :class:`~google.cloud.bigquery.job.SourceFormat`). |
| 37 | + external_source_format = "AVRO" |
| 38 | + |
| 39 | + # TODO(developer): Set the source_uris to point to your data in Google Cloud |
| 40 | + source_uris = [ |
| 41 | + "gs://cloud-samples-data/bigquery/federated-formats-reference-file-schema/a-twitter.avro", |
| 42 | + "gs://cloud-samples-data/bigquery/federated-formats-reference-file-schema/b-twitter.avro", |
| 43 | + "gs://cloud-samples-data/bigquery/federated-formats-reference-file-schema/c-twitter.avro", |
| 44 | + ] |
| 45 | + |
| 46 | + # Create ExternalConfig object with external source format |
| 47 | + external_config = bigquery.ExternalConfig(external_source_format) |
| 48 | + # Set source_uris that point to your data in Google Cloud |
| 49 | + external_config.source_uris = source_uris |
| 50 | + |
| 51 | + # TODO(developer) You have the option to set a reference_file_schema_uri, which points to |
| 52 | + # a reference file for the table schema |
| 53 | + reference_file_schema_uri = "gs://cloud-samples-data/bigquery/federated-formats-reference-file-schema/b-twitter.avro" |
| 54 | + |
| 55 | + external_config.reference_file_schema_uri = reference_file_schema_uri |
| 56 | + # [END bigquery_create_external_table_definition] |
| 57 | + |
| 58 | + table = bigquery.Table(table_id) |
| 59 | + # Set the external data configuration of the table |
| 60 | + table.external_data_configuration = external_config |
| 61 | + table = client.create_table(table) # Make an API request. |
| 62 | + |
| 63 | + print( |
| 64 | + f"Created table with external source format {table.external_data_configuration.source_format}" |
| 65 | + ) |
| 66 | + # [END bigquery_create_table_external_data_configuration] |
0 commit comments