Skip to content

Commit c4a69d4

Browse files
tswastplamut
authored andcommitted
doc(bigquery): add table create sample using integer range partitioning (#9478)
* doc(bigquery): add table create sample using integer range partitioning This code sample doubles as a system test for integer range partitioning features. * blacken * add range partitioned sample to docs
1 parent 9405db9 commit c4a69d4

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

bigquery/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 an integer range partitioned table with the
62+
:func:`~google.cloud.bigquery.client.Client.create_table` method:
63+
64+
.. literalinclude:: ../samples/create_table_range_partitioned.py
65+
:language: python
66+
:dedent: 4
67+
:start-after: [START bigquery_create_table_range_partitioned]
68+
:end-before: [END bigquery_create_table_range_partitioned]
69+
6170
Load table data from a file with the
6271
:func:`~google.cloud.bigquery.client.Client.load_table_from_file` method:
6372

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Copyright 2019 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_table_range_partitioned(client, table_id):
17+
18+
# [START bigquery_create_table_range_partitioned]
19+
from google.cloud import bigquery
20+
21+
# TODO(developer): Construct a BigQuery client object.
22+
# client = bigquery.Client()
23+
24+
# TODO(developer): Set table_id to the ID of the table to create.
25+
# table_id = "your-project.your_dataset.your_table_name"
26+
27+
schema = [
28+
bigquery.SchemaField("full_name", "STRING"),
29+
bigquery.SchemaField("city", "STRING"),
30+
bigquery.SchemaField("zipcode", "INTEGER"),
31+
]
32+
33+
table = bigquery.Table(table_id, schema=schema)
34+
table.range_partitioning = bigquery.RangePartitioning(
35+
# To use integer range partitioning, select a top-level REQUIRED /
36+
# NULLABLE column with INTEGER / INT64 data type.
37+
field="zipcode",
38+
range_=bigquery.PartitionRange(start=0, end=100000, interval=10),
39+
)
40+
table = client.create_table(table) # Make an API request.
41+
print(
42+
"Created table {}.{}.{}".format(table.project, table.dataset_id, table.table_id)
43+
)
44+
# [END bigquery_create_table_range_partitioned]
45+
return table
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Copyright 2019 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+
from .. import create_table_range_partitioned
17+
18+
19+
def test_create_table_range_partitioned(capsys, client, random_table_id):
20+
table = create_table_range_partitioned.create_table_range_partitioned(
21+
client, random_table_id
22+
)
23+
out, _ = capsys.readouterr()
24+
assert "Created table {}".format(random_table_id) in out
25+
assert table.range_partitioning.field == "zipcode"
26+
assert table.range_partitioning.range_.start == 0
27+
assert table.range_partitioning.range_.end == 100000
28+
assert table.range_partitioning.range_.interval == 10

0 commit comments

Comments
 (0)