气流 - 无法将Bashoperator中的模板Env变量推到XCOM
我正在尝试对XCOM和Jinja模板上的云作曲家的气流2.2基础知识感到满意。我的Bashoperator的代码如下(它以文件的名称使用执行日期)。该文件名作为env
变量传递,以便{{ts}}
被识别为模板(如果我将其作为params
变量传递给,执行日期不会被解析为params
未根据源代码):
writeToFile1Bash = BashOperator (
task_id='writetofile1bash',
retries=0,
bash_command='echo "number,square,root\n" > ${file1name} && echo "{{ task_instance.xcom_push(key="file1name", value=$file1name) }}"',
env={
'file1name':'/home/airflow/gcs/data/file1-{{ ts }}.txt'
}
)
对变量$ {file1name}
的第一个引用是按预期工作的,如果我完成了bash命令在那里,它将成功创建文件。但是,我希望能够将其作为XCOM(带有自定义变量名称)传递到下游任务,而我无法实现它。我在第二个命令中提取file1name
变量的不同格式以下错误:
- var.value.file.file1name
- > keyError:'变量file1name不存在'
- $(file1name)
或$ file1name
- > 错误渲染模板:101
- $ {file1name}
- > jinja2.exceptions.templatesyntaxerror:预期令牌':',get'}'}'
- env.file1name
,env> env ['file1name']
或<代码> env ['file1name] - &gt; env未定义
- params.file1name1
- &gt; 错误渲染模板:类型strictundefined的对象不是JSON序列化
我尝试了每种想法的方式,但是文档尚不清楚。即使在此 tutorial (短语之后:之后:修改DAG后,您应该拥有以下操作:
),鼓励您使用env> env
而不是params
,因为需要jinja-parssparible,但是;没有一个代码示例,即如何从双括号内部引用此变量{{}}。
如果我已经花了很多时间,我会非常感谢是否有人可以帮助解决这个问题。
I'm trying to get comfortable with Airflow 2.2 basics on Cloud Composer regarding Xcom and Jinja Templates. My BashOperator's code is the following (it uses the execution date in the name of the file). This filename is passed as an Env
variable so that {{ ts }}
is recognised as a template (If I pass it as a Params
variable, the execution date would not be parsed as Params
is not templated according to the source code):
writeToFile1Bash = BashOperator (
task_id='writetofile1bash',
retries=0,
bash_command='echo "number,square,root\n" > ${file1name} && echo "{{ task_instance.xcom_push(key="file1name", value=$file1name) }}"',
env={
'file1name':'/home/airflow/gcs/data/file1-{{ ts }}.txt'
}
)
The first reference to the variable ${file1name}
is working as expected and, if I finish the bash command there, it will create the file succesfully. However, I would like to be able to pass it as an XCom (with a custom variable name) to downstream tasks and I have not been able to achieve it. I have got the following errors with different formats for extracting the file1name
variable inside the second command:
-var.value.file1name
--> KeyError: 'Variable file1name does not exist'
-$(file1name)
or $file1name
--> Error rendering template: unexpected char '$' at 101
-${file1name}
--> jinja2.exceptions.TemplateSyntaxError: expected token ':', got '}'
-env.file1name
, env['file1name']
or ENV['file1name]
--> env is not defined
-params.file1name1
--> Error rendering template: Object of type StrictUndefined is not JSON serializable
I have tried every single way I could think of but the documentation is not clear. Even in this tutorial (just after the phrase: So, after modifying the DAG you should have this:
), you are encouraged to use env
instead of params
because of the need of Jinja-parsing the variable, however; there is not a code example of how to reference this variable from inside the double brackets {{ }}.
I would highly appreciate if anyone could help to solve this issue as I have spent many hours already.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于
bashoperator
,stdout的最后一行将推到XCOM。参考: https://airflow.apache.org/docs/apache-airflow/1.10.13/_api/_api/airflow/airflow/operators/bash_operator/index.html#airflow.operators.bash_operator.bashoperator.bashoperator.bashoperator.bashoperator
您可以做这样:
然后在连续的运算符中:
update
否XCOM方法:
{{{ts}}
是dag_run的Timestamp SOTS
不会更改在任务之间。也就是说,在同一dag中多次使用
{{ts}}
是安全的,并且使用静态变量,您不必使用XCOM。For
BashOperator
, the last line to stdout will pushed to xcom.ref: https://airflow.apache.org/docs/apache-airflow/1.10.13/_api/airflow/operators/bash_operator/index.html#airflow.operators.bash_operator.BashOperator
You can do it this way:
Then in the successive operator:
Update
No xcom approach:
{{ ts }}
is dag_run's timestamp sots
won't change between tasks.That says, it is safe to use
{{ ts }}
multiple times in the same DAG and using static variable you don't have to use xcom.