-
Notifications
You must be signed in to change notification settings - Fork 309
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <swast@google.com>
- Loading branch information
1 parent
53aad82
commit 40ba859
Showing
3 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# 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_partitioned_table(table_id): | ||
your_fully_qualified_table_id = table_id | ||
|
||
# [START bigquery_create_table_partitioned] | ||
from google.cloud import bigquery | ||
|
||
client = bigquery.Client() | ||
|
||
# Use format "your-project.your_dataset.your_table_name" for table_id | ||
table_id = your_fully_qualified_table_id | ||
schema = [ | ||
bigquery.SchemaField("name", "STRING"), | ||
bigquery.SchemaField("post_abbr", "STRING"), | ||
bigquery.SchemaField("date", "DATE"), | ||
] | ||
table = bigquery.Table(table_id, schema=schema) | ||
table.time_partitioning = bigquery.TimePartitioning( | ||
type_=bigquery.TimePartitioningType.DAY, | ||
field="date", # name of column to use for partitioning | ||
expiration_ms=1000 * 60 * 60 * 24 * 90, | ||
) # 90 days | ||
|
||
table = client.create_table(table) | ||
|
||
print( | ||
f"Created table {table.project}.{table.dataset_id}.{table.table_id}, " | ||
f"partitioned on column {table.time_partitioning.field}." | ||
) | ||
# [END bigquery_create_table_partitioned] | ||
return table |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# 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 | ||
|
||
import create_partitioned_table | ||
|
||
if typing.TYPE_CHECKING: | ||
import pytest | ||
|
||
|
||
def test_create_partitioned_table( | ||
capsys: "pytest.CaptureFixture[str]", | ||
random_table_id: str, | ||
) -> None: | ||
table = create_partitioned_table.create_partitioned_table(random_table_id) | ||
|
||
out, _ = capsys.readouterr() | ||
assert "Created" in out | ||
assert random_table_id in out | ||
|
||
assert table.time_partitioning.type_ == "DAY" | ||
assert table.time_partitioning.field == "date" |