gpt4 book ai didi

python - GAE BigQuery 在开发服务器上运行,但部署时出现 HTTP 400 错误

转载 作者:行者123 更新时间:2023-12-01 09:32:01 25 4
gpt4 key购买 nike

制作了一个 GAE 标准应用程序,将 .JSON 保存到 Google Cloud Storage,然后使用架构将 .JSON 加载到 BigQuery 中。通过 Google Cloud Shell 编写并运行它。

该脚本在开发服务器上启动时有效(保存的 .JSON,加载 BQ 表)。脚本在部署并访问 appspot URL 时出错。该错误是关于架构的......但它在开发上运行得很好。

部署并点击 appspot URL 时出错:

HttpError: https://www.googleapis.com/bigquery/v2/projects/api-gcs/jobs?alt=json returned "Required parameter: [resource.configuration.load.schema.field[0].name]">

代码:

import cloudstorage
from google.appengine.api import app_identity
from google.appengine.ext import blobstore
from google.appengine.ext.webapp import blobstore_handlers
import json
import sys
from collections import OrderedDict
import requests_toolbelt.adapters.appengine
requests_toolbelt.adapters.appengine.monkeypatch()
from oauth2client.client import GoogleCredentials
credentials=GoogleCredentials.get_application_default()
from googleapiclient.discovery import build
import os
import webapp2

# This handler creates a file in Cloud Storage using the cloudstorage
# client library and then reads the data back using the Blobstore API.
class CreateAndReadFileHandler(webapp2.RequestHandler):
def get(self):
dict_test = {'date': '2018-01-02', 'username': 'pasta456', 'age': 43, 'favorite_number': 1.22}

bucket = app_identity.get_default_gcs_bucket_name()

filename = '/{}/json_example.json'.format(bucket)

with cloudstorage.open(filename, 'w') as filehandle:
filehandle.write(json.dumps(dict_test))

blobstore_filename = '/gs{}'.format(filename)
blob_key = blobstore.create_gs_key(blobstore_filename)

data = blobstore.fetch_data(blob_key, 0, 6)

PROJECT = os.environ['PROJECT']
BUCKET = os.environ['BUCKET']
DATASET = os.environ['DATASET']

service = build("bigquery", "v2", credentials = credentials)

job = {
"configuration": {
"load": {
"sourceUris": ["gs://XXX.appspot.com/json_example.json"],
"schema": {
"fields" : [
{"name": "date",
"type": "DATE"},
{"name": "username",
"type": "STRING"},
{"name": "age",
"type": "INTEGER"},
{"name": "favorite_number",
"type": "FLOAT"}
]
},
"destinationTable": {
"projectId": PROJECT,
"datasetId": DATASET,
"tableId": "json_test2"
},
"sourceFormat" : "NEWLINE_DELIMITED_JSON",
"createDisposition": "CREATE_IF_NEEDED"
}
}
}


response = service.jobs().insert(
projectId = PROJECT,
body = job
).execute()

最佳答案

答案:解决方案是架构的 JSON 格式。我仍然不知道为什么所讨论的模式格式可以在开发中工作,但在部署时却不能。下面是适用于部署的模式格式。

            "schema": {
"fields" : [
{"name": "date2", "type": "DATE"},
{"name": "username", "type": "STRING"},
{"name": "age", "type": "INTEGER"},
{"name": "favorite_number", "type": "FLOAT"}
]
}

关于python - GAE BigQuery 在开发服务器上运行,但部署时出现 HTTP 400 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49881215/

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