Gil Adirim
Gil Adirim

Reputation: 1934

Exception when trying to create bigquery table via python API

I'm working on an app that will stream events into BQ. Since Streamed Inserts require the table to pre-exist, I'm running the following code to check if the table exists, and then to create it if it doesn't:

TABLE_ID = "data" + single_date.strftime("%Y%m%d")
exists = False;

request  = bigquery.tables().list(projectId=PROJECT_ID,
                                        datasetId=DATASET_ID)
response = request.execute()

while response is not None:
    for t in response.get('tables', []):
        if t['tableReference']['tableId'] == TABLE_ID:
            exists = True
            break

    request = bigquery.tables().list_next(request, response)
    if request is None:
        break

if not exists:
    print("Creating Table " + TABLE_ID)
    dataset_ref = {'datasetId': DATASET_ID,
                   'projectId': PROJECT_ID}
    table_ref = {'tableId': TABLE_ID,
                 'datasetId': DATASET_ID,
                 'projectId': PROJECT_ID}
    schema_ref = SCHEMA
    table = {'tableReference': table_ref,
             'schema': schema_ref}
    table = bigquery.tables().insert(body=table, **dataset_ref).execute(http)

I'm running python 2.7, and have installed the google client API through PIP.

When I try to run the script, I get the following error:

No handlers could be found for logger "oauth2client.util"
Traceback (most recent call last):
  File "do_hourly.py", line 158, in <module>
    main()
  File "do_hourly.py", line 101, in main
    body=table, **dataset_ref).execute(http)
  File "build/bdist.linux-x86_64/egg/oauth2client/util.py", line 142, in positional_wrapper
  File "/usr/lib/python2.7/site-packages/googleapiclient/http.py", line 721, in execute
    resp, content = http.request(str(self.uri), method=str(self.method),
AttributeError: 'module' object has no attribute 'request'

I tried researching the issue, but all I could find was info about confusing between urllib, urllib2 and Python 2.7 / 3.

I'm not quite sure how to continue with this, and will appreciate all help.

Thanks!

Upvotes: 1

Views: 1232

Answers (1)

Gil Adirim
Gil Adirim

Reputation: 1934

Figured out that the issue was in the following line, which I took from another SO thread:

table = bigquery.tables().insert(body=table, **dataset_ref).execute(http)

Once I removed the "http" variable, which doesn't exist in my scope, the exception dissappeared

Upvotes: 1

Related Questions