【问题标题】:Azure Blob Storage downloading ORC files in PythonAzure Blob 存储在 Python 中下载 ORC 文件
【发布时间】:2021-06-17 19:21:51
【问题描述】:
这是我第一次使用 Azure Storage 和 ORC。
这是我到目前为止所学到的,我能够从 Azure 下载一个 ORC blob 存储文件并保存到磁盘。下载完成后,我可以使用 Python 中的 pyorc 库来迭代 ORC 文件。它们大多是较小的文件,可以很容易地放入内存中。我的问题是,我不想写入文件,而是想将 blob 保留在内存中并进行迭代,并且可以避免写入磁盘。我可以将 blob 下载到流中,但我不确定如何将 pyorc 与 blob 流一起使用,或者我找不到它的帮助。
感谢任何有关 azure 存储下载的帮助和最佳实践。
【问题讨论】:
标签:
python
azure
azure-blob-storage
【解决方案1】:
关于问题,请参考以下步骤
import pyorc
import io
from azure.storage.blob import BlobClient
key = 'account key'
blob_client = BlobClient(account_url='https://<accountname>.blob.core.windows.net',
container_name='test',
blob_name='my.orc',
credential=key,)
with io.BytesIO() as f:
blob_client.download_blob().readinto(f)
reader = pyorc.Reader(f)
print(next(reader))
【解决方案2】:
我要感谢 Jim Xu 的解决方案,如果有人感兴趣,我会稍微修改他的解决方案以满足我的需要
from azure.storage.blob import ContainerClient, BlobClient
from io import BytesIO
import pyorc
containerClient = ContainerClient.from_connection_string(azureConnString, container_name=azureContainer)
blobList = containerClient.list_blobs(azureBlobFolder)
for fileNo, blob in enumerate(blobList):
blobClient = containerClient.get_blob_client(blob=blob.name)
with BytesIO() as f:
blobClient.download_blob().readinto(f)
reader = pyorc.Reader(f)
print(next(reader))