【问题标题】:Accessing Big Query from Cloud DataLab using Pandas使用 Pandas 从 Cloud DataLab 访问大查询
【发布时间】:2016-10-14 01:07:04
【问题描述】:

我有一个使用 Pandas 作为工具访问 Big Query 的 Jypyter Notebook:

df = pd.io.gbq.read_gbq( query, project_id = 'xxxxxxx-xxxx' )

这在我的本地机器上运行良好! (太好了,事实上!) 但是,当我将同一个笔记本加载到 Cloud DataLab 时,我得到:

DistributionNotFound: google-api-python-client

这似乎相当令人失望!我相信该模块应该与 Pandas 一起安装。但不知何故谷歌不包括它? 出于多种原因,最好不必将我们在本地机器上开发的代码更改为 Cloud DataLab 中需要的代码,在这种情况下,我们会大量参数化数据访问......

好的,我跑了:

!pip install --upgrade google-api-python-client

现在,当我运行笔记本时,我收到一个身份验证提示,由于 DataLab 位于远程计算机上,因此我无法解决:

Your browser has been opened to visit:
 >>> Browser string>>>>
If your browser is on a different machine then exit and re-run this
application with the command-line parameter 

 --noauth_local_webserver

对此没有明显的答案?

在更新笔记本中的 google-api-python-client 后,我​​在同一笔记本中使用@Anthonios Partheniou 建议的以下代码(在单元块中执行它) 我得到了以下回溯:

TypeError                                 Traceback (most recent call last)
<ipython-input-3-038366843e56> in <module>()
  5                            scope='https://www.googleapis.com/auth/bigquery',
  6                            redirect_uri='urn:ietf:wg:oauth:2.0:oob')
----> 7 storage = Storage('bigquery_credentials.dat')
  8 authorize_url = flow.step1_get_authorize_url()
  9 print 'Go to the following link in your browser: ' + authorize_url

/usr/local/lib/python2.7/dist-packages/oauth2client/file.pyc in __init__(self, filename)
 37 
 38     def __init__(self, filename):
---> 39         super(Storage, self).__init__(lock=threading.Lock())
 40         self._filename = filename
 41 

 TypeError: object.__init__() takes no parameters

他提到需要从同一个文件夹中执行笔记本,但我知道执行数据实验室笔记本的唯一方法是通过 repo?

虽然使用新 Jupyter Datalab 模块的新模块是一种可能的替代方案,但在本地和 DataLab 实例上使用完整的 Pandas BQ 界面的能力将非常有帮助!因此,请尽情寻找解决方案!

pip installed:
GCPDataLab 0.1.0
GCPData 0.1.0
wheel 0.29.0
tensorflow 0.6.0
protobuf 3.0.0a3
oauth2client 1.4.12
futures 3.0.3
pexpect 4.0.1
terminado 0.6
pyasn1 0.1.9
jsonschema 2.5.1
mistune 0.7.2
statsmodels 0.6.1
path.py 8.1.2
ipython 4.1.2
nose 1.3.7
MarkupSafe 0.23
py-dateutil 2.2
pyparsing 2.1.1
pickleshare 0.6
pandas 0.18.0
singledispatch 3.4.0.3
PyYAML 3.11
nbformat 4.0.1
certifi 2016.2.28
notebook 4.0.2
cycler 0.10.0
scipy 0.17.0
ipython-genutils 0.1.0
pyasn1-modules 0.0.8
functools32 3.2.3-2
ipykernel 4.3.1
pandocfilters 1.2.4
decorator 4.0.9
jupyter-core 4.1.0
rsa 3.4.2
mock 1.3.0
httplib2 0.9.2
pytz 2016.3
sympy 0.7.6
numpy 1.11.0
seaborn 0.6.0
pbr 1.8.1
backports.ssl-match-hostname 3.5.0.1
ggplot 0.6.5
simplegeneric 0.8.1
ptyprocess 0.5.1
funcsigs 0.4
scikit-learn 0.16.1
traitlets 4.2.1
jupyter-client 4.2.2
nbconvert 4.1.0
matplotlib 1.5.1
patsy 0.4.1
tornado 4.3
python-dateutil 2.5.2
Jinja2 2.8
backports-abc 0.4
brewer2mpl 1.4.1
Pygments 2.1.3

结束

【问题讨论】:

  • 我无法产生异常。您能否运行以下命令并使用 oauth2client 版本进行回复?运行import oauth2client,然后运行oauth2client.__version__
  • 同时运行import pip,然后运行for dist in pip.get_installed_distributions(): print dist
  • 导入 oauth2client 2 oauth2client.__version__ '1.4.12'
  • 上面的Pip安装列表
  • 我使用datalab.cloud.google.com 部署了一个干净的datalab 实例,并且我在答案中提供的代码有效。我注意到的一件事是,在我已安装的 python 模块列表中,我看到了 google-api-python-client 1.5.1 ,但在您提供的输出中我没有看到 google-api-python-client

标签: python pandas google-bigquery google-cloud-datalab


【解决方案1】:

pandas 中的 Google BigQuery 身份验证通常是直截了当的,除非在远程服务器上执行 pandas 代码。例如,在云中的 Datalab 上运行 pandas。在这种情况下,请使用以下代码创建 pandas 在 Google Datalab 中访问 Google BigQuery 所需的凭据文件。

from oauth2client.client import OAuth2WebServerFlow
from oauth2client.file import Storage
flow = OAuth2WebServerFlow(client_id='<Client ID from Google API Console>',
                           client_secret='<Client secret from Google API Console>',
                           scope='https://www.googleapis.com/auth/bigquery',
                           redirect_uri='urn:ietf:wg:oauth:2.0:oob')
storage = Storage('bigquery_credentials.dat')
authorize_url = flow.step1_get_authorize_url()
print 'Go to the following link in your browser: ' + authorize_url
code = raw_input('Enter verification code: ')
credentials = flow.step2_exchange(code)
storage.put(credentials)

一旦您完成该过程,我预计您不会看到错误(只要笔记本与新创建的 'bigquery_credentials.dat' 文件位于同一文件夹中)。

您还需要安装 google-api-python-client python 包,因为它是 required by pandas 以获得 Google BigQuery 支持。您可以在笔记本中运行以下任一命令来安装它。

要么

!pip install google-api-python-client --no-deps
!pip install uritemplate --no-deps
!pip install simplejson --no-deps

%%bash
pip install google-api-python-client --no-deps
pip install uritemplate --no-deps
pip install simplejson --no-deps

--no-deps 选项是必需的,这样您就不会意外更新默认安装在 datalab 中的 python 包(以确保 datalab 的其他部分不会损坏)。

注意:使用 pandas 0.19.0(尚未发布),在 Google Cloud Datalab 中使用 pandas 会容易得多。见Pull Request #13608

注意:您还可以选择在 jupyter 中使用(新的)google datalab 模块(这样代码也可以在云端的 Google Datalab 中运行)。请参阅以下相关的堆栈溢出答案: How do I use gcp package from outside of google datalabs?

【讨论】:

  • 刚试了一下没有运气,更新了底部的帖子,并带有回溯任何帮助表示赞赏!
  • 更近了!我完全重新启动了实例并运行了 pip 安装,然后我得到了获取验证码的提示,但是链接给了我一个 404 错误:invalid_client 找不到 OAuth 客户端。请求详细信息 access_type=offline scope=googleapis.com/auth/bigquery response_type=code redirect_uri=urn:ietf:wg:oauth:2.0:oob client_id= 我注意到我在部分云上遇到了一些问题,比如 repo和 BQ 并且必须使用隐身模式,因为它不会尊重 Chrome 上的多用户?我也尝试过隐身打开此链接,但没有运气
  • 请将 &lt;Client ID from Google API Console&gt;&lt;Client secret from Google API Console&gt; 替换为您在 BigQuery 管理项目中创建凭据时获得的客户端 ID 和密码
  • 我认为它在您点击 Google Cloud Console 中的 BigQuery 标题后的权限菜单下
  • 第二次尝试魅力!这是工作!非常感谢!!需要补充一点关于如何做 api 密钥的内容!
猜你喜欢
  • 2023-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-21
  • 2018-09-19
  • 1970-01-01
  • 2023-02-21
相关资源
最近更新 更多