gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 16:02:18 25 4
gpt4 key购买 nike

我正在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/

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