如何在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中设置成功判断逻辑:
- 修改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}") # 异常时返回失败标记
- 在DataFactory的Notebook活动配置中,找到成功条件字段,输入表达式:
@equals(activity('你的Notebook活动名称').output.runOutput, 'SUCCESS')
只有当Notebook返回SUCCESS时,DataFactory才会判定任务成功,否则标记为失败。
内容的提问来源于stack exchange,提问作者Tiago
相关产品推荐
相关产品推荐

