【问题标题】:How to zip files (on Azure Blob Storage) with shutil in Databricks如何在 Databricks 中使用 shutil 压缩文件(在 Azure Blob 存储上)
【发布时间】:2020-04-29 23:07:26
【问题描述】:

我经过训练的深度学习模型存在于一个文件夹中的几个文件中。所以这与压缩数据帧无关。

我想压缩此文件夹(在 Azure Blob 存储中)。但是当我使用shutil时,这似乎不起作用:

import shutil
modelPath = "/dbfs/mnt/databricks/Models/predictBaseTerm/noNormalizationCode/2020-01-10-13-43/9_0.8147903598547376"
zipPath= "/mnt/databricks/Deploy/" (no /dbfs here or it will error)
shutil.make_archive(base_dir= modelPath, format='zip', base_name=zipPath)

有人知道如何执行此操作并将文件放到 Azure Blob 存储(我从中读取它的位置)吗?

【问题讨论】:

    标签: pyspark zip databricks azure-blob-storage shutil


    【解决方案1】:

    最后我自己想通了。

    无法使用 Shutil 直接写入 dbfs(Azure Blob 存储)。

    你需要先把文件放在databricks的本地驱动节点上,像这样(在文档中你不能直接写入Blob存储的地方读取它):

    import shutil
    modelPath = "/dbfs/mnt/databricks/Models/predictBaseTerm/noNormalizationCode/2020-01-10-13-43/9_0.8147903598547376"
    zipPath= "/tmp/model"
    shutil.make_archive(base_dir= modelPath, format='zip', base_name=zipPath)
    

    然后您可以将文件从本地驱动程序节点复制到 Blob 存储。请注意“文件:”以从本地存储中获取文件!

    blobStoragePath = "dbfs:/mnt/databricks/Models"
    dbutils.fs.cp("file:" +zipPath + ".zip", blobStoragePath)
    

    我为此浪费了几个小时,如果这个答案对你有帮助,请投票!

    【讨论】:

      【解决方案2】:

      实际上,不使用shutil,我可以将Databricks dbfs 中的文件压缩为一个zip 文件,作为已安装到dbfs 的Azure Blob Storage 的blob。

      这是我使用 Python 标准库 oszipfile 的示例代码。

      # Mount a container of Azure Blob Storage to dbfs
      storage_account_name='<your storage account name>'
      storage_account_access_key='<your storage account key>'
      container_name = '<your container name>'
      
      dbutils.fs.mount(
        source = "wasbs://"+container_name+"@"+storage_account_name+".blob.core.windows.net",
        mount_point = "/mnt/<a mount directory name under /mnt, such as `test`>",
        extra_configs = {"fs.azure.account.key."+storage_account_name+".blob.core.windows.net":storage_account_access_key})
      
      # List all files which need to be compressed
      import os
      modelPath  = '/dbfs/mnt/databricks/Models/predictBaseTerm/noNormalizationCode/2020-01-10-13-43/9_0.8147903598547376'
      filenames = [os.path.join(root, name) for root, dirs, files in os.walk(top=modelPath , topdown=False) for name in files]
      # print(filenames)
      
      # Directly zip files to Azure Blob Storage as a blob
      # zipPath is the absoluted path of the compressed file on the mount point, such as `/dbfs/mnt/test/demo.zip`
      zipPath = '/dbfs/mnt/<a mount directory name under /mnt, such as `test`>/demo.zip'
      import zipfile
      with zipfile.ZipFile(zipPath, 'w') as myzip:
        for filename in filenames:
      #    print(filename)
          myzip.write(filename)
      

      我尝试将我的test 容器挂载到dbfs 并运行我的示例代码,然后我得到了包含test 容器中所有文件的demo.zip 文件,如下图所示。

      【讨论】:

      • 这看起来比我做的更整洁!感谢您的深入解释。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-19
      • 2020-12-16
      • 1970-01-01
      • 2017-10-06
      • 2017-04-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多