Skip to content

Commit e1f6921

Browse files
committed
doc(bigquery): add table create sample using integer range partitioning
This code sample doubles as a system test for integer range partitioning features.
1 parent ecb2162 commit e1f6921

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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(
39+
start=0,
40+
end=100000,
41+
interval=10,
42+
),
43+
)
44+
table = client.create_table(table) # Make an API request.
45+
print(
46+
"Created table {}.{}.{}".format(table.project, table.dataset_id, table.table_id)
47+
)
48+
# [END bigquery_create_table_range_partitioned]
49+
return table
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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(client, random_table_id)
21+
out, _ = capsys.readouterr()
22+
assert "Created table {}".format(random_table_id) in out
23+
assert table.range_partitioning.field == "zipcode"
24+
assert table.range_partitioning.range_.start == 0
25+
assert table.range_partitioning.range_.end == 100000
26+
assert table.range_partitioning.range_.interval == 10

0 commit comments

Comments
 (0)