npohle
npohle

Reputation: 71

Missing parameters when creating new table in Google BigQuery through Python API V2

I'm trying to create new table using BigQuery's 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()

The error I'm getting is:

(<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>)

I think all required parameters are included as far as this is documented at https://developers.google.com/resources/api-libraries/documentation/bigquery/v2/python/latest/bigquery_v2.tables.html#insert

What's missing?

Upvotes: 1

Views: 2578

Answers (2)

Jordan Tigani
Jordan Tigani

Reputation: 26617

The only required parameter for a tables.insert is the tableReference, which must have tableId, datasetId, and projectId fields. I think the actual issue may be that you're passing the JSON string when you could just pass a dict with the values. For instance, the following code works to create a table (note the dataset_ref is a Python trick to copy the contents to named arguments):

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)

Upvotes: 3

HectorIP
HectorIP

Reputation: 36

Maybe too late, but the thing is that the body parameter must be dictionary instead of a string.

Upvotes: 1

Related Questions