将附加文本文件从 databricks 写入 azure adls gen1

问题描述 投票:0回答:3

我想将某种日志文件写回到azure adls gen1 我可以使用

编写(而不是附加)
dbutils.fs.put(filename,"random text")

但我无法使用

附加它
with open("/dbfs/mnt/filename.txt","a"):
f.write("random text")

它给了我错误

1 with  open("/dbfs/mnt/filename.txt", "a") as f:
----> 2   f.write("append values")

OSError: [Errno 95] Operation not supported

或者,我尝试使用 logger.basicconfig(logging.basicConfig(filename='dbfs:/mnt/filename.txt', filemode='w')

但看起来它没有写入路径。 有人可以帮忙吗

python-3.x file append azure-data-lake azure-databricks
3个回答
2
投票

Append Only (‘a’):打开文件进行写入。如果文件不存在,则创建该文件。句柄位于文件末尾。正在写入的数据将插入到末尾、现有数据之后。

file = open("myfile.txt","a")#append mode 
file.write("Today \n") 

追加文件的输出:


1
投票

您可以对 DBFS 文件执行此操作。 https://kb.databricks.com/en_US/dbfs/errno95-operation-not-supported

您可能需要找出使用 python CLI 从数据湖读取文件并写入的逻辑。


0
投票

我也有同样的问题。事实证明,由于文件系统(DBFS)的原因,您无法在 databricks 上使用标准 python 文件操作。您最终会收到错误 [Errno 95] 操作不受支持。您必须使用 dbutils 来执行此操作,但我发现您无法附加。下面的代码是我解决这个问题的方法。注意,使用dbutils.fs操作时需要使用dbfs:/文件路径。

dbfs_path_home = "dbfs:/mnt/directory
dbfs_path_log = f"{dbfs_path_home}/logfile.log"

然后,当您想要记录某些内容时,您将需要读取日志文件的全部内容并添加新消息。例如

def logging(message):
    logmsg = dbutils.fs.head(dbfs_path_log)
    logmsg += message + '\n'
    dbutils.fs.put(dbfs_path_log, logmsg, overwrite=True)
© www.soinside.com 2019 - 2024. All rights reserved.