Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BigQuery: rewrite simple app tutorial. #1270

Merged
merged 1 commit into from
Dec 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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