Skip to content

Commit

Permalink
docs: update multiple samples to change query to query_and_wait (#1784)
Browse files Browse the repository at this point in the history
* docs: update multiple samples for query_and_wait API

* black

* update rest of samples to use query_and_wait

* changed query_jobs to results
  • Loading branch information
kiraksi authored Jan 17, 2024
1 parent ffe8059 commit d1161dd
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 25 deletions.
5 changes: 2 additions & 3 deletions samples/client_query_add_column.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 3 additions & 2 deletions samples/client_query_destination_table_clustered.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 5 additions & 3 deletions samples/client_query_legacy_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
5 changes: 2 additions & 3 deletions samples/client_query_relax_column.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 4 additions & 2 deletions samples/client_query_w_struct_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
4 changes: 3 additions & 1 deletion samples/download_public_data_sandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
6 changes: 2 additions & 4 deletions samples/snippets/authorized_view_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions samples/snippets/natality_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]


Expand Down
6 changes: 2 additions & 4 deletions samples/snippets/simple_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion samples/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down

0 comments on commit d1161dd

Please sign in to comment.