Skip to content

Commit d1161dd

Browse files
authored
docs: update multiple samples to change query to query_and_wait (#1784)
* docs: update multiple samples for query_and_wait API * black * update rest of samples to use query_and_wait * changed query_jobs to results
1 parent ffe8059 commit d1161dd

10 files changed

+25
-25
lines changed

samples/client_query_add_column.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,13 @@ def client_query_add_column(table_id: str) -> None:
3636
)
3737

3838
# Start the query, passing in the extra configuration.
39-
query_job = client.query(
39+
client.query_and_wait(
4040
# In this example, the existing table contains only the 'full_name' and
4141
# 'age' columns, while the results of this query will contain an
4242
# additional 'favorite_color' column.
4343
'SELECT "Timmy" as full_name, 85 as age, "Blue" as favorite_color;',
4444
job_config=job_config,
45-
) # Make an API request.
46-
query_job.result() # Wait for the job to complete.
45+
) # Make an API request and wait for job to complete.
4746

4847
# Checks the updated length of the schema.
4948
table = client.get_table(table_id) # Make an API request.

samples/client_query_destination_table_clustered.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ def client_query_destination_table_clustered(table_id: str) -> None:
3131
)
3232

3333
# Start the query, passing in the extra configuration.
34-
query_job = client.query(sql, job_config=job_config) # Make an API request.
35-
query_job.result() # Wait for the job to complete.
34+
client.query_and_wait(
35+
sql, job_config=job_config
36+
) # Make an API request and wait for job to complete.
3637

3738
table = client.get_table(table_id) # Make an API request.
3839
if table.clustering_fields == cluster_fields:

samples/client_query_legacy_sql.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@ def client_query_legacy_sql() -> None:
2929
# Set use_legacy_sql to True to use legacy SQL syntax.
3030
job_config = bigquery.QueryJobConfig(use_legacy_sql=True)
3131

32-
# Start the query, passing in the extra configuration.
33-
query_job = client.query(query, job_config=job_config) # Make an API request.
32+
# Start the query and waits for query job to complete, passing in the extra configuration.
33+
results = client.query_and_wait(
34+
query, job_config=job_config
35+
) # Make an API request.
3436

3537
print("The query data:")
36-
for row in query_job:
38+
for row in results:
3739
print(row)
3840
# [END bigquery_query_legacy]

samples/client_query_relax_column.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,12 @@ def client_query_relax_column(table_id: str) -> None:
3939
)
4040

4141
# Start the query, passing in the extra configuration.
42-
query_job = client.query(
42+
client.query_and_wait(
4343
# In this example, the existing table contains 'full_name' and 'age' as
4444
# required columns, but the query results will omit the second column.
4545
'SELECT "Beyonce" as full_name;',
4646
job_config=job_config,
47-
) # Make an API request.
48-
query_job.result() # Wait for the job to complete.
47+
) # Make an API request and wait for job to complete
4948

5049
# Checks the updated number of required fields.
5150
table = client.get_table(table_id) # Make an API request.

samples/client_query_w_struct_params.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ def client_query_w_struct_params() -> None:
3030
)
3131
]
3232
)
33-
query_job = client.query(query, job_config=job_config) # Make an API request.
33+
results = client.query_and_wait(
34+
query, job_config=job_config
35+
) # Make an API request and waits for results.
3436

35-
for row in query_job:
37+
for row in results:
3638
print(row.s)
3739
# [END bigquery_query_params_structs]

samples/download_public_data_sandbox.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ def download_public_data_sandbox() -> None:
2727
query_string = "SELECT * FROM `bigquery-public-data.usa_names.usa_1910_current`"
2828

2929
# Use the BigQuery Storage API to speed-up downloads of large tables.
30-
dataframe = client.query(query_string).to_dataframe(create_bqstorage_client=True)
30+
dataframe = client.query_and_wait(query_string).to_dataframe(
31+
create_bqstorage_client=True
32+
)
3133

3234
print(dataframe.info())
3335
# [END bigquery_pandas_public_data_sandbox]

samples/snippets/authorized_view_tutorial.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,13 @@ def run_authorized_view_tutorial(
6262
FROM `bigquery-public-data.github_repos.commits`
6363
LIMIT 1000
6464
"""
65-
query_job = client.query(
65+
client.query_and_wait(
6666
sql,
6767
# Location must match that of the dataset(s) referenced in the query
6868
# and of the destination table.
6969
location="US",
7070
job_config=job_config,
71-
) # API request - starts the query
72-
73-
query_job.result() # Waits for the query to finish
71+
) # API request - starts the query and waits for query to finish
7472
# [END bigquery_avt_create_source_table]
7573

7674
# Create a separate dataset to store your view

samples/snippets/natality_tutorial.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ def run_natality_tutorial(override_values: Optional[Dict[str, str]] = None) -> N
8383
"""
8484

8585
# Run the query.
86-
query_job = client.query(query, job_config=job_config)
87-
query_job.result() # Waits for the query to finish
86+
client.query_and_wait(query, job_config=job_config) # Waits for the query to finish
8887
# [END bigquery_query_natality_tutorial]
8988

9089

samples/snippets/simple_app.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def query_stackoverflow() -> None:
2727
client = bigquery.Client()
2828
# [END bigquery_simple_app_client]
2929
# [START bigquery_simple_app_query]
30-
query_job = client.query(
30+
results = client.query_and_wait(
3131
"""
3232
SELECT
3333
CONCAT(
@@ -38,9 +38,7 @@ def query_stackoverflow() -> None:
3838
WHERE tags like '%google-bigquery%'
3939
ORDER BY view_count DESC
4040
LIMIT 10"""
41-
)
42-
43-
results = query_job.result() # Waits for job to complete.
41+
) # Waits for job to complete.
4442
# [END bigquery_simple_app_query]
4543

4644
# [START bigquery_simple_app_print]

samples/tests/conftest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def model_id(client: bigquery.Client, dataset_id: str) -> str:
174174
model_id
175175
)
176176

177-
client.query(sql).result()
177+
client.query_and_wait(sql)
178178
return model_id
179179

180180

0 commit comments

Comments
 (0)