You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在DataFactory中使Notebook执行失败终止?

解决DataFactory中Notebook任务异常仍显示成功的问题

你的问题核心是dbutils.notebook.exit()会让Notebook以正常终止状态结束,DataFactory会默认判定为任务成功。要让DataFactory识别为失败,需让Notebook以异常状态终止,以下是两种可行方案:

方案1:重新抛出异常(推荐)

在捕获异常后直接重新抛出,Notebook会标记为执行失败,DataFactory会同步这个失败状态,代码修改如下:

try:
    df = spark.sql(query)
    df.write.format(formato).mode(method).saveAsTable(table)
except Exception as e:
    error_msg = "An error occurred while writing the query to the table: {}".format(e)
    print(error_msg)  # 打印错误信息便于排查
    raise e  # 重新抛出异常,触发Notebook执行失败

方案2:自定义返回标识并配置DataFactory成功条件

如果需要保留自定义错误信息,可返回特定标识,再在DataFactory中设置成功判断逻辑:

  1. 修改Notebook代码:
try:
    df = spark.sql(query)
    df.write.format(formato).mode(method).saveAsTable(table)
    dbutils.notebook.exit("SUCCESS")  # 正常执行返回成功标记
except Exception as e:
    error_msg = "An error occurred while writing the query to the table: {}".format(e)
    dbutils.notebook.exit(f"FAILED: {error_msg}")  # 异常时返回失败标记
  1. 在DataFactory的Notebook活动配置中,找到成功条件字段,输入表达式:
@equals(activity('你的Notebook活动名称').output.runOutput, 'SUCCESS')

只有当Notebook返回SUCCESS时,DataFactory才会判定任务成功,否则标记为失败。

内容的提问来源于stack exchange,提问作者Tiago

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 21:12:39