8000 BigQuery: rewrite simple app tutorial. · johnmanong/python-docs-samples@1712960 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1712960

Browse files
committed
BigQuery: rewrite simple app tutorial.
- Add region tags for needed dependencies. - Use more relevant query from public datasets.
1 parent 5433252 commit 1712960

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# [START bigquery_simple_app_pkgs]
12
google-cloud-bigquery==0.28.0
3+
# [END bigquery_simple_app_pkgs]
24
google-auth-oauthlib==0.2.0
35
pytz==2017.3

bigquery/cloud-client/simple_app.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,36 @@
1616

1717
"""Simple application that performs a query with BigQuery."""
1818
# [START all]
19-
# [START create_client]
19+
# [START bigquery_simple_app_deps]
2020
from google.cloud import bigquery
21+
# [END bigquery_simple_app_deps]
2122

2223

23-
def query_shakespeare():
24+
def query_stackoverflow():
25+
# [START create_client]
2426
client = bigquery.Client()
2527
# [END create_client]
2628
# [START run_query]
2729
query_job = client.query("""
28-
#standardSQL
29-
SELECT corpus AS title, COUNT(*) AS unique_words
30-
FROM `bigquery-public-data.samples.shakespeare`
31-
GROUP BY title
32-
ORDER BY unique_words DESC
30+
SELECT
31+
CONCAT(
32+
'https://stackoverflow.com/questions/',
33+
CAST(id as STRING)) as url,
34+
view_count
35+
FROM `bigquery-public-data.stackoverflow.posts_questions`
36+
WHERE tags like '%google-bigquery%'
37+
ORDER BY view_count DESC
3338
LIMIT 10""")
3439

3540
results = query_job.result() # Waits for job to complete.
3641
# [END run_query]
3742

3843
# [START print_results]
3944
for row in results:
40-
print("{}: {}".format(row.title, row.unique_words))
45+
print("{} : {} views".format(row.url, row.view_count))
4146
# [END print_results]
4247

4348

4449
if __name__ == '__main__':
45-
query_shakespeare()
50+
query_stackoverflow()
4651
# [END all]

bigquery/cloud-client/simple_app_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import simple_app
1616

1717

18-
def test_query_shakespeare(capsys):
19-
simple_app.query_shakespeare()
18+
def test_query_stackoverflow(capsys):
19+
simple_app.query_stackoverflow()
2020
out, _ = capsys.readouterr()
21-
assert 'hamlet' in out
21+
assert 'views' in out

0 commit comments

Comments
 (0)
0