python - 如何从 HDInsight 群集下载从 pd.to_csv() 保存的文件

标签 python azure pandas azure-blob-storage

我有一个运行 Spark 1.6.2 和 Jupyter 的 HDInsight 集群 在 jupyter 笔记本中,我运行 pyspark 命令,部分输出在 pandas 数据帧中进行处理。

作为最后一步,我想将 pandas 数据框保存到 csv 文件中,并且:

  1. 将其保存到“jupyter 文件系统”并将其下载到我的笔记本电脑
  2. 将其保存到我的 Blob 存储

但我不知道该怎么做。

我尝试了以下方法:

<强>1。将其保存到“jupyter 文件系统”并将其下载到我的笔记本电脑

# df is my resulting dataframe, so I save it to the filesystem where jupyter runs
df.to_csv('app_keys.txt')

我希望它保存在与我的笔记本相同的目录中,从而在浏览器的 TreeView 中看到它。不是这种情况。所以我的问题是:这个文件保存在文件系统上的什么位置?

<强>2。将其保存到我的 blob 存储 谷歌搜索后,我似乎还可以使用 azure.storage.blob 模块将文件上传到 blob 存储。所以我尝试了:

from azure.storage.blob import BlobService # a lot of examples online import BlockBlobService but this one is not available in HDInsight

# i have all variables in CAPITALS provided in the code
blob_service=BlobService(account_name=STORAGEACCOUNTNAME,account_key=STORAGEACCOUNTKEY)

# check if reading from blob works
blob_service.get_blob_to_path(CONTAINERNAME, 'iris.txt', 'mylocalfile.txt') # this works

# now try to reverse the process and write to blob
blob_service.create_blob_from_path(CONTAINERNAME,'myblobfile.txt','mylocalfile.txt')   # fails with AttributeError: 'BlobService' object has no attribute 'create_blob_from_path'

blob_service.create_blob_from_text(CONTAINERNAME,'myblobfile.txt','mylocalfile.txt') # fails with 'BlobService' object has no attribute 'create_blob_from_text'

所以我不知道如何写回并访问我从 pandas 写到文件系统的内容。

感谢任何帮助

最佳答案

根据我的经验,您遇到的第二个问题是由于 python 的 azure 存储客户端库的版本引起的。 对于旧版本,该库不包含您在代码中调用的方法。以下网址对您有用。

How to import Azure BlobService in python? .

关于python - 如何从 HDInsight 群集下载从 pd.to_csv() 保存的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39877341/

相关文章:

azure - 在 Azure 上运行 native 代码

python - Twisted:在互联网连接断开时捕获 DNSLookupError

python - 无法安装 psycopg2 Ubuntu

Azure key 保管库限制

asp.net - DotNetNuke 网站迁移到 Azure 失败,出现 nx 域 DNS 错误

python - 如何将时间戳四舍五入到最接近的秒数?

python - 如何更新 Tkinter Canvas 上的图像?

Python:从网页中抓取视频(在 HTML 中不可见)

python - 使用 Pandas 在python中查找连续相同行的数量

python-3.x - 匹配两个lookbehinds之一