gpt4 book ai didi

python-2.7 - Google App Engine 的 BigQuery cron 作业凭据

转载 作者:太空宇宙 更新时间:2023-11-03 15:25:24 24 4
gpt4 key购买 nike

更新:我已经缩小了问题的范围,所以我删除了不必要的代码和示例:

更新 2:在让 cron 作业每隔 12 小时运行一段时间后(每次都成功结束,但 BQ 中没有任何内容)我们震惊地发现,大约一周后,其中一个 cron jobs 确实成功地写入了 BigQuery,同时 Stackdriver 日志指出“此请求导致为您的应用程序启动一个新进程(...)”,如下所示。以下作业再次停止写入。现在我想知道这是否以某种方式连接到缓存的应用程序状态(有一些有效期)或凭据到期日期以某种方式阻止在第一次之后进一步写入 BigQuery,但不会导致错误。

问题描述:

我正在尝试在 App Engine(标准)中设置一个 cron 作业以从 BigQuery 查询数据并将数据写回 BigQuery(数据集与已部署的应用程序位于同一项目中)并且 cron 作业成功执行但仅写入BigQuery 在部署后第一次执行,之后仍然执行成功但不写入。

我发现的主要区别是在 Stackdriver 日志中,对于正确写入的执行有额外的调试和信息,对于后续的执行没有这样的消息:

2018-04-19 04:44:03.933 CEST
Converted retries value: 3 -> Retry(total=3, connect=None, read=None, redirect=None, status=None) (/base/data/home/apps/e~<redacted>/lib/urllib3/util/retry.py:200)
2018-04-19 04:44:04.154 CEST
Making request: POST https://accounts.google.com/o/oauth2/token (/base/data/home/apps/e~<redacted>/lib/google/auth/transport/requests.py:117)
2018-04-19 04:44:04.160 CEST
Starting new HTTPS connection (1): accounts.google.com (/base/data/home/apps/e~<redacted>/lib/urllib3/connectionpool.py:824)
2018-04-19 04:44:04.329 CEST
https://accounts.google.com:443 "POST /o/oauth2/token HTTP/1.1" 200 None (/base/data/home/apps/e~<redacted>/lib/urllib3/connectionpool.py:396)
2018-04-19 04:44:04.339 CEST
Starting new HTTPS connection (1): www.googleapis.com (/base/data/home/apps/e~<redacted>/lib/urllib3/connectionpool.py:824)
2018-04-19 04:44:04.802 CEST
https://www.googleapis.com:443 "POST /bigquery/v2/projects/<redacted>/jobs HTTP/1.1" 200 None (/base/data/home/apps/e~<redacted>/lib/urllib3/connectionpool.py:396)
2018-04-19 04:44:04.813 CEST
This request caused a new process to be started for your application, and thus caused your application code to be loaded for the first time. This request may thus take longer and use more CPU than a typical request for your application.

我试过:

  • 为默认应用引擎服务帐户添加 BigQuery DataOwner 和 User 权限,但没有效果。

  • 有人提到标准应用引擎不完全支持 google.cloud 库,所以我尝试使用 OAuth2/httplib2/googleapiclient 凭据进行身份验证,但这是我第一次尝试,但我没有不明白如何将各个部分组合在一起,没有 google.cloud 库,我什至不知道如何为 BQ 编写正确的查询

  • 下面建议的其他凭证设置方法,但似乎连接到 BQ 不是问题,它们都连接并写入(一次),只是在已部署的应用引擎中重复它。

下面是完整的实现:

应用程序.yaml:

runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /bigquerycron
script: bigquerycron.app
login: admin

libraries:
- name: ssl
version: latest

env_variables:
GAE_USE_SOCKETS_HTTPLIB : 'true'

大查询cron.py

from __future__ import absolute_import
from google.cloud import bigquery
import webapp2

class MainPage(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('CRON test page')

def writeDataTest(dataset_id = '<redacted>',table_id='<redacted>'):
client = bigquery.Client.from_service_account_json("credentials.json")
job_config = bigquery.QueryJobConfig()
table_ref = client.dataset(dataset_id).table(table_id)
job_config.destination = table_ref
job_config.write_disposition = 'WRITE_APPEND'

query_job = client.query(
"""SELECT CURRENT_DATETIME() AS Datetime, 'CRON' as Source""", job_config=job_config)

writeDataTest()

app = webapp2.WSGIApplication([
('/bigquerycron', MainPage),
], debug=True)

cron.yaml:

cron:
- url: /bigquerycron
schedule: every 30 minutes

最佳答案

在此特定情况下,凭据不是问题,问题只是由于对 App Engine 工作方式的误解而导致函数调用的放置。 bigquery 的函数调用应该移动到 MainPage 类定义中,固定的 bigquerycron.py 看起来像这样(只移动了一行代码):

from __future__ import absolute_import
from google.cloud import bigquery
import webapp2

class MainPage(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('CRON test page')
writeDataTest()

def writeDataTest(dataset_id = '<redacted>',table_id='<redacted>'):
client = bigquery.Client.from_service_account_json("credentials.json")
job_config = bigquery.QueryJobConfig()
table_ref = client.dataset(dataset_id).table(table_id)
job_config.destination = table_ref
job_config.write_disposition = 'WRITE_APPEND'
query_job = client.query(
"""SELECT CURRENT_DATETIME() AS Datetime, 'CRON' as Source""", job_config=job_config)

app = webapp2.WSGIApplication([
('/bigquerycron', MainPage),
], debug=True)

OP 中的版本确实只向 BigQuery 写入一次,当第一次加载 App Engine 应用程序时,所有后续调用都只执行 MainPage 类,在这种情况下它什么都不做,因为实际的 BigQuery 代码在它之外。

此外,在不使用 GAE 标准 (https://github.com/GoogleCloudPlatform/google-cloud-python/issues/1893) 不支持的 google-cloud-python 库的情况下重写应用程序将是有益的。这尤其不幸,因为即使是 python 的官方 bigquery 文档(https://cloud.google.com/bigquery/docs/)也使用了这个库。但是,有多种解决方法可以继续使用它,包括链接的 github 问题中提到的一些方法以及此处: Using gcloud-python in GAE并且此示例中使用了类似的解决方法。

但如前所述,最好使用适用于 Python 的专用 Google API 客户端库: https://developers.google.com/api-client-library/python/

关于python-2.7 - Google App Engine 的 BigQuery cron 作业凭据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49812689/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com