Skip to content

Commit 40ba859

Browse files
docs: revised create_partitioned_table sample (#1447)
* docs: revised create_partitioned_table sample * update sample tests to use correct fixture --------- Co-authored-by: Tim Swast <[email protected]>
1 parent 53aad82 commit 40ba859

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

docs/snippets.py

+2
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ def test_create_partitioned_table(client, to_delete):
125125
dataset = client.create_dataset(dataset_ref)
126126
to_delete.append(dataset)
127127

128+
# TODO(tswast): remove this snippet once cloud.google.com is updated to use
129+
# samples/snippets/create_partitioned_table.py
128130
# [START bigquery_create_table_partitioned]
129131
# from google.cloud import bigquery
130132
# client = bigquery.Client()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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_partitioned_table(table_id):
17+
your_fully_qualified_table_id = table_id
18+
19+
# [START bigquery_create_table_partitioned]
20+
from google.cloud import bigquery
21+
22+
client = bigquery.Client()
23+
24+
# Use format "your-project.your_dataset.your_table_name" for table_id
25+
table_id = your_fully_qualified_table_id
26+
schema = [
27+
bigquery.SchemaField("name", "STRING"),
28+
bigquery.SchemaField("post_abbr", "STRING"),
29+
bigquery.SchemaField("date", "DATE"),
30+
]
31+
table = bigquery.Table(table_id, schema=schema)
32+
table.time_partitioning = bigquery.TimePartitioning(
33+
type_=bigquery.TimePartitioningType.DAY,
34+
field="date", # name of column to use for partitioning
35+
expiration_ms=1000 * 60 * 60 * 24 * 90,
36+
) # 90 days
37+
38+
table = client.create_table(table)
39+
40+
print(
41+
f"Created table {table.project}.{table.dataset_id}.{table.table_id}, "
42+
f"partitioned on column {table.time_partitioning.field}."
43+
)
44+
# [END bigquery_create_table_partitioned]
45+
return table
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
import create_partitioned_table
18+
19+
if typing.TYPE_CHECKING:
20+
import pytest
21+
22+
23+
def test_create_partitioned_table(
24+
capsys: "pytest.CaptureFixture[str]",
25+
random_table_id: str,
26+
) -> None:
27+
table = create_partitioned_table.create_partitioned_table(random_table_id)
28+
29+
out, _ = capsys.readouterr()
30+
assert "Created" in out
31+
assert random_table_id in out
32+
33+
assert table.time_partitioning.type_ == "DAY"
34+
assert table.time_partitioning.field == "date"

0 commit comments

Comments
 (0)