[英]Apache-airflow 2.5.0: How to utilize config JSON in Python VirtualEnv Operator
我的 Apache airflow 版本是2.5.0。
我想訪問和使用在觸發 DAG 時設置的配置 json 的值。 我已經嘗試了以下給定的問題解決方案,但沒有一個有效。
配置在配置 json 部分:
{“conf1”:“測試”}
我想在我的 Python VirtualEnv Operator 中訪問 conf1 的值。
配置 JSON:{“conf1”:“測試”}
使用 **上下文context['dag_run'].conf['conf1']
錯誤:這給出了上下文中沒有“dag_run”的錯誤
使用 Jinja Templete:沒有用。 dag_run 未定義dag_config = {{ dag_run }}
dag_config = {{ dag_run.conf['conf1'] }}
dag_config = "{{ dag_run }}"
DAG 參數:可以創建默認參數但無法在 python 運算符中訪問它們params={"conf1": ""}
安裝jinja2獲取模板:錯誤。 “dag_run”未定義。
tm = Template("Hello {{ dag_run }}")
msg = tm.render(dag_run=dag_run)
tm = Template("Hello {{ dag_run.conf['conf1'] }}")
msg = tm.render(dag_run=dag_run)
期待:
在 Python VirtualEnv Operator 下的函數中獲取值 'test' 在一個可以使用的變量中。
您可以使用上游任務作為幫助程序將上下文變量提供給 PythonVirtualEnvOperator:
from airflow import DAG
from airflow.decorators import task
import pendulum
with DAG(
dag_id='test_venv',
start_date=pendulum.datetime(2023, 1, 1),
schedule=None,
catchup=False,
params={
"param1": "default_value",
},
render_template_as_native_obj=True # To prevent all params to be converted to strings
):
@task(
templates_dict={"my_config" : "{{ params.param1 }}"}
)
def get_param(**kwargs):
return kwargs["templates_dict"]["my_config"]
@task.virtualenv(
task_id="virtualenv_python",
requirements=["numpy"],
)
def python_virtual_env_operator_task(my_config):
print(my_config)
python_virtual_env_operator_task(get_param())
您還需要仔細檢查core.dag_run_conf_overrides_params
是否設置為 True。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.