From 17129602d63fd6290980ffc35a84d9889b9d6fda Mon Sep 17 00:00:00 2001 From: Tim Swast Date: Fri, 1 Dec 2017 17:07:38 -0800 Subject: [PATCH] BigQuery: rewrite simple app tutorial. - Add region tags for needed dependencies. - Use more relevant query from public datasets. --- bigquery/cloud-client/requirements.txt | 2 ++ bigquery/cloud-client/simple_app.py | 23 ++++++++++++++--------- bigquery/cloud-client/simple_app_test.py | 6 +++--- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/bigquery/cloud-client/requirements.txt b/bigquery/cloud-client/requirements.txt index c2b4ac2d5adc..618a85dfd39e 100644 --- a/bigquery/cloud-client/requirements.txt +++ b/bigquery/cloud-client/requirements.txt @@ -1,3 +1,5 @@ +# [START bigquery_simple_app_pkgs] google-cloud-bigquery==0.28.0 +# [END bigquery_simple_app_pkgs] google-auth-oauthlib==0.2.0 pytz==2017.3 diff --git a/bigquery/cloud-client/simple_app.py b/bigquery/cloud-client/simple_app.py index 5d0d04e666d9..9f266ed7aac4 100644 --- a/bigquery/cloud-client/simple_app.py +++ b/bigquery/cloud-client/simple_app.py @@ -16,20 +16,25 @@ """Simple application that performs a query with BigQuery.""" # [START all] -# [START create_client] +# [START bigquery_simple_app_deps] from google.cloud import bigquery +# [END bigquery_simple_app_deps] -def query_shakespeare(): +def query_stackoverflow(): + # [START create_client] client = bigquery.Client() # [END create_client] # [START run_query] query_job = client.query(""" - #standardSQL - SELECT corpus AS title, COUNT(*) AS unique_words - FROM `bigquery-public-data.samples.shakespeare` - GROUP BY title - ORDER BY unique_words DESC + SELECT + CONCAT( + 'https://stackoverflow.com/questions/', + CAST(id as STRING)) as url, + view_count + FROM `bigquery-public-data.stackoverflow.posts_questions` + WHERE tags like '%google-bigquery%' + ORDER BY view_count DESC LIMIT 10""") results = query_job.result() # Waits for job to complete. @@ -37,10 +42,10 @@ def query_shakespeare(): # [START print_results] for row in results: - print("{}: {}".format(row.title, row.unique_words)) + print("{} : {} views".format(row.url, row.view_count)) # [END print_results] if __name__ == '__main__': - query_shakespeare() + query_stackoverflow() # [END all] diff --git a/bigquery/cloud-client/simple_app_test.py b/bigquery/cloud-client/simple_app_test.py index 3733bf6ef3fe..33f9f1adf69a 100644 --- a/bigquery/cloud-client/simple_app_test.py +++ b/bigquery/cloud-client/simple_app_test.py @@ -15,7 +15,7 @@ import simple_app -def test_query_shakespeare(capsys): - simple_app.query_shakespeare() +def test_query_stackoverflow(capsys): + simple_app.query_stackoverflow() out, _ = capsys.readouterr() - assert 'hamlet' in out + assert 'views' in out