python-2.7 - 在Python中使用google.cloud.bigquery写入bigQuery时,必填参数缺少错误

标签 python-2.7 google-analytics google-cloud-platform google-bigquery google-analytics-api

我正在Python 2.7中使用以下代码段将新行分隔JSON加载到bigQuery:

from google.cloud import bigquery
from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials

bigquery_client = bigquery.Client()
dataset = bigquery_client.dataset('testGAData')
table_ref = dataset.table('gaData')
table = bigquery.Table(table_ref)

with open('gaData.json', 'rb') as source_file:
    job_config = bigquery.LoadJobConfig()
    job_config.source_format = 'NEWLINE_DELIMITED_JSON'
    job = bigquery_client.load_table_from_file(
        source_file, table, job_config=job_config)


它返回以下错误:

File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/google/cloud/bigquery/client.py", line 897, in load_table_from_file
    raise exceptions.from_http_response(exc.response)
google.api_core.exceptions.BadRequest: 400 POST https://www.googleapis.com/upload/bigquery/v2/projects/test-project-for-experiments/jobs?uploadType=resumable: Required parameter is missing


为什么会出现此错误?我怎样才能解决这个问题?还有其他人遇到过类似的问题吗?提前致谢。
编辑:添加了最后一个段落,包括python导入并更正了缩进。

最佳答案

初始代码中发现的问题


您缺少表的架构。您可以使用job_config.autodetect = Truejob_config.schema = [bigquery.SchemaField("FIELD NAME", "FIELD TYPE")]
从文档中,您应该将job_config.source_format = `bigquery.SourceFormat.NEWLINE_DELIMITED_JSON`设置为JSON文件源
您应该将table_ref变量作为参数传递,而不是table中的bigquery_client.load_table_from_file(source_file, table, job_config=job_config)变量


Link到文档

工作守则

下面的代码对我有用。我正在使用python 3和google-cloud-bigquery v1.5

from google.cloud import bigquery

client = bigquery.Client()
dataset_id, table_id = "TEST_DATASET", "TEST_TABLE"
data_ref = client.dataset(dataset_id)
table_ref = data_ref.table(table_id)
file_path = "path/to/test.json"

job_config = bigquery.LoadJobConfig()
job_config.source_format = bigquery.SourceFormat.NEWLINE_DELIMITED_JSON
#job_config.autodetect = True
job_config.schema = [bigquery.SchemaField("Name", "STRING"), bigquery.SchemaField("Age", "INTEGER")]

with open(file_path, 'rb') as source_file:
    job = client.load_table_from_file(source_file, table_ref, location='US', job_config=job_config)

job.result()

print('Loaded {} rows into {}:{}.'.format(job.output_rows, dataset_id, table_id))


输出量

>> Loaded 2 rows into TEST_DATASET:TEST_TABLE.

关于python-2.7 - 在Python中使用google.cloud.bigquery写入bigQuery时,必填参数缺少错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51942492/

相关文章:

google-analytics - 为什么谷歌分析显示未知(未设置)位置?

google-cloud-platform - 无法使用 Google 的 Gcloud Bazel 构建器并行构建

python - 段落的标题大小写

iframe - iframe中加载的内容会影响Google Analytics(分析)“页面加载时间”吗?

python - 我试图了解如何与多处理共享只读对象

google-analytics - 您认为哪些 Google Analytics 信息最有用?

javascript - 使用 Google Home 或 Alexa 的 TTS

go - 将多个文件传递给存储桶中的 exec.Command 调用

python - 如何让panda3d更快接受控件?

python-2.7 - SSL证书下载