我正在开发一个将事件流式传输到BQ的应用程序。由于流式插入需要表预先存在,我运行以下代码来检查表是否存在,然后如果不存在则创建它:
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)
我正在运行python 2.7,并通过PIP安装了谷歌客户端API。
当我尝试运行脚本时,出现以下错误:
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'
我尝试过研究这个问题,但我能找到的只是关于urllib,urllib2和Python 2.7 / 3之间混淆的信息。
我不太确定如何继续这一点,并将感谢所有帮助。
谢谢!
答案 0 :(得分:1)
想出问题出在以下一行,我从another SO线程中获取:
table = bigquery.tables().insert(body=table, **dataset_ref).execute(http)
一旦我删除了我的范围中不存在的“http”变量,异常就消失了