Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd1eb8b3

Browse files
ryanyuantswast
andauthored
docs(samples): add create_table_clustered code snippet (#291)
* docs(samples): add create_table_clustered code snippet* docs(samples): add create_table_clustered code snippet* fix unit test and lintCo-authored-by: Tim Swast <swast@google.com>
1 parent9090e1c commitd1eb8b3

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

‎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 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

‎samples/create_table_clustered.py‎

Lines 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+
defcreate_table_clustered(table_id):
17+
18+
# [START bigquery_create_table_clustered]
19+
fromgoogle.cloudimportbigquery
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+
returntable
Lines 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 ..importcreate_table_clustered
16+
17+
18+
deftest_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)inout
22+
asserttable.clustering_fields== ["city","zipcode"]

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp