【发布时间】:2020-07-28 20:39:02
【问题描述】:
当我尝试仅使用库通过 appengine 执行我的 script.py 时遇到问题 “从 google.cloud 导入 bigquery”,我尝试了一些解决方案,但我没有成功的方法,我向您展示了我用来执行此脚本的脚本。
这是我的脚本:
import json
import datetime
import webapp2
from google.cloud import bigquery
from google.appengine.ext import vendor
vendor.add('lib')
filename = "example.json"
def date_format(time):
if time.find(".") != -1:
time = time[:time.find(".")]
date = datetime.datetime.strptime(time,"%Y-%m-%d %H:%M:%S")
return date.strftime("%Y%m%d")
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
with open(filename) as file:
array = []
jsonData = json.load(file)
d = str(date_format(jsonData['e']))
self.response.write('Hello AppEngine from script! :: '+d)
client = bigquery.Client()
QUERY = (
'SELECT field FROM `table` WHERE date='+d+' LIMIT 10'
)
query_job = client.query(QUERY)
rows = query_job.result()
for row in rows:
print(row.field)
app = webapp2.WSGIApplication([
('/', MainPage),
], debug=True)
这是我的 app.yaml:
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /.*
script: main.app
这是我的 requirements.txt:
google-api-python-client
google-cloud
我有一个包含 bigquery 库的 lib 目录:google_cloud_bigquery-1.24.0.dist-info。 和其他带有 google_cloud 的库
我不知道我的代码是否正确,因为我看到了一些解决方案,但任何东西都可以帮助我使用 appengine 运行脚本。
希望你能帮帮我。
这是python 3的脚本
import google.cloud.bigquery as bigquery
# [START gae_python37_app]
from flask import Flask
from google.appengine.ext import vendor
vendor.add('lib')
# If `entrypoint` is not defined in app.yaml, App Engine will look for an app
# called `app` in `main.py`.
app = Flask(__name__)
@app.route('/')
def hello():
"""Return a friendly HTTP greeting."""
client = bigquery.Client()
QUERY = (
'SELECT ev FROM `table` WHERE f="20200201" LIMIT 10'
)
query_job = client.query(QUERY)
rows = query_job.result()
for row in rows:
return row.ev
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_app]
【问题讨论】:
标签: python google-app-engine google-cloud-platform google-bigquery google-app-engine-python