在 databrick 上运行时将 PySpark 标准输出和标准错误日志保存到云对象存储

2024-04-15

我正在标准 databricks 集群上运行 PySpark 数据管道代码。我需要保存所有 Python/PySpark 标准输出和标准错误消息存储到 Azure BLOB 帐户中的文件中。

当我在本地运行 Python 代码时,我可以在终端中看到包括错误在内的所有消息 并将它们保存到日志文件中。如何使用 Databricks 和 Azure 完成类似的事情 PySpark 数据管道代码的 BLOB?这可以做到吗?

非常感谢 :)


如果要将错误日志存储到 azure 存储帐户。

请按照以下步骤操作:

1.创建一个挂载到azure blob存储容器,如果您已经有日志文件,则将日志存储到挂载位置。

访问密钥

dbutils.fs.mount(    
    source = "wasbs://<container_name>@<storage_account_name>.blob.core.windows.net/",
    mount_point = "/mnt/<mount_name>",
    extra_configs = {"fs.azure.account.key.<storage_account_name>.blob.core.windows.net":"< storage_account_access key>})

2.文件路径创建

根据您的要求,您可以更改时区并保存文件。(例如:IST、UST...等)

from datetime import datetime
import pytz
curr_dt=datetime.now(pytz.timezone('Asia/Kolkata')).strftime("%Y%m%d_%H%M%S")#create timezone
directory="/mnt/"
logfilename="<file_name>"+curr_dt+"log"
path=directory+logfilename
print(path)

Ref3 3.File Handler

import logging
logger = logging.getLogger('demologger')
logger.setLevel(logging.INFO)
FileHandler=logging.FileHandler(path,mode='a')
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s: %(message)s',datefmt='%m/%d/%Y %I:%M:%S %p')
FileHandler.setFormatter(formatter)
logger.addHandler(FileHandler)
logger.debug( 'debug message')
logger.info('info message')
logger.warn('warn message')
logger.error('error message')
logger.critical ('critical message')

4.创建分区

from datetime import datetime
import pytz
partition=datetime.now(pytz.timezone('Asia/Kolkata')).strftime("%Y/%m/%d")
print(partition)

5.上传日志文件存储帐户。

 dbutils.fs.mv("file:"+path,"dbfs:/mnt/<filelocation>/log/"+partition+logfilename)

Output:

参考:

  • Databricks pyspark 中的自定义日志记录 || Azure Databricks 中的日志记录策略(作者:Cloudpandith) https://www.youtube.com/watch?v=Dp1Nv4o0lrA

  • 使用微软提供的azure DataBricks访问Azure Blob存储 https://learn.microsoft.com/en-us/azure/databricks/data/data-sources/azure/azure-storage#access-azure-blob-storage-using-the-dataframe-api.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 databrick 上运行时将 PySpark 标准输出和标准错误日志保存到云对象存储 的相关文章

随机推荐