From cb95c6d18dd5e7df0b6a472ceaa4fde7dbb8c9a0 Mon Sep 17 00:00:00 2001 From: kiraksi Date: Tue, 16 Jan 2024 16:27:01 -0800 Subject: [PATCH 1/4] docs: update multiple samples for query_and_wait API --- samples/client_query_w_struct_params.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/client_query_w_struct_params.py b/samples/client_query_w_struct_params.py index 6b68e78ed..71ab9f23c 100644 --- a/samples/client_query_w_struct_params.py +++ b/samples/client_query_w_struct_params.py @@ -30,7 +30,7 @@ def client_query_w_struct_params() -> None: ) ] ) - query_job = client.query(query, job_config=job_config) # Make an API request. + query_job = client.query_and_wait(query, job_config=job_config) # Make an API request. for row in query_job: print(row.s) From f1fc47aefd5e89ec16a544d6ded63b867ad48cd0 Mon Sep 17 00:00:00 2001 From: kiraksi Date: Wed, 17 Jan 2024 02:41:29 -0800 Subject: [PATCH 2/4] black --- samples/client_query_w_struct_params.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/samples/client_query_w_struct_params.py b/samples/client_query_w_struct_params.py index 71ab9f23c..af49395d4 100644 --- a/samples/client_query_w_struct_params.py +++ b/samples/client_query_w_struct_params.py @@ -30,7 +30,9 @@ def client_query_w_struct_params() -> None: ) ] ) - query_job = client.query_and_wait(query, job_config=job_config) # Make an API request. + query_job = client.query_and_wait( + query, job_config=job_config + ) # Make an API request. for row in query_job: print(row.s) From ff9f1a551b13d2c24bae315e41347d428c10232e Mon Sep 17 00:00:00 2001 From: kiraksi Date: Wed, 17 Jan 2024 03:17:26 -0800 Subject: [PATCH 3/4] update rest of samples to use query_and_wait --- samples/client_query_add_column.py | 5 ++--- samples/client_query_destination_table_clustered.py | 5 +++-- samples/client_query_legacy_sql.py | 6 ++++-- samples/client_query_relax_column.py | 5 ++--- samples/client_query_w_struct_params.py | 2 +- 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, 21 insertions(+), 23 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..eba0542ba 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 results 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..d92dd156b 100644 --- a/samples/client_query_legacy_sql.py +++ b/samples/client_query_legacy_sql.py @@ -29,8 +29,10 @@ 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. + query_job = client.query_and_wait( + query, job_config=job_config + ) # Make an API request. print("The query data:") for row in query_job: 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 af49395d4..a7205d1ba 100644 --- a/samples/client_query_w_struct_params.py +++ b/samples/client_query_w_struct_params.py @@ -32,7 +32,7 @@ def client_query_w_struct_params() -> None: ) query_job = client.query_and_wait( query, job_config=job_config - ) # Make an API request. + ) # Make an API request and waits for results. for row in query_job: print(row.s) 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 From 085cf83c840197af85d17c07f3a926ee1d4c711e Mon Sep 17 00:00:00 2001 From: kiraksi Date: Wed, 17 Jan 2024 10:49:02 -0800 Subject: [PATCH 4/4] changed query_jobs to results --- samples/client_query_destination_table_clustered.py | 2 +- samples/client_query_legacy_sql.py | 4 ++-- samples/client_query_w_struct_params.py | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/samples/client_query_destination_table_clustered.py b/samples/client_query_destination_table_clustered.py index eba0542ba..19330500a 100644 --- a/samples/client_query_destination_table_clustered.py +++ b/samples/client_query_destination_table_clustered.py @@ -33,7 +33,7 @@ def client_query_destination_table_clustered(table_id: str) -> None: # Start the query, passing in the extra configuration. client.query_and_wait( sql, job_config=job_config - ) # Make an API request and wait for results + ) # 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 d92dd156b..1fb5b797a 100644 --- a/samples/client_query_legacy_sql.py +++ b/samples/client_query_legacy_sql.py @@ -30,11 +30,11 @@ def client_query_legacy_sql() -> None: job_config = bigquery.QueryJobConfig(use_legacy_sql=True) # Start the query and waits for query job to complete, passing in the extra configuration. - query_job = client.query_and_wait( + 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_w_struct_params.py b/samples/client_query_w_struct_params.py index a7205d1ba..cda2fcb43 100644 --- a/samples/client_query_w_struct_params.py +++ b/samples/client_query_w_struct_params.py @@ -30,10 +30,10 @@ def client_query_w_struct_params() -> None: ) ] ) - query_job = client.query_and_wait( + 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]