我正在尝试使用BigQuery的Python API创建新表:
bigquery.tables().insert(
projectId="xxxxxxxxxxxxxx",
datasetId="xxxxxxxxxxxxxx",
body='{
"tableReference": {
"projectId":"xxxxxxxxxxxxxx",
"tableId":"xxxxxxxxxxxxxx",
"datasetId":"accesslog"},
"schema": {
"fields": [
{"type":"STRING", "name":"ip"},
{"type":"TIMESTAMP", "name":"ts"},
{"type":"STRING", "name":"event"},
{"type":"STRING", "name":"id"},
{"type":"STRING","name":"sh"},
{"type":"STRING", "name":"pub"},
{"type":"STRING", "name":"context"},
{"type":"STRING", "name":"brand"},
{"type":"STRING", "name":"product"}
]
}
}'
).execute()
我得到的错误是:
(<class 'apiclient.errors.HttpError'>, <HttpError 400 when requesting https://www.googleapis.com/bigquery/v2/projects/xxxxxxxxxxxxxx/datasets/xxxxxxxxxxxxxx/tables?alt=json returned "Required parameter is missing">, <traceback object at 0x17e1c20>)
我认为所有必需的参数都包括在https://developers.google.com/resources/api-libraries/documentation/bigquery/v2/python/latest/bigquery_v2.tables.html#insert
中缺少什么?
答案 0 :(得分:2)
tables.insert
唯一必需的参数是tableReference
,其必须包含tableId
,datasetId
和projectId
字段。我认为实际的问题可能是你传递JSON字符串时只能传递dict
值。例如,以下代码用于创建表(请注意dataset_ref
是将内容复制到命名参数的Python技巧):
project_id = <my project>
dataset_id = <my dataset>
table_id = 'table_001'
dataset_ref = {'datasetId': dataset_id,
'projectId': project_id}
table_ref = {'tableId': table_id,
'datasetId': dataset_id,
'projectId': project_id}
table = {'tableReference': table_ref}
table = bigquery.tables().insert(
body=table, **dataset_ref).execute(http)
答案 1 :(得分:1)
可能为时已晚,但问题是body
参数必须是字典而不是字符串。