Skip to content

Commit 5aeedaa

Browse files
chalmerlowedandhleegcf-owl-bot[bot]
authored
fix: uses function (create_job) more appropriate to the described sample intent (#1309)
* fix: uses function more appropriate to the described title * adds additional explanation for the end users * adds REST API URL for reference * corrects flake 8 linter errors * blackens file * adds type hints * avoids unreliable version of grpcio * updates imports to fix linting error * better method to avoid grpcio 1.49.0rc1 * Update samples/create_job.py Co-authored-by: Dan Lee <[email protected]> * adds further explanation on when/why to use create_jobs * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * updates references Co-authored-by: Dan Lee <[email protected]> Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent b9e882f commit 5aeedaa

File tree

1 file changed

+37
-15
lines changed

1 file changed

+37
-15
lines changed

samples/create_job.py

+37-15
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,55 @@
1313
# limitations under the License.
1414

1515
import typing
16+
from typing import Union
1617

1718
if typing.TYPE_CHECKING:
18-
from google.cloud import bigquery
19+
from google.cloud.bigquery import LoadJob, CopyJob, ExtractJob, QueryJob
1920

2021

21-
def create_job() -> "bigquery.QueryJob":
22+
def create_job() -> "Union[LoadJob, CopyJob, ExtractJob, QueryJob]":
2223

2324
# [START bigquery_create_job]
2425
from google.cloud import bigquery
2526

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

29-
query_job = client.query(
30-
"SELECT country_name from `bigquery-public-data.utility_us.country_code_iso`",
31-
# Explicitly force job execution to be routed to a specific processing
32-
# location.
33-
location="US",
34-
# Specify a job configuration to set optional job resource properties.
35-
job_config=bigquery.QueryJobConfig(
36-
labels={"example-label": "example-value"}, maximum_bytes_billed=1000000
37-
),
38-
# The client libraries automatically generate a job ID. Override the
39-
# generated ID with either the job_id_prefix or job_id parameters.
40-
job_id_prefix="code_sample_",
30+
query_job = client.create_job(
31+
# Specify a job configuration, providing a query
32+
# and/or optional job resource properties, as needed.
33+
# The job instance can be a LoadJob, CopyJob, ExtractJob, QueryJob
34+
# Here, we demonstrate a "query" job.
35+
# References:
36+
# https://googleapis.dev/python/bigquery/latest/generated/google.cloud.bigquery.client.Client.html#google.cloud.bigquery.client.Client.create_job
37+
# https://cloud.google.com/bigquery/docs/reference/rest/v2/Job
38+
#
39+
# Example use cases for .create_job() include:
40+
# * to retry failed jobs
41+
# * to generate jobs with an experimental API property that hasn't
42+
# been added to one of the manually written job configuration
43+
# classes yet
44+
#
45+
# NOTE: unless it is necessary to create a job in this way, the
46+
# preferred approach is to use one of the dedicated API calls:
47+
# client.query()
48+
# client.extract_table()
49+
# client.copy_table()
50+
# client.load_table_file(), client.load_table_from_dataframe(), etc
51+
job_config={
52+
"query": {
53+
"query": """
54+
SELECT country_name
55+
FROM `bigquery-public-data.utility_us.country_code_iso`
56+
LIMIT 5
57+
""",
58+
},
59+
"labels": {"example-label": "example-value"},
60+
"maximum_bytes_billed": 10000000,
61+
}
4162
) # Make an API request.
4263

43-
print("Started job: {}".format(query_job.job_id))
64+
print(f"Started job: {query_job.job_id}")
4465
# [END bigquery_create_job]
66+
4567
return query_job

0 commit comments

Comments
 (0)