【问题标题】:write/save Dataframe to azure file share from azure databricks从 azure databricks 将 Dataframe 写入/保存到 azure 文件共享
【发布时间】:2021-01-10 06:12:57
【问题描述】:

如何从 azure databricks spark 作业写入 azure 文件共享。

我配置了 Hadoop 存储键和值。

spark.sparkContext.hadoopConfiguration.set(
  "fs.azure.account.key.STORAGEKEY.file.core.windows.net",
  "SECRETVALUE"
)


val wasbFileShare =
    s"wasbs://testfileshare@STORAGEKEY.file.core.windows.net/testPath"

df.coalesce(1).write.mode("overwrite").csv(wasbBlob)

当尝试将数据帧保存到 azure 文件共享时,尽管存在 URI,但我看到以下资源未找到错误。

 Exception in thread "main" org.apache.hadoop.fs.azure.AzureException: com.microsoft.azure.storage.StorageException: The requested URI does not represent any resource on the server.

【问题讨论】:

  • 在 Azure 存储中,wasbs: 只支持 azure blob:datacadamia.com/azure/wasb。如果要使用Azure文件共享,看来需要使用sdk。

标签: azure apache-spark azure-databricks azure-files


【解决方案1】:

从数据块连接到 azure 文件共享的步骤

首先使用 Databricks 中的 pip install 安装适用于 Python 的 Microsoft Azure 存储文件共享客户端库。 https://pypi.org/project/azure-storage-file-share/

安装后,创建一个存储帐户。然后您可以从数据块创建文件共享

from azure.storage.fileshare import ShareClient

share = ShareClient.from_connection_string(conn_str="<connection_string consists of FileEndpoint=myFileEndpoint(https://storageaccountname.file.core.windows.net/);SharedAccessSignature=sasToken>", share_name="<file share name that you want to create>")

share.create_share()

此代码是通过databricks将文件上传到文件共享中

from azure.storage.fileshare import ShareFileClient
 
file_client = ShareFileClient.from_connection_string(conn_str="<connection_string consists of FileEndpoint=myFileEndpoint(https://storageaccountname.file.core.windows.net/);SharedAccessSignature=sasToken>", share_name="<your_fileshare_name>", file_path="my_file")
 
with open("./SampleSource.txt", "rb") as source_file:
    file_client.upload_file(source_file)

更多信息请参考此链接https://pypi.org/project/azure-storage-file-share/

【讨论】:

    【解决方案2】:

    很遗憾,Azure 数据块不支持读取和写入 Azure 文件共享。

    Azure Databricks 支持的数据源:https://docs.microsoft.com/en-us/azure/databricks/data/data-sources/

    我建议您提供相同的反馈:

    https://feedback.azure.com/forums/909463-azure-databricks

    您在这些论坛中分享的所有反馈都将由负责构建 Azure 的 Microsoft 工程团队进行监控和审查。

    您可以查看解决类似问题的 SO 线程:Databricks and Azure Files

    下面是用于将 CSV 数据直接写入 Azure Databricks Notebook 中的 Azure blob 存储容器的代码 sn-p。

    # Configure blob storage account access key globally
    spark.conf.set("fs.azure.account.key.chepra.blob.core.windows.net", "gv7nVIXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXdlOiA==")
    output_container_path = "wasbs://sampledata@chepra.blob.core.windows.net"
    output_blob_folder = "%s/wrangled_data_folder" % output_container_path
    
    # write the dataframe as a single file to blob storage
    (dataframe
     .coalesce(1)
     .write
     .mode("overwrite")
     .option("header", "true")
     .format("com.databricks.spark.csv")
     .save(output_blob_folder))
    
    # Get the name of the wrangled-data CSV file that was just saved to Azure blob storage (it starts with 'part-')
    files = dbutils.fs.ls(output_blob_folder)
    output_file = [x for x in files if x.name.startswith("part-")]
    
    # Move the wrangled-data CSV file from a sub-folder (wrangled_data_folder) to the root of the blob container
    # While simultaneously changing the file name
    dbutils.fs.mv(output_file[0].path, "%s/predict-transform-output.csv" % output_container_path)
    

    【讨论】:

    • 您好,感谢您的回答,Azure 是否支持在没有数据砖的情况下从文件共享读取/写入,而不是直接使用 Azure 和 spark 集群?
    猜你喜欢
    • 1970-01-01
    • 2020-11-15
    • 2021-03-09
    • 2019-09-23
    • 2022-10-04
    • 2020-05-12
    • 2020-05-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多