Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过Python API V2在Google BigQuery中创建新表时缺少参数_Python_Google Bigquery - Fatal编程技术网

通过Python API V2在Google BigQuery中创建新表时缺少参数

通过Python API V2在Google BigQuery中创建新表时缺少参数,python,google-bigquery,Python,Google Bigquery,我正在尝试使用BigQuery的Python API创建新表: bigquery.tables().insert( projectId="xxxxxxxxxxxxxx", datasetId="xxxxxxxxxxxxxx", body='{ "tableReference": { "projectId":"xxxxxxxxxxxxxx", "tableId":"xxxxxxxxxxxxxx",

我正在尝试使用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>)
(,)
我认为所有要求的参数都包含在文件中


缺少什么?

表的唯一必需参数。insert是
表引用
,它必须具有
表ID
数据集ID
项目ID
字段。我认为实际的问题可能是,在传递JSON字符串时,您可以传递一个带有值的
dict
。例如,以下代码用于创建一个表(注意,
dataset\u ref
是一个Python技巧,用于将内容复制到命名参数):

project\u id=
数据集\u id=
table_id='table_001'
dataset_ref={'datasetId':dataset_id,
“projectId”:项目\u id}
table_ref={'tableId':table_id,
“datasetId”:数据集\u id,
“projectId”:项目\u id}
table={'tableReference':table_ref}
table=bigquery.tables().insert(
body=table,**dataset\u ref).execute(http)

表格的唯一必需参数。插入是
表格引用
,它必须具有
表格ID
数据集ID
项目ID
字段。我认为实际的问题可能是,在传递JSON字符串时,您可以传递一个带有值的
dict
。例如,以下代码用于创建一个表(注意,
dataset\u ref
是一个Python技巧,用于将内容复制到命名参数):

project\u id=
数据集\u id=
table_id='table_001'
dataset_ref={'datasetId':dataset_id,
“projectId”:项目\u id}
table_ref={'tableId':table_id,
“datasetId”:数据集\u id,
“projectId”:项目\u id}
table={'tableReference':table_ref}
table=bigquery.tables().insert(
body=table,**dataset\u ref).execute(http)

可能太晚了,但问题是
主体
参数必须是字典而不是字符串。

可能太晚了,但问题是
主体
参数必须是字典而不是字符串

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)