1

我正在从文件夹 /mnt/lake/customer 读取数据,其中 mnt/lake 是指 ADLS Gen 2 的挂载路径,现在我想将文件夹从 /mnt/lake/customer 重命名为 /mnt/lake/customeraddress无需将数据从一个文件夹复制到另一个文件夹。

我不想使用 move copy(mv) 命令,因为复制数据需要很长时间,而且我有几乎数千个文件夹需要重命名,而且数据量非常大。

我想通过数据砖来做到这一点,有人有想法吗?

4

1 回答 1

1

更新的答案:

不幸的是,现在dbutils.fs.mv实现为原始文件的复制+删除,因此无法使用。另一种方法是使用 ADLS Python SDK,它具有执行该任务的rename_directory方法,如下所示:

%pip install azure-storage-file-datalake azure-identity
from azure.storage.filedatalake import DataLakeServiceClient
from azure.identity import ClientSecretCredential

tenant_id = "...."
client_secret = dbutils.secrets.get("scope", "client_secret")
client_id = "...."
credential = ClientSecretCredential(tenant_id, client_id, client_secret)
service_client = DataLakeServiceClient(
  account_url="https://<storage_acc>.dfs.core.windows.net", 
  credential=credential)
file_system_client = service_client.get_file_system_client(
  file_system="<container>")
directory_client = file_system_client.get_directory_client("<source_dir>")
new_dir_name = "abc2"
directory_client.rename_directory(
   new_name=directory_client.file_system_name + '/' + new_dir_name)

更正之前的原始答案:安装只是一些内部数据库中的一个条目,它将名称映射到数据的实际位置。如果要重命名挂载点,只需使用dbutils.fs.unmount("/mnt/mount-name")卸载它,然后使用新名称使用dbutils.fs.mount再次挂载它(您需要具有服务主体的凭据):

dbutils.fs.unmount("/mnt/lake/customer")
configs = {"fs.azure.account.auth.type": "OAuth",
          "fs.azure.account.oauth.provider.type": "org.apache.hadoop.fs.azurebfs.oauth2.ClientCredsTokenProvider",
          "fs.azure.account.oauth2.client.id": "<application-id>",
          "fs.azure.account.oauth2.client.secret": dbutils.secrets.get(scope="<scope-name>",key="<service-credential-key-name>"),
          "fs.azure.account.oauth2.client.endpoint": "https://login.microsoftonline.com/<directory-id>/oauth2/token"}

# Optionally, you can add <directory-name> to the source URI of your mount point.
dbutils.fs.mount(
  source = "abfss://<container-name>@<storage-account-name>.dfs.core.windows.net/",
  mount_point = "/mnt/lake/customeraddress",
  extra_configs = configs)
于 2021-08-25T10:26:40.530 回答