File tree Expand file tree Collapse file tree 3 files changed +19
-12
lines changed Expand file tree Collapse file tree 3 files changed +19
-12
lines changed Original file line number Diff line number Diff line change
1
+ # [START bigquery_simple_app_pkgs]
1
2
google-cloud-bigquery == 0.28.0
3
+ # [END bigquery_simple_app_pkgs]
2
4
google-auth-oauthlib == 0.2.0
3
5
pytz == 2017.3
Original file line number Diff line number Diff line change 16
16
17
17
"""Simple application that performs a query with BigQuery."""
18
18
# [START all]
19
- # [START create_client ]
19
+ # [START bigquery_simple_app_deps ]
20
20
from google .cloud import bigquery
21
+ # [END bigquery_simple_app_deps]
21
22
22
23
23
- def query_shakespeare ():
24
+ def query_stackoverflow ():
25
+ # [START create_client]
24
26
client = bigquery .Client ()
25
27
# [END create_client]
26
28
# [START run_query]
27
29
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
33
38
LIMIT 10""" )
34
39
35
40
results = query_job .result () # Waits for job to complete.
36
41
# [END run_query]
37
42
38
43
# [START print_results]
39
44
for row in results :
40
- print ("{}: {}" .format (row .title , row .unique_words ))
45
+ print ("{} : {} views " .format (row .url , row .view_count ))
41
46
# [END print_results]
42
47
43
48
44
49
if __name__ == '__main__' :
45
- query_shakespeare ()
50
+ query_stackoverflow ()
46
51
# [END all]
Original file line number Diff line number Diff line change 15
15
import simple_app
16
16
17
17
18
- def test_query_shakespeare (capsys ):
19
- simple_app .query_shakespeare ()
18
+ def test_query_stackoverflow (capsys ):
19
+ simple_app .query_stackoverflow ()
20
20
out , _ = capsys .readouterr ()
21
- assert 'hamlet ' in out
21
+ assert 'views ' in out
You can’t perform that action at this time.
0 commit comments