使用BigQuery API客户端库时,无法访问网络

时间:2018-01-02 10:59:02

标签: python google-bigquery google-compute-engine

我正在使用Google Compute Engine中的BigQuery API client library for Python。在进行查询时会抛出网络无法访问的错误。

[INFO:2018-01-02 16:16:04,887:oauth2client.transport] Attempting refresh to obtain initial access_token
[INFO:2018-01-02 16:16:04,924:oauth2client.client] Refreshing access_token
Traceback (most recent call last):
  File "/test/data/reports/ga_bigquery.py", line 130, in ga_table_string
    if bq_dataset.table("ga_sessions_{}".format(str_date)).exists():
  File "/test/data/reports/.venv/lib/python2.7/site-packages/gcloud/bigquery/table.py", line 472, in exists
    query_params={'fields': 'id'})
  File "/test/data/reports/.venv/lib/python2.7/site-packages/gcloud/connection.py", line 343, in api_request
    target_object=_target_object)
  File "/test/data/reports/.venv/lib/python2.7/site-packages/gcloud/connection.py", line 241, in _make_request
    return self._do_request(method, url, headers, data, target_object)
  File "/test/data/reports/.venv/lib/python2.7/site-packages/gcloud/connection.py", line 270, in _do_request
    body=data)
  File "/test/data/reports/.venv/lib/python2.7/site-packages/oauth2client/transport.py", line 153, in new_request
    credentials._refresh(orig_request_method)
  File "/test/data/reports/.venv/lib/python2.7/site-packages/oauth2client/client.py", line 765, in _refresh
    self._do_refresh_request(http_request)
  File "/test/data/reports/.venv/lib/python2.7/site-packages/oauth2client/client.py", line 797, in _do_refresh_request
    self.token_uri, method='POST', body=body, headers=headers)
  File "/test/data/reports/.venv/lib/python2.7/site-packages/httplib2/__init__.py", line 1609, in request
    (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
  File "/test/data/reports/.venv/lib/python2.7/site-packages/httplib2/__init__.py", line 1351, in _request
    (response, content) = self._conn_request(conn, request_uri, method, body, headers)
  File "/test/data/reports/.venv/lib/python2.7/site-packages/httplib2/__init__.py", line 1272, in _conn_request
    conn.connect()
  File "/test/data/reports/.venv/lib/python2.7/site-packages/httplib2/__init__.py", line 1075, in connect
    raise socket.error, msg
socket.error: [Errno 101] Network is unreachable

可能是什么原因?

我使用以下选项运行代码:

[root@myserver]# strace -ff -e poll,select,connect,recvfrom,sendto python run.py --date=20180102 >> strace.log

[root@myserver]# cat strace.log | grep unreachable
connect(3, {sa_family=AF_INET6, sin6_port=htons(443), inet_pton(AF_INET6, “2404:6800:4003:c02::54”, &sin6_addr), sin6_flowinfo=0, sin6_scope_id=0}, 28) = -1 ENETUNREACH (Network isunreachable)
connect(4, {sa_family=AF_INET6, sin6_port=htons(443), inet_pton(AF_INET6, “2404:6800:4003:803::200a”, &sin6_addr), sin6_flowinfo=0, sin6_scope_id=0}, 28) = -1 ENETUNREACH (Network is unreachable)
connect(5, {sa_family=AF_INET6, sin6_port=htons(443), inet_pton(AF_INET6, “2404:6800:4003:808::200d”, &sin6_addr), sin6_flowinfo=0, sin6_scope_id=0}, 28) = -1 ENETUNREACH (Network is unreachable)
connect(6, {sa_family=AF_INET6, sin6_port=htons(443), inet_pton(AF_INET6, “2404:6800:4003:c03::5f”, &sin6_addr), sin6_flowinfo=0, sin6_scope_id=0}, 28) = -1 ENETUNREACH (Network isunreachable)

从日志看起来计算实例阻止了传出的ipv6请求。 我们有什么方法可以在谷歌计算引擎中解除ipv6请求吗?

1 个答案:

答案 0 :(得分:1)

首先,如果您使用的是BigQuery API Client Library for Python,则可能需要尝试使用BigQuery Cloud Client Library for Python

这样做的一个原因可能是 API 客户端库使用httplib2向具有problems in some network setups such as behind proxies的BigQuery发出请求。 Cloud 库使用更标准的requests库,因此它应该更可靠。

其次,即使您没有切换库,也应该更改正在使用的身份验证库。您的堆栈跟踪显示oauth2client,但oauth2client is deprecated。使用google-auth-httplib2将API客户端库与google-auth库一起使用。