forked from googleapis/python-bigquery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_table_external_data_configuration.py
65 lines (52 loc) · 2.41 KB
/
create_table_external_data_configuration.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import List
def create_table_external_data_configuration(
table_id: str,
source_uris: List[str],
external_source_format: str,
) -> None:
"""Create a table using an external data source"""
# [START bigquery_create_table_external_data_configuration]
# [START bigquery_create_external_table_definition]
from google.cloud import bigquery
# Construct a BigQuery client object.
client = bigquery.Client()
"""
TODO(developer):
Set table_id to the ID of the table to create.
table_id = "your-project.your_dataset.your_table_name"
Set the external source format of your table. # Note that the
set of allowed values for external data sources is different than the set used
for loading data (see :class:`~google.cloud.bigquery.job.SourceFormat`).
external_source_format = "your-source-format"
Set the source_uris to point to your data in Google Cloud
source_uris = ["your", "source", "uris"]
"""
# Create ExternalConfig object with external source format
external_config = bigquery.ExternalConfig(external_source_format)
# Set source_uris that point to your data in Google Cloud
external_config.source_uris = source_uris
# You have the option to set a reference_file_schema_uri, which points to
# a reference file for the table schema
# external_config.reference_file_schema_uri = "path/to/your/reference/file/schema/uri"
# [END bigquery_create_external_table_definition]
table = bigquery.Table(table_id)
# Set the external data configuration of the table
table.external_data_configuration = external_config
table = client.create_table(table) # Make an API request.
print(
f"Created table with external source format {table.external_data_configuration.source_format}"
)
# [END bigquery_create_table_external_data_configuration]