From 4950c10715d45e003bc6206e2a4feec4c5dd3c6b Mon Sep 17 00:00:00 2001 From: Leah Cole Date: Wed, 18 Jun 2025 10:46:02 -0400 Subject: [PATCH 1/4] chore: add label job sample --- samples/snippets/label_job.py | 40 ++++++++++++++++++++++++++++++ samples/snippets/label_job_test.py | 33 ++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 samples/snippets/label_job.py create mode 100644 samples/snippets/label_job_test.py diff --git a/samples/snippets/label_job.py b/samples/snippets/label_job.py new file mode 100644 index 000000000..015617f02 --- /dev/null +++ b/samples/snippets/label_job.py @@ -0,0 +1,40 @@ +# Copyright 2022 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 label_job() -> None: + # [START bigquery_label_job] + from google.cloud import bigquery + + client = bigquery.Client() + + sql = """ + SELECT corpus + FROM `bigquery-public-data.samples.shakespeare` + GROUP BY corpus; + """ + labels = {"color": "green"} + + config = bigquery.QueryJobConfig() + config.labels = labels + location = "us" + job = client.query(sql, location=location, job_config=config) + job_id = job.job_id + + + job = client.get_job(job_id) # API request + + print(f"Added {job.labels} to {job_id}.") + # [END bigquery_label_job] + diff --git a/samples/snippets/label_job_test.py b/samples/snippets/label_job_test.py new file mode 100644 index 000000000..a709f1ce0 --- /dev/null +++ b/samples/snippets/label_job_test.py @@ -0,0 +1,33 @@ +# Copyright 2022 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. + +import typing + +import label_job # type: ignore + + +if typing.TYPE_CHECKING: + import pytest + + +def test_label_job( + capsys: "pytest.CaptureFixture[str]", +) -> None: + + + label_job.label_job() + + out, _ = capsys.readouterr() + assert "color" in out + assert "green" in out From 913db979f3349a5b59a11ad7b38e5e4ce56e6d86 Mon Sep 17 00:00:00 2001 From: Leah Cole Date: Wed, 18 Jun 2025 11:15:52 -0400 Subject: [PATCH 2/4] lint --- samples/snippets/label_job.py | 2 -- samples/snippets/label_job_test.py | 1 - 2 files changed, 3 deletions(-) diff --git a/samples/snippets/label_job.py b/samples/snippets/label_job.py index 015617f02..606229555 100644 --- a/samples/snippets/label_job.py +++ b/samples/snippets/label_job.py @@ -32,9 +32,7 @@ def label_job() -> None: job = client.query(sql, location=location, job_config=config) job_id = job.job_id - job = client.get_job(job_id) # API request print(f"Added {job.labels} to {job_id}.") # [END bigquery_label_job] - diff --git a/samples/snippets/label_job_test.py b/samples/snippets/label_job_test.py index a709f1ce0..cc4e1799d 100644 --- a/samples/snippets/label_job_test.py +++ b/samples/snippets/label_job_test.py @@ -25,7 +25,6 @@ def test_label_job( capsys: "pytest.CaptureFixture[str]", ) -> None: - label_job.label_job() out, _ = capsys.readouterr() From b8f2baa4620589143cf53902d8bf9136f54d7667 Mon Sep 17 00:00:00 2001 From: Leah Cole Date: Wed, 18 Jun 2025 13:58:10 -0400 Subject: [PATCH 3/4] remove unnecessary api call --- samples/snippets/label_job.py | 2 -- samples/snippets/label_job_test.py | 1 - 2 files changed, 3 deletions(-) diff --git a/samples/snippets/label_job.py b/samples/snippets/label_job.py index 606229555..6639934ff 100644 --- a/samples/snippets/label_job.py +++ b/samples/snippets/label_job.py @@ -32,7 +32,5 @@ def label_job() -> None: job = client.query(sql, location=location, job_config=config) job_id = job.job_id - job = client.get_job(job_id) # API request - print(f"Added {job.labels} to {job_id}.") # [END bigquery_label_job] diff --git a/samples/snippets/label_job_test.py b/samples/snippets/label_job_test.py index cc4e1799d..e6be5fb21 100644 --- a/samples/snippets/label_job_test.py +++ b/samples/snippets/label_job_test.py @@ -24,7 +24,6 @@ def test_label_job( capsys: "pytest.CaptureFixture[str]", ) -> None: - label_job.label_job() out, _ = capsys.readouterr() From f4da89ad4b381037c42d8862c9e390cac47e92c6 Mon Sep 17 00:00:00 2001 From: "Leah E. Cole" <6719667+leahecole@users.noreply.github.com> Date: Mon, 23 Jun 2025 09:50:40 -0400 Subject: [PATCH 4/4] Apply suggestions from code review Co-authored-by: Chalmer Lowe --- samples/snippets/label_job.py | 2 +- samples/snippets/label_job_test.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/snippets/label_job.py b/samples/snippets/label_job.py index 6639934ff..cfd06d189 100644 --- a/samples/snippets/label_job.py +++ b/samples/snippets/label_job.py @@ -1,4 +1,4 @@ -# Copyright 2022 Google LLC +# Copyright 2025 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/samples/snippets/label_job_test.py b/samples/snippets/label_job_test.py index e6be5fb21..0780db61a 100644 --- a/samples/snippets/label_job_test.py +++ b/samples/snippets/label_job_test.py @@ -1,4 +1,4 @@ -# Copyright 2022 Google LLC +# Copyright 2025 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License.