Skip to content

Commit f0ace2a

Browse files
aribrayparthea
andauthored
docs: adds snippet for creating table with external data config (#1420)
* docs: add samples for creating table with external data configuration and creating an external table definition Co-authored-by: Anthonios Partheniou <[email protected]>
1 parent 0dab7d2 commit f0ace2a

File tree

4 files changed

+124
-1
lines changed

4 files changed

+124
-1
lines changed

docs/usage/tables.rst

+9
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@ Create an empty table with the
5858
:start-after: [START bigquery_create_table]
5959
:end-before: [END bigquery_create_table]
6060

61+
Create a table using an external data source with the
62+
:func:`~google.cloud.bigquery.client.Client.create_table` method:
63+
64+
.. literalinclude:: ../samples/create_table_external_data_configuration.py
65+
:language: python
66+
:dedent: 4
67+
:start-after: [START bigquery_create_table_external_data_configuration]
68+
:end-before: [END bigquery_create_table_external_data_configuration]
69+
6170
Create a clustered table with the
6271
:func:`~google.cloud.bigquery.client.Client.create_table` method:
6372

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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]

samples/tests/conftest.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414

1515
import datetime
16-
from typing import Iterator
16+
from typing import Iterator, List
1717
import uuid
1818

1919
import google.auth
@@ -47,6 +47,22 @@ def random_table_id(dataset_id: str) -> str:
4747
return "{}.{}".format(dataset_id, random_table_id)
4848

4949

50+
@pytest.fixture
51+
def avro_source_uris() -> List[str]:
52+
avro_source_uris = [
53+
"gs://cloud-samples-data/bigquery/federated-formats-reference-file-schema/a-twitter.avro",
54+
"gs://cloud-samples-data/bigquery/federated-formats-reference-file-schema/b-twitter.avro",
55+
"gs://cloud-samples-data/bigquery/federated-formats-reference-file-schema/c-twitter.avro",
56+
]
57+
return avro_source_uris
58+
59+
60+
@pytest.fixture
61+
def reference_file_schema_uri() -> str:
62+
reference_file_schema_uri = "gs://cloud-samples-data/bigquery/federated-formats-reference-file-schema/b-twitter.avro"
63+
return reference_file_schema_uri
64+
65+
5066
@pytest.fixture
5167
def random_dataset_id(client: bigquery.Client) -> Iterator[str]:
5268
now = datetime.datetime.now()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
import typing
16+
17+
from .. import create_table_external_data_configuration
18+
19+
if typing.TYPE_CHECKING:
20+
import pytest
21+
22+
23+
def test_create_table_external_data_configuration(
24+
capsys: "pytest.CaptureFixture[str]",
25+
random_table_id: str,
26+
) -> None:
27+
28+
create_table_external_data_configuration.create_table_external_data_configuration(
29+
random_table_id
30+
)
31+
out, _ = capsys.readouterr()
32+
assert "Created table with external source format AVRO" in out

0 commit comments

Comments
 (0)