forked from GoogleCloudPlatform/python-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
78 lines (64 loc) · 2.26 KB
/
main.py
File metadata and controls
78 lines (64 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# [START gae_python37_bigquery]
import concurrent.futures
import flask
from google.cloud import bigquery
app = flask.Flask(__name__)
bigquery_client = bigquery.Client()
@app.route("/")
def main():
query_job = bigquery_client.query(
"""
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
"""
)
return flask.redirect(
flask.url_for(
"results",
project_id=query_job.project,
job_id=query_job.job_id,
location=query_job.location,
)
)
@app.route("/results")
def results():
project_id = flask.request.args.get("project_id")
job_id = flask.request.args.get("job_id")
location = flask.request.args.get("location")
query_job = bigquery_client.get_job(
job_id,
project=project_id,
location=location,
)
try:
# Set a timeout because queries could take longer than one minute.
results = query_job.result(timeout=30)
except concurrent.futures.TimeoutError:
return flask.render_template("timeout.html", job_id=query_job.job_id)
return flask.render_template("query_result.html", results=results)
if __name__ == "__main__":
# This is used when running locally only. When deploying to Google App
# Engine, a webserver process such as Gunicorn will serve the app. This
# can be configured by adding an `entrypoint` to app.yaml.
app.run(host="127.0.0.1", port=8080, debug=True)
# [END gae_python37_bigquery]