如何在DataFactory中中止一个笔记本

huangapple go评论75阅读模式
英文:

How to abort a notebook in DataFactory

问题

我正在尝试编写一个查询来访问数据工厂中的表格。我已经有了函数和查询,它可以正常工作。现在,当代码进入except部分时,我想立即中止笔记本。我目前有以下代码:

try:
    df = spark.sql(query)
    df.write.format(formato).mode(method).saveAsTable(table)
except Exception as e:
    msg = "写入查询到表格时发生错误: {}".format(e)
    dbutils.notebook.exit(msg)

实际上,它会在笔记本上显示一条消息并阻止它继续运行。但是,在数据工厂上,它会显示运行成功 如何在DataFactory中中止一个笔记本,但我想显示为 "失败" 如何在DataFactory中中止一个笔记本

有人能帮我吗?

非常感谢! 如何在DataFactory中中止一个笔记本

英文:

I am trying to write a query to a table in data factory.
I already have functions and query and it works fine.
Now I want to abort the notebook as soon as the code enters the except part.
I have this right now:

try:
    df = spark.sql(query)
    df.write.format(formato).mode(method).saveAsTable(table)
    except Exception as e:
        msg = "An error occured while writing the query to the table: {}".format(e)
        dbutils.notebook.exit(msg)

It does actually show a message on the notebook and stop it from continuing. BUT, on data factory it does show that the run was successfully 如何在DataFactory中中止一个笔记本, but I wanted to show as "Failed" 如何在DataFactory中中止一个笔记本.

Does anyone can help me?

Thank you so much!! 如何在DataFactory中中止一个笔记本

答案1

得分: 1

我尝试了与您的代码相同的操作,我的笔记本也显示成功。

在这里,您正在处理错误并通过 dbutils.notebook.exit(msg) 向笔记本传递消息。

笔记本在退出并附带消息时不会失败,但 Databricks 笔记本执行将被中止。

如果您想在 ADF 管道中获取导致 Databricks 笔记本中止的原因(消息),请在笔记本活动的成功后使用以下表达式。

@activity('Notebook1').output.runOutput

(或)

如果您希望笔记本活动失败,您需要生成错误并中止笔记本,像下面的示例一样。

try:
    df = spark.sql(query)
    df.write.format(formato).mode(method).saveAsTable(table)
except Exception as e:
    msg = "写入查询到表时发生错误: {}".format(e)
    logging.error(msg)
    raise

现在,要在 ADF 中获取错误消息,请在笔记本活动失败后使用以下表达式。

@activity('Notebook1').output.runError
英文:

I tried the same with your code and my Notebook is also gave me Success.

如何在DataFactory中中止一个笔记本

Here, you are handling the error and passing a message to the notebook using dbutils.notebook.exit(msg).

Notebook won't fail when it exited with a message but the databricks notebook execution will be aborted.

If you want to get the reason(Message) causing the abort of the databricks notebook in the ADF pipeline, use the below expression after the Success of the notebook activity.

@activity('Notebook1').output.runOutput

(OR)

If you want Notebook activity to fail, you need to generate the error and abort the notebook like below sample.

try:
    df = spark.sql(query)
    df.write.format(formato).mode(method).saveAsTable(table)
except Exception as e:
    msg = "An error occured while writing the query to the table: {}".format(e)
    logging.error(msg)
    raise

Now, to get the error message in ADF, use the below expression after the Failure of the Notebook activity.

@activity('Notebook1').output.runError

如何在DataFactory中中止一个笔记本

huangapple
  • 本文由 发表于 2023年7月28日 02:18:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76782464.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定