|
13 | 13 | # limitations under the License.
|
14 | 14 |
|
15 | 15 | import typing
|
| 16 | +from typing import Union |
16 | 17 |
|
17 | 18 | if typing.TYPE_CHECKING:
|
18 |
| - from google.cloud import bigquery |
| 19 | + from google.cloud.bigquery import LoadJob, CopyJob, ExtractJob, QueryJob |
19 | 20 |
|
20 | 21 |
|
21 |
| -def create_job() -> "bigquery.QueryJob": |
| 22 | +def create_job() -> "Union[LoadJob, CopyJob, ExtractJob, QueryJob]": |
22 | 23 |
|
23 | 24 | # [START bigquery_create_job]
|
24 | 25 | from google.cloud import bigquery
|
25 | 26 |
|
26 | 27 | # Construct a BigQuery client object.
|
27 | 28 | client = bigquery.Client()
|
28 | 29 |
|
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 | + } |
41 | 62 | ) # Make an API request.
|
42 | 63 |
|
43 |
| - print("Started job: {}".format(query_job.job_id)) |
| 64 | + print(f"Started job: {query_job.job_id}") |
44 | 65 | # [END bigquery_create_job]
|
| 66 | + |
45 | 67 | return query_job
|
0 commit comments