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

Commite760d1b

Browse files
authored
docs(samples): add table snapshot sample (#1274)
* docs(samples): add table snapshot sample* docs(samples): fix region tag
1 parent728b07c commite760d1b

File tree

3 files changed

+90
-1
lines changed

3 files changed

+90
-1
lines changed

‎samples/snippets/conftest.py‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
importpytest
1919
importtest_utils.prefixer
2020

21-
2221
prefixer=test_utils.prefixer.Prefixer("python-bigquery","samples/snippets")
2322

2423

@@ -52,6 +51,20 @@ def dataset_id(bigquery_client: bigquery.Client, project_id: str) -> Iterator[st
5251
bigquery_client.delete_dataset(dataset,delete_contents=True,not_found_ok=True)
5352

5453

54+
@pytest.fixture
55+
deftable_id(
56+
bigquery_client:bigquery.Client,project_id:str,dataset_id:str
57+
)->Iterator[str]:
58+
table_id=prefixer.create_prefix()
59+
full_table_id=f"{project_id}.{dataset_id}.{table_id}"
60+
table=bigquery.Table(
61+
full_table_id,schema=[bigquery.SchemaField("string_col","STRING")]
62+
)
63+
bigquery_client.create_table(table)
64+
yieldfull_table_id
65+
bigquery_client.delete_table(table,not_found_ok=True)
66+
67+
5568
@pytest.fixture(scope="session")
5669
defentity_id(bigquery_client:bigquery.Client,dataset_id:str)->str:
5770
return"cloud-developer-relations@google.com"
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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_snapshot(source_table_id:str,snapshot_table_id:str)->None:
17+
original_source_table_id=source_table_id
18+
original_snapshot_table_id=snapshot_table_id
19+
# [START bigquery_create_table_snapshot]
20+
fromgoogle.cloudimportbigquery
21+
22+
# TODO(developer): Set table_id to the ID of the table to create.
23+
source_table_id="your-project.your_dataset.your_table_name"
24+
snapshot_table_id="your-project.your_dataset.snapshot_table_name"
25+
# [END bigquery_create_table_snapshot]
26+
source_table_id=original_source_table_id
27+
snapshot_table_id=original_snapshot_table_id
28+
# [START bigquery_create_table_snapshot]
29+
30+
# Construct a BigQuery client object.
31+
client=bigquery.Client()
32+
copy_config=bigquery.CopyJobConfig()
33+
copy_config.operation_type=bigquery.OperationType.SNAPSHOT
34+
35+
copy_job=client.copy_table(
36+
sources=source_table_id,
37+
destination=snapshot_table_id,
38+
job_config=copy_config,
39+
)
40+
copy_job.result()
41+
42+
print("Created table snapshot {}".format(snapshot_table_id))
43+
# [END bigquery_create_table_snapshot]
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
importcreate_table_snapshot
18+
19+
iftyping.TYPE_CHECKING:
20+
importpytest
21+
22+
23+
deftest_create_table_snapshot(
24+
capsys:"pytest.CaptureFixture[str]",
25+
table_id:str,
26+
random_table_id:str,
27+
)->None:
28+
29+
create_table_snapshot.create_table_snapshot(table_id,random_table_id)
30+
31+
out,_=capsys.readouterr()
32+
33+
assert"Created table snapshot {}".format(random_table_id)inout

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp