GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — docs(samples): add create_table_clustered code snippet (#291) · toransahu/python-bigquery@d1eb8b3

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • docs/usage
    • tables.rst
  • samples
    • create_table_clustered.py
    • tests
      • test_create_table_clustered.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎docs/usage/tables.rst‎

Copy file name to clipboardExpand all lines: docs/usage/tables.rst
+9Lines changed: 9 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
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 a clustered table with the
62+
:func:`~google.cloud.bigquery.client.Client.create_table` method:
63+
64+
.. literalinclude:: ../samples/create_table_clustered.py
65+
:language: python
66+
:dedent: 4
67+
:start-after: [START bigquery_create_table_clustered]
68+
:end-before: [END bigquery_create_table_clustered]
69+
6170
Create an integer range partitioned table with the
6271
:func:`~google.cloud.bigquery.client.Client.create_table` method:
6372

Collapse file

‎samples/create_table_clustered.py‎

Copy file name to clipboard
+42Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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_clustered(table_id):
17+
18+
# [START bigquery_create_table_clustered]
19+
from google.cloud import bigquery
20+
21+
# 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.clustering_fields = ["city", "zipcode"]
35+
table = client.create_table(table) # Make an API request.
36+
print(
37+
"Created clustered table {}.{}.{}".format(
38+
table.project, table.dataset_id, table.table_id
39+
)
40+
)
41+
# [END bigquery_create_table_clustered]
42+
return table
Collapse file

‎samples/tests/test_create_table_clustered.py‎

Copy file name to clipboard
+22Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
from .. import create_table_clustered
16+
17+
18+
def test_create_table_clustered(capsys, random_table_id):
19+
table = create_table_clustered.create_table_clustered(random_table_id)
20+
out, _ = capsys.readouterr()
21+
assert "Created clustered table {}".format(random_table_id) in out
22+
assert table.clustering_fields == ["city", "zipcode"]

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.