首页 > 解决方案 > 如何为 .write.mode("overwrite") 添加`try/except`

问题描述

我有一个函数可以将一些数据写入 S3 存储桶:

def write_to_s3(data, s3_path):
    
    data.write.mode("overwrite").parquet(s3_path)

    data.write.mode("overwrite").json(s3_path, compression="gzip")

如何添加try / except语句以捕获潜在的错误/异常?我在网上找不到例子,有人可以帮忙吗?谢谢。

就像是:

def write_to_s3(data, s3_path):
    try:
      data.write.mode("overwrite").parquet(s3_path)
    except ????Error as error:
      logging.error(error)
      raise

    try:
      data.write.mode("overwrite").json(s3_path, compression="gzip")
    except ????Error as error:
      logging.error(error)
      raise

标签: pythonpython-3.xexceptionpysparktry-catch

解决方案


推荐阅读