0

我正在尝试使用 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中

少了什么东西?

4

2 回答 2

2

a 的唯一必需参数tables.inserttableReference,它必须具有tableIddatasetIdprojectId字段。我认为实际的问题可能是当您可以传递dict带有值的 a 时,您正在传递 JSON 字符串。例如,下面的代码用于创建一个表(注意这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)
于 2013-09-28T18:05:42.460 回答
1

也许为时已晚,但问题是body参数必须是字典而不是字符串。

于 2016-05-12T17:01:48.563 回答