首页 > 解决方案 > 如何并行化 Databricks 文件复制/上传操作?

问题描述

我在 Microsoft Azure 上运行 Databricks。我正在使用名为 google-cloud-storage 和 google-cloud-core 的 python 库将所有​​文件从 Databricks dbfs 路径复制到 GCP/GCS 存储桶(Google Cloud Platform / Google Cloud Storage 存储桶)。我用 PyPl 安装了这些库,并使用了 google-cloud-storage python 库中的“upload_from_filename”命令。源目录包含数百个文件,文件总量超过 100 GB。文件复制/上传成功,但复制/上传操作是一个接一个,一次一个文件。

我的问题是:如何强制 Databricks “并行化”复制/上传操作(即在多个线程中异步执行复制操作)?

以下是我的代码(为清楚起见,修改了 gcp 存储桶名称和源文件路径)

from google.cloud import storage
from datetime import datetime

storage_client = storage.Client()
bucket = storage_client.bucket('the-gcp-bucket')
files = dbutils.fs.ls('dbfs:/sourcefilepath/')
filenumber = 0

for fi in files:
  source_file_name = fi.path
  source_file_name = source_file_name.replace("dbfs:", "/dbfs")
  blob = bucket.blob('TargetSubFolder/' + fi.name)
  blob.upload_from_filename(source_file_name)
  filenumber = filenumber + 1
  print("File num: {} {} uploaded to {}.".format(str(filenumber), source_file_name, destination_blob_name))
  
print("File Copy Complete")

标签: pythongoogle-cloud-storagedatabricksazure-databricks

解决方案


推荐阅读