如何使用Databricks将CSV写回到Azure Blob存储?

问题描述

我正在努力写回Azure Blob存储容器。我可以使用以下内容从容器中读取内容

storage_account_name = "expstorage"
storage_account_key = "1VP89J..."
container = "source"

spark.conf.set("fs.azure.account.key.{0}.blob.core.windows.net".format(storage_account_name),storage_account_key)

dbutils.fs.ls("dbfs:/mnt/azurestorage")

我已经尝试了多种方法来仅通过搜索将其写回我的容器,但是我找不到确定的方法

这里是使用SAS密钥的替代方案的链接,但我不想混合/匹配密钥类型。

Write dataframe to blob using azure databricks

解决方法

要写入您的Blob存储,只需指定路径,以dbfs:/mnt/azurestorage开头:

df.write
 .mode("overwrite")
 .option("header","true")
 .csv("dbfs:/mnt/azurestorage/filename.csv"))

这将创建一个包含分布式数据的文件夹。如果您要查找单个csv文件,请尝试以下方法:

df.toPandas().to_csv("dbfs:/mnt/azurestorage/filename.csv")

如果仅使用熊猫,则将无法访问dbfs api,因此需要使用本地文件API,这意味着您的路径必须以/dbfs/开头,而不是{{1} },如下所示:

dbfs:/