15
15
# limitations under the License.
16
16
17
17
"""Simple application that performs a query with BigQuery."""
18
+
18
19
# [START bigquery_simple_app_all]
20
+ import argparse
21
+
19
22
# [START bigquery_simple_app_deps]
23
+ import google_auth_oauthlib
20
24
from google .cloud import bigquery
21
25
# [END bigquery_simple_app_deps]
22
26
23
27
24
- def query_stackoverflow ():
28
+ def authenticate (client_id , client_secret ):
29
+ # [START bigquery_simple_app_authenticate]
30
+ # TODO(developer): Set the client ID and client secret to the values
31
+ # identifying your application.
32
+ # client_id = "YOUR-CLIENT-ID.apps.googleusercontent.com"
33
+ # client_secret = "abc_ThIsIsAsEcReT"
34
+
35
+ # This function opens a browser window to authenticate to your Google Cloud
36
+ # Platform account. See the Google Cloud Platform Auth Guide for how to
37
+ # authenticate your application in non-interactive contexts:
38
+ # https://cloud.google.com/docs/authentication/
39
+ credentials = google_auth_oauthlib .get_user_credentials (
40
+ ["https://www.googleapis.com/auth/cloud-platform" ],
41
+ client_id ,
42
+ client_secret ,
43
+ )
44
+ # [END bigquery_simple_app_authenticate]
45
+ return credentials
46
+
47
+
48
+ def query_stackoverflow (credentials , project_id ):
25
49
# [START bigquery_simple_app_client]
26
- client = bigquery .Client ()
50
+ # TODO(developer): Set the project ID to the project used for billing.
51
+ # project_id = "my-billing-project-id"
52
+
53
+ client = bigquery .Client (credentials = credentials , project = project_id )
27
54
# [END bigquery_simple_app_client]
28
55
# [START bigquery_simple_app_query]
29
56
query_job = client .query ("""
@@ -41,11 +68,22 @@ def query_stackoverflow():
41
68
# [END bigquery_simple_app_query]
42
69
43
70
# [START bigquery_simple_app_print]
44
- for row in results :
45
- print ("{} : {} views" .format (row .url , row .view_count ))
71
+ # Download the full query results as a pandas DataFrame.
72
+ df = results .to_dataframe ()
73
+ print (df )
46
74
# [END bigquery_simple_app_print]
47
75
48
76
77
+ def main (project_id , client_id , client_secret ):
78
+ credentials = authenticate (client_id , client_secret )
79
+ query_stackoverflow (credentials , project_id )
80
+
81
+
49
82
if __name__ == '__main__' :
50
- query_stackoverflow ()
83
+ parser = argparse .ArgumentParser ("Sample BigQuery Application" )
84
+ parser .add_argument ("project_id" )
85
+ parser .add_argument ("client_id" )
86
+ parser .add_argument ("client_secret" )
87
+ args = parser .parse_args ()
88
+ main (args .project_id , args .client_id , args .client_secret )
51
89
# [END bigquery_simple_app_all]
0 commit comments