【发布时间】: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