简体   繁体   English

在 airflow 上触发 dag 时将参数传递给 UI

[英]passing parameters to the UI when triggering a dag on airflow

I'm trying to find a way to generalize passing parameters of a dag when triggering it from the UI itself,I know I should pass it as a key/value pair format but I don't know how to parse those parameters in the dag script itself我试图找到一种方法来概括从 UI 本身触发 dag 的传递参数,我知道我应该将它作为键/值对格式传递,但我不知道如何解析 dag 中的这些参数脚本本身

for example if I pass the conf pair {"dir_of_project":"root/home/project"}例如,如果我通过 conf 对 {"dir_of_project":"root/home/project"}

what should i do inside the dag script so that I have inside it a variable called dir_of_project that equals the path above so that I can use it further in my code.我应该在 dag 脚本中做什么,以便在其中有一个名为 dir_of_project 的变量,它等于上面的路径,以便我可以在我的代码中进一步使用它。 I tried to do the following inside the script but obviously i'm doing it wrong:我尝试在脚本中执行以下操作,但显然我做错了:

dir of project = "{{ dag_run.conf['file_name'] }}"

for example let's say i did this:例如,假设我这样做了:

def func(**kwargs):
    file_name = kwargs['dag_run'].conf.get('dir_of_project')
    return file_name
op = PythonOperator(
    task_id='task',
    python_callable=func,
    dag=dag,
    provide_context=True
)


sampling_out_parent_dir="/root/home/myfolder/sampled-vids"
file_path=os.path.join(sampling_out_parent_dir,file_name)

this would result in an undef.这将导致undef。 variable error as it's defined only in the scope of the fn and the operator and i can't used it outside like this, so how can i make it work?变量错误,因为它仅在 fn 和操作员的 scope 中定义,我不能像这样在外面使用它,那么我怎样才能让它工作呢?

I believe your issue is because you are using Jinja somewhere that isn't being templated.我相信你的问题是因为你在没有被模板化的地方使用 Jinja。

For input of {"dir_of_project":"root/home/project"} when you manually trigger DAG in the UI or executing with CLI:当您在 UI 中手动触发 DAG 或使用 CLI 执行时输入{"dir_of_project":"root/home/project"}

airflow trigger_dag your_dag_id --conf '{"dir_of_project":"root/home/project"}'

you can extract with:你可以提取:

{{ dag_run.conf['dir_of_project'] }}

or if you use PythonOperator with:或者如果您将 PythonOperator 用于:

kwargs['dag_run'].conf['dir_of_project']

Example:例子:

def func(**kwargs):
    file_name = kwargs['dag_run'].conf.get('dir_of_project')
    # Rest of the code

op = PythonOperator(
    task_id='task',
    python_callable=func,
    dag=dag,
    provide_context=True # Remove this if you are using Airflow >=2.0
)

I wanted to updated this as I solved the issue a while back but forgot to, after much research I found out that's actually impossible to extract variables with their values from airflow's operator and use them outside of it, however I found a workaround this by using template fields, airflow uses something called Xcom variables which variables that can be pushed and pulled from and to operators( basically a comm. system between them), it pretty much fets the job done if you want to extract the variable from one operator to another.我想更新它,因为我不久前解决了这个问题,但忘记了,经过大量研究后,我发现实际上不可能从气流操作符中提取变量及其值并在其外部使用它们,但是我通过使用找到了一种解决方法模板字段,airflow 使用称为 Xcom 变量的东西,这些变量可以从操作员推入和拉出(基本上是它们之间的一个通信系统),如果您想将变量从一个操作员提取到另一个操作员,它几乎可以完成工作. further more if you want to build new normal variables depending on an Xcom one, you can do that by using the Xcom one as a template filed and even build further ones, as long as you'll eventually use those normal ones inside an operator which then will be able to render that template.此外,如果您想根据 Xcom 构建新的正常变量,您可以通过将 Xcom 用作模板文件,甚至构建更多的正常变量,只要您最终在运算符中使用这些正常变量然后将能够呈现该模板。 for example: if I push an Xcom variable in one of my tasks operator and that variable has an integer value = 100, i can get the variable by using this: newvar="{{ task_instance.xcom_pull(task_ids='%s') }}"%task_id where task_id is the name of the task you pushed the variable in, further more i can use newvar outside the operator, it will carry around that value as a string until again passed to an operator which will render the template field and replace it with the original value 100. for more info refer to airflow Xcom documentation: here例如:如果我在我的一个任务运算符中推送一个 Xcom 变量,并且该变量的 integer 值 = 100,我可以使用以下方法获取该变量: newvar="{{ task_instance.xcom_pull(task_ids='%s') }}"%task_id其中 task_id 是您将变量推入的任务的名称,此外,我可以在运算符之外使用 newvar,它将将该值作为字符串携带,直到再次传递给将呈现模板字段的运算符并将其替换为原始值 100。有关更多信息,请参阅 airflow Xcom 文档: 此处

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

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