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 more clustering code snippets by ryanyuan · Pull Request #330 · googleapis/python-bigquery

This repository was archived by the owner on Mar 6, 2026. It is now read-only.

docs(samples): add more clustering code snippets #330

Merged
gcf-merge-on-green merged 7 commits into googleapis:master from ryanyuan:add-sample-code-clustering
Nov 17, 2020

Uh oh!

There was an error while loading. Please reload this page.

Merged

docs(samples): add more clustering code snippets #330

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
7 commits
Select commit Hold shift + click to select a range
2ca69be
docs(samples): add more clustering code snippets
ryanyuan Oct 18, 2020
5ac35eb
Merge remote-tracking branch 'upstream/master' into add-sample-code-c…
tswast Nov 4, 2020
307fb42
docs: use clustered source file from GCS
tswast Nov 4, 2020
39d0c28
blacken
tswast Nov 4, 2020
ce9c62c
Merge branch 'master' into add-sample-code-clustering
tswast Nov 4, 2020
0293145
docs(samples): add more clustering code snippets
ryanyuan Nov 4, 2020
9cdad19
Merge branch 'master' into add-sample-code-clustering
tswast Nov 17, 2020
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

17 changes: 17 additions & 0 deletions 17 docs/usage/tables.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,23 @@ Load table data from a file with the
:start-after: [START bigquery_load_from_file]
:end-before: [END bigquery_load_from_file]

Creating a clustered table from a query result:

.. literalinclude:: ../samples/client_query_destination_table_clustered.py
:language: python
:dedent: 4
:start-after: [START bigquery_query_clustered_table]
:end-before: [END bigquery_query_clustered_table]

Creating a clustered table when you load data with the
:func:`~google.cloud.bigquery.client.Client.load_table_from_uri` method:

.. literalinclude:: ../samples/load_table_clustered.py
:language: python
:dedent: 4
:start-after: [START bigquery_load_table_clustered]
:end-before: [END bigquery_load_table_clustered]

Load a CSV file from Cloud Storage with the
:func:`~google.cloud.bigquery.client.Client.load_table_from_uri` method:

Expand Down
4 changes: 3 additions & 1 deletion 4 google/cloud/bigquery/__init__.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from google.cloud.bigquery.dataset import Dataset
from google.cloud.bigquery.dataset import DatasetReference
from google.cloud.bigquery import enums
from google.cloud.bigquery.enums import SqlTypeNames
from google.cloud.bigquery.enums import StandardSqlDataTypes
from google.cloud.bigquery.external_config import ExternalConfig
from google.cloud.bigquery.external_config import BigtableOptions
Expand Down Expand Up @@ -137,8 +138,9 @@
"Encoding",
"QueryPriority",
"SchemaUpdateOption",
"StandardSqlDataTypes",
"SourceFormat",
"SqlTypeNames",
"StandardSqlDataTypes",
"WriteDisposition",
# EncryptionConfiguration
"EncryptionConfiguration",
Expand Down
43 changes: 43 additions & 0 deletions 43 samples/client_query_destination_table_clustered.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Copyright 2020 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 client_query_destination_table_clustered(table_id):

# [START bigquery_query_clustered_table]
from google.cloud import bigquery

# Construct a BigQuery client object.
client = bigquery.Client()

# TODO(developer): Set table_id to the ID of the destination table.
# table_id = "your-project.your_dataset.your_table_name"

sql = "SELECT * FROM `bigquery-public-data.samples.shakespeare`"
cluster_fields = ["corpus"]

job_config = bigquery.QueryJobConfig(
clustering_fields=cluster_fields, destination=table_id
)

# Start the query, passing in the extra configuration.
query_job = client.query(sql, job_config=job_config) # Make an API request.
query_job.result() # Wait for the job to complete.

table = client.get_table(table_id) # Make an API request.
if table.clustering_fields == cluster_fields:
print(
"The destination table is written using the cluster_fields configuration."
)
# [END bigquery_query_clustered_table]
55 changes: 55 additions & 0 deletions 55 samples/load_table_clustered.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Copyright 2020 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
#
# http://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 load_table_clustered(table_id):

# [START bigquery_load_table_clustered]
from google.cloud import bigquery

# Construct a BigQuery client object.
client = bigquery.Client()

# TODO(developer): Set table_id to the ID of the table to create.
# table_id = "your-project.your_dataset.your_table_name"

job_config = bigquery.LoadJobConfig(
skip_leading_rows=1,
source_format=bigquery.SourceFormat.CSV,
schema=[
bigquery.SchemaField("timestamp", bigquery.SqlTypeNames.TIMESTAMP),
bigquery.SchemaField("origin", bigquery.SqlTypeNames.STRING),
bigquery.SchemaField("destination", bigquery.SqlTypeNames.STRING),
bigquery.SchemaField("amount", bigquery.SqlTypeNames.NUMERIC),
],
time_partitioning=bigquery.TimePartitioning(field="timestamp"),
clustering_fields=["origin", "destination"],
)

job = client.load_table_from_uri(
["gs://cloud-samples-data/bigquery/sample-transactions/transactions.csv"],
table_id,
job_config=job_config,
)

job.result() # Waits for the job to complete.

table = client.get_table(table_id) # Make an API request.
print(
"Loaded {} rows and {} columns to {}".format(
table.num_rows, len(table.schema), table_id
)
)
# [END bigquery_load_table_clustered]
return table
27 changes: 27 additions & 0 deletions 27 samples/tests/test_client_query_destination_table_clustered.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright 2020 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.

from .. import client_query_destination_table_clustered


def test_client_query_destination_table_clustered(capsys, random_table_id):

client_query_destination_table_clustered.client_query_destination_table_clustered(
random_table_id
)
out, err = capsys.readouterr()
assert (
"The destination table is written using the cluster_fields configuration."
in out
)
27 changes: 27 additions & 0 deletions 27 samples/tests/test_load_table_clustered.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright 2020 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.

from .. import load_table_clustered


def test_load_table_clustered(capsys, random_table_id, client):

table = load_table_clustered.load_table_clustered(random_table_id)

out, _ = capsys.readouterr()
assert "rows and 4 columns" in out

rows = list(client.list_rows(table)) # Make an API request.
assert len(rows) > 0
assert table.clustering_fields == ["origin", "destination"]
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — 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.