Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf0ace2a

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 definitionCo-authored-by: Anthonios Partheniou <partheniou@google.com>
1 parent0dab7d2 commitf0ace2a

File tree

4 files changed

+124
-1
lines changed

4 files changed

+124
-1
lines changed

‎docs/usage/tables.rst‎

Lines changed: 9 additions & 0 deletions
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

Lines changed: 66 additions & 0 deletions
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+
defcreate_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+
fromgoogle.cloudimportbigquery
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‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414

1515
importdatetime
16-
fromtypingimportIterator
16+
fromtypingimportIterator,List
1717
importuuid
1818

1919
importgoogle.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+
defavro_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+
returnavro_source_uris
58+
59+
60+
@pytest.fixture
61+
defreference_file_schema_uri()->str:
62+
reference_file_schema_uri="gs://cloud-samples-data/bigquery/federated-formats-reference-file-schema/b-twitter.avro"
63+
returnreference_file_schema_uri
64+
65+
5066
@pytest.fixture
5167
defrandom_dataset_id(client:bigquery.Client)->Iterator[str]:
5268
now=datetime.datetime.now()
Lines changed: 32 additions & 0 deletions
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+
importtyping
16+
17+
from ..importcreate_table_external_data_configuration
18+
19+
iftyping.TYPE_CHECKING:
20+
importpytest
21+
22+
23+
deftest_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"inout

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp