Skip to content

Commit

Permalink
Merge pull request #1270 from GoogleCloudPlatform/tswast-simple-app
Browse files Browse the repository at this point in the history
BigQuery: rewrite simple app tutorial.
  • Loading branch information
tswast authored Dec 12, 2017
2 parents 5c4426c + 1712960 commit 278afe6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
2 changes: 2 additions & 0 deletions bigquery/cloud-client/requirements.txt
Original file line number Diff line number Diff line change
@@ -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
23 changes: 14 additions & 9 deletions bigquery/cloud-client/simple_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,36 @@

"""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.
# [END run_query]

# [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]
6 changes: 3 additions & 3 deletions bigquery/cloud-client/simple_app_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 278afe6

Please sign in to comment.