首页 > 解决方案 > 将 XCom 与 BashOperator 一起使用:将多行 sdout 输出推送到其他任务?

问题描述

根据文档,“ ,当 bash 命令完成时,If xcom_push is True写入的最后一行stdout也将被推送到XCom 。”

但是,我需要返回一个多行字符串,该字符串是从终端执行的python 脚本产生的。我想随后在EmailOperator中使用这个字符串。

所以我的问题是:是否可以通过xcom_push推送比最后一行更多的内容?理想情况下,它可以任意长。非常感谢您的帮助,谢谢!

编辑:我已经通过使用 PythonOperator 并调用脚本解决了这个问题,但我仍然很好奇是否可以将多行数据从 BashOperator 推送到 XCom

标签: airflow

解决方案


正如源代码中明确指出的那样,只有 BashOperator 的最后一行被推送 if xcom_push = True

 :param xcom_push: If xcom_push is True, the last line written to stdout
        will also be pushed to an XCom when the bash command completes.

但是,您可以轻松地创建一个继承自 BashOperator 的自定义运算符并实现 double xcom_push

请参阅插件文档,了解如何使用 Airflow 插件构建自定义运算符。


推荐阅读