1

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?

2 Answers 2

3

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)
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.