Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: adds snippet for creating table with external data config #1420

Merged
merged 26 commits into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
0084ce7
docs: add sample and test for creating table with external data confi…
aribray Nov 21, 2022
3a326f8
add type annotation for arg
aribray Nov 21, 2022
01aa07f
add 'create_table_external_data_configuration' region tag and snippet…
aribray Nov 22, 2022
0ff2e49
add nested region tag for 'bigquery_create_external_table_definition'
aribray Nov 22, 2022
5bfd10e
Merge branch 'main' into aribray--external-table-docs
aribray Dec 12, 2022
a517b86
fix comment style
aribray Dec 13, 2022
bb444db
Merge branch 'main' of github.com:googleapis/python-bigquery into ari…
aribray Dec 13, 2022
5e18940
Merge branch 'aribray--external-table-docs' of github.com:googleapis/…
aribray Dec 13, 2022
3318d10
fix comment style
aribray Dec 14, 2022
de42b4d
Merge branch 'main' of github.com:aribray/python-bigquery into aribra…
aribray Dec 14, 2022
3473fd2
Merge branch 'main' of github.com:aribray/python-bigquery into aribra…
aribray Dec 15, 2022
3f28647
change sample test fixture
aribray Dec 15, 2022
6363ce8
fix region tag
aribray Dec 15, 2022
f842139
Merge branch 'main' into aribray--external-table-docs
aribray Jan 3, 2023
014112e
Merge branch 'main' of github.com:aribray/python-bigquery into aribra…
aribray Jan 3, 2023
6ecfffb
fix linting
aribray Jan 3, 2023
e0b3e12
Merge branch 'main' into aribray--external-table-docs
aribray Jan 17, 2023
839b190
Merge branch 'main' of github.com:aribray/python-bigquery into aribra…
aribray Jan 17, 2023
f6b91e0
Merge branch 'main' into aribray--external-table-docs
aribray Jan 18, 2023
b0413fa
Merge branch 'main' of github.com:aribray/python-bigquery into aribra…
aribray Jan 18, 2023
fc80be0
Merge branch 'main' into aribray--external-table-docs
aribray Jan 18, 2023
d89def6
Update create_table_external_data_configuration.py
aribray Jan 18, 2023
c0b3b1e
Merge branch 'main' into aribray--external-table-docs
parthea Jan 18, 2023
a736086
Merge branch 'main' into aribray--external-table-docs
parthea Jan 18, 2023
9df83de
remove comments
aribray Jan 18, 2023
420a4d9
Merge branch 'aribray--external-table-docs' of github.com:googleapis/…
aribray Jan 18, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/usage/tables.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ Create an empty table with the
:start-after: [START bigquery_create_table]
:end-before: [END bigquery_create_table]

Create a table using an external data source with the
:func:`~google.cloud.bigquery.client.Client.create_table` method:

.. literalinclude:: ../samples/create_table_external_data_configuration.py
:language: python
:dedent: 4
:start-after: [START bigquery_create_table_external_data_configuration]
:end-before: [END bigquery_create_table_external_data_configuration]

Create a clustered table with the
:func:`~google.cloud.bigquery.client.Client.create_table` method:

Expand Down
66 changes: 66 additions & 0 deletions samples/create_table_external_data_configuration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# 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.


def create_table_external_data_configuration(
table_id: str,
) -> None:
"""Create a table using an external data source"""
orig_table_id = table_id
# [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"
aribray marked this conversation as resolved.
Show resolved Hide resolved
# [END bigquery_create_table_external_data_configuration]
table_id = orig_table_id
# [START bigquery_create_table_external_data_configuration]

# TODO(developer): 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 = "AVRO"

# TODO(developer): Set the source_uris to point to your data in Google Cloud
source_uris = [
"gs://cloud-samples-data/bigquery/federated-formats-reference-file-schema/a-twitter.avro",
"gs://cloud-samples-data/bigquery/federated-formats-reference-file-schema/b-twitter.avro",
"gs://cloud-samples-data/bigquery/federated-formats-reference-file-schema/c-twitter.avro",
]

# 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

# TODO(developer) You have the option to set a reference_file_schema_uri, which points to
# a reference file for the table schema
reference_file_schema_uri = "gs://cloud-samples-data/bigquery/federated-formats-reference-file-schema/b-twitter.avro"

external_config.reference_file_schema_uri = 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]
18 changes: 17 additions & 1 deletion samples/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

import datetime
from typing import Iterator
from typing import Iterator, List
import uuid

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


@pytest.fixture
def avro_source_uris() -> List[str]:
avro_source_uris = [
"gs://cloud-samples-data/bigquery/federated-formats-reference-file-schema/a-twitter.avro",
"gs://cloud-samples-data/bigquery/federated-formats-reference-file-schema/b-twitter.avro",
"gs://cloud-samples-data/bigquery/federated-formats-reference-file-schema/c-twitter.avro",
]
return avro_source_uris


@pytest.fixture
def reference_file_schema_uri() -> str:
reference_file_schema_uri = "gs://cloud-samples-data/bigquery/federated-formats-reference-file-schema/b-twitter.avro"
return reference_file_schema_uri


@pytest.fixture
def random_dataset_id(client: bigquery.Client) -> Iterator[str]:
now = datetime.datetime.now()
Expand Down
32 changes: 32 additions & 0 deletions samples/tests/test_create_table_external_data_configuration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# 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.

import typing

from .. import create_table_external_data_configuration

if typing.TYPE_CHECKING:
import pytest


def test_create_table_external_data_configuration(
capsys: "pytest.CaptureFixture[str]",
random_table_id: str,
) -> None:

create_table_external_data_configuration.create_table_external_data_configuration(
random_table_id
)
out, _ = capsys.readouterr()
assert "Created table with external source format AVRO" in out