【问题标题】:GAE BigQuery works on dev server, but HTTP 400 error when deployedGAE BigQuery 在开发服务器上工作,但部署时出现 HTTP 400 错误
【发布时间】:2018-09-27 14:30:04
【问题描述】:

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

脚本在开发服务器上启动时有效(保存 .JSON,加载 BQ 表)。部署并点击 apppot URL 时出现脚本错误。错误是在谈论架构......但它在开发人员上工作得很好。

部署和点击 apppot URL 时出错:

HttpError:https://www.googleapis.com/bigquery/v2/projects/api-gcs/jobs?alt=json 返回“必需参数:[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()

【问题讨论】:

    标签: python google-app-engine google-cloud-platform google-bigquery


    【解决方案1】:

    答案:修复是架构的 JSON 格式。我仍然不知道为什么有问题的模式格式可以在 dev 上工作,但在部署时却不行。以下是部署时的架构格式。

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

    【讨论】:

    • 我不认为修复正在格式化架构,因为它与发布的原始架构本质上完全相同。错误(“必需参数:[resource.configuration.load.schema.field[0].name]">),与这部分代码有关:{“name”:“date”,“type”:“DATE "},并且这不起作用是有道理的,因为 'date' 是一个保留的 SQL 字(我知道它在 dev 中有效,但这只是一个模拟环境)。因此,在部署后将架构中的“日期”更改为“日期2”是有道理的。
    【解决方案2】:

    这并不完全是一个答案(在此处添加屏幕截图更容易)。

    我只是想说这似乎不是 BigQuery 问题。使用 API explorer bigquery.jobs.insert 方法,我能够使用与原始代码中完全相同的请求插入作业。

    【讨论】:

      猜你喜欢
      • 2017-12-19
      • 2013-01-01
      • 2018-05-17
      • 2013-03-28
      • 1970-01-01
      • 2018-10-22
      • 1970-01-01
      • 2017-02-21
      • 2020-03-07
      相关资源
      最近更新 更多