简体   繁体   English

Apache-airflow 2.5.0:如何在 Python VirtualEnv Operator 中使用配置 JSON

[英]Apache-airflow 2.5.0: How to utilize config JSON in Python VirtualEnv Operator

my Apache airflow version is 2.5.0.我的 Apache airflow 版本是2.5.0。

I want to access and use the values of config json set on triggering the DAG.我想访问和使用在触发 DAG 时设置的配置 json 的值。 I have tried the following given solutions to the issue but None of them worked.我已经尝试了以下给定的问题解决方案,但没有一个有效。

The config is ritten in the configuration json section:配置在配置 json 部分:
{"conf1": "test"} {“conf1”:“测试”}

I want to acces the value of conf1 in my Python VirtualEnv Operator.我想在我的 Python VirtualEnv Operator 中访问 conf1 的值。

config JSON: {"conf1": "test"}配置 JSON:{“conf1”:“测试”}

  • using **context使用 **上下文
    context['dag_run'].conf['conf1']
    error: this gave the error that there was no "dag_run" in context错误:这给出了上下文中没有“dag_run”的错误

  • using Jinja Templete: did not work.使用 Jinja Templete:没有用。 dag_run not defined dag_run 未定义
    dag_config = {{ dag_run }}
    dag_config = {{ dag_run.conf['conf1'] }}
    dag_config = "{{ dag_run }}"

  • DAG params: Could create default params BUT could not access them in the python operator DAG 参数:可以创建默认参数但无法在 python 运算符中访问它们
    params={"conf1": ""}

  • Installing jinja2 to get Template: Error.安装jinja2获取模板:错误。 "dag_run" not defined. “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)

Expectation:期待:

To get the value 'test' in the functions under Python VirtualEnv Operator in a varaible that can be used.在 Python VirtualEnv Operator 下的函数中获取值 'test' 在一个可以使用的变量中。

You can use an upstream task as a helper to feed the context variable into the PythonVirtualEnvOperator:您可以使用上游任务作为帮助程序将上下文变量提供给 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())

You will also want to double check that core.dag_run_conf_overrides_params is set to True.您还需要仔细检查core.dag_run_conf_overrides_params是否设置为 True。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM