From d1161dddde41a7d35b30033ccbf6984a5de640bd Mon Sep 17 00:00:00 2001 From: Kira Date: Wed, 17 Jan 2024 15:53:20 -0500 Subject: [PATCH] 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 --- samples/client_query_add_column.py | 5 ++--- samples/client_query_destination_table_clustered.py | 5 +++-- samples/client_query_legacy_sql.py | 8 +++++--- samples/client_query_relax_column.py | 5 ++--- samples/client_query_w_struct_params.py | 6 ++++-- samples/download_public_data_sandbox.py | 4 +++- samples/snippets/authorized_view_tutorial.py | 6 ++---- samples/snippets/natality_tutorial.py | 3 +-- samples/snippets/simple_app.py | 6 ++---- samples/tests/conftest.py | 2 +- 10 files changed, 25 insertions(+), 25 deletions(-) diff --git a/samples/client_query_add_column.py b/samples/client_query_add_column.py index ec14087fb..6aae5fce4 100644 --- a/samples/client_query_add_column.py +++ b/samples/client_query_add_column.py @@ -36,14 +36,13 @@ def client_query_add_column(table_id: str) -> None: ) # Start the query, passing in the extra configuration. - query_job = client.query( + client.query_and_wait( # In this example, the existing table contains only the 'full_name' and # 'age' columns, while the results of this query will contain an # additional 'favorite_color' column. 'SELECT "Timmy" as full_name, 85 as age, "Blue" as favorite_color;', job_config=job_config, - ) # Make an API request. - query_job.result() # Wait for the job to complete. + ) # Make an API request and wait for job to complete. # Checks the updated length of the schema. table = client.get_table(table_id) # Make an API request. diff --git a/samples/client_query_destination_table_clustered.py b/samples/client_query_destination_table_clustered.py index de9fff2d0..19330500a 100644 --- a/samples/client_query_destination_table_clustered.py +++ b/samples/client_query_destination_table_clustered.py @@ -31,8 +31,9 @@ def client_query_destination_table_clustered(table_id: str) -> None: ) # 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. + client.query_and_wait( + sql, job_config=job_config + ) # Make an API request and wait for job to complete. table = client.get_table(table_id) # Make an API request. if table.clustering_fields == cluster_fields: diff --git a/samples/client_query_legacy_sql.py b/samples/client_query_legacy_sql.py index 44917e4e0..1fb5b797a 100644 --- a/samples/client_query_legacy_sql.py +++ b/samples/client_query_legacy_sql.py @@ -29,10 +29,12 @@ def client_query_legacy_sql() -> None: # Set use_legacy_sql to True to use legacy SQL syntax. job_config = bigquery.QueryJobConfig(use_legacy_sql=True) - # Start the query, passing in the extra configuration. - query_job = client.query(query, job_config=job_config) # Make an API request. + # Start the query and waits for query job to complete, passing in the extra configuration. + results = client.query_and_wait( + query, job_config=job_config + ) # Make an API request. print("The query data:") - for row in query_job: + for row in results: print(row) # [END bigquery_query_legacy] diff --git a/samples/client_query_relax_column.py b/samples/client_query_relax_column.py index 22ecb33d1..26dce888f 100644 --- a/samples/client_query_relax_column.py +++ b/samples/client_query_relax_column.py @@ -39,13 +39,12 @@ def client_query_relax_column(table_id: str) -> None: ) # Start the query, passing in the extra configuration. - query_job = client.query( + client.query_and_wait( # In this example, the existing table contains 'full_name' and 'age' as # required columns, but the query results will omit the second column. 'SELECT "Beyonce" as full_name;', job_config=job_config, - ) # Make an API request. - query_job.result() # Wait for the job to complete. + ) # Make an API request and wait for job to complete # Checks the updated number of required fields. table = client.get_table(table_id) # Make an API request. diff --git a/samples/client_query_w_struct_params.py b/samples/client_query_w_struct_params.py index 6b68e78ed..cda2fcb43 100644 --- a/samples/client_query_w_struct_params.py +++ b/samples/client_query_w_struct_params.py @@ -30,8 +30,10 @@ def client_query_w_struct_params() -> None: ) ] ) - query_job = client.query(query, job_config=job_config) # Make an API request. + results = client.query_and_wait( + query, job_config=job_config + ) # Make an API request and waits for results. - for row in query_job: + for row in results: print(row.s) # [END bigquery_query_params_structs] diff --git a/samples/download_public_data_sandbox.py b/samples/download_public_data_sandbox.py index e165a31ce..909a7da05 100644 --- a/samples/download_public_data_sandbox.py +++ b/samples/download_public_data_sandbox.py @@ -27,7 +27,9 @@ def download_public_data_sandbox() -> None: query_string = "SELECT * FROM `bigquery-public-data.usa_names.usa_1910_current`" # Use the BigQuery Storage API to speed-up downloads of large tables. - dataframe = client.query(query_string).to_dataframe(create_bqstorage_client=True) + dataframe = client.query_and_wait(query_string).to_dataframe( + create_bqstorage_client=True + ) print(dataframe.info()) # [END bigquery_pandas_public_data_sandbox] diff --git a/samples/snippets/authorized_view_tutorial.py b/samples/snippets/authorized_view_tutorial.py index bfb61bc38..f52170bc6 100644 --- a/samples/snippets/authorized_view_tutorial.py +++ b/samples/snippets/authorized_view_tutorial.py @@ -62,15 +62,13 @@ def run_authorized_view_tutorial( FROM `bigquery-public-data.github_repos.commits` LIMIT 1000 """ - query_job = client.query( + client.query_and_wait( sql, # Location must match that of the dataset(s) referenced in the query # and of the destination table. location="US", job_config=job_config, - ) # API request - starts the query - - query_job.result() # Waits for the query to finish + ) # API request - starts the query and waits for query to finish # [END bigquery_avt_create_source_table] # Create a separate dataset to store your view diff --git a/samples/snippets/natality_tutorial.py b/samples/snippets/natality_tutorial.py index b330a3c21..df9fc15be 100644 --- a/samples/snippets/natality_tutorial.py +++ b/samples/snippets/natality_tutorial.py @@ -83,8 +83,7 @@ def run_natality_tutorial(override_values: Optional[Dict[str, str]] = None) -> N """ # Run the query. - query_job = client.query(query, job_config=job_config) - query_job.result() # Waits for the query to finish + client.query_and_wait(query, job_config=job_config) # Waits for the query to finish # [END bigquery_query_natality_tutorial] diff --git a/samples/snippets/simple_app.py b/samples/snippets/simple_app.py index 3d856d4bb..8281e1877 100644 --- a/samples/snippets/simple_app.py +++ b/samples/snippets/simple_app.py @@ -27,7 +27,7 @@ def query_stackoverflow() -> None: client = bigquery.Client() # [END bigquery_simple_app_client] # [START bigquery_simple_app_query] - query_job = client.query( + results = client.query_and_wait( """ SELECT CONCAT( @@ -38,9 +38,7 @@ def query_stackoverflow() -> None: WHERE tags like '%google-bigquery%' ORDER BY view_count DESC LIMIT 10""" - ) - - results = query_job.result() # Waits for job to complete. + ) # Waits for job to complete. # [END bigquery_simple_app_query] # [START bigquery_simple_app_print] diff --git a/samples/tests/conftest.py b/samples/tests/conftest.py index 99bd2e367..2b5b89c43 100644 --- a/samples/tests/conftest.py +++ b/samples/tests/conftest.py @@ -174,7 +174,7 @@ def model_id(client: bigquery.Client, dataset_id: str) -> str: model_id ) - client.query(sql).result() + client.query_and_wait(sql) return model_id