Airflow 如何在python callable中检索默认参数
我需要能够访问Python操作符Python_callable中定义为DAG定义一部分的默认参数。也许是因为我对python或airflow不太熟悉,但有人可以指导我如何实现这一点 下面是我试图实现的代码示例Airflow 如何在python callable中检索默认参数,airflow,Airflow,我需要能够访问Python操作符Python_callable中定义为DAG定义一部分的默认参数。也许是因为我对python或airflow不太熟悉,但有人可以指导我如何实现这一点 下面是我试图实现的代码示例 default_args = { 'owner': 'airflow', 'depends_on_past': False, 'email': 'xyz@xyz.com', 'email_on_failure': 'xyz@xyz.com', 'em
default_args = {
'owner': 'airflow',
'depends_on_past': False,
'email': 'xyz@xyz.com',
'email_on_failure': 'xyz@xyz.com',
'email_on_retry': False,
'retries': 1,
'retry_delay': timedelta(minutes=5),
'start_date': datetime(2017, 5, 15, 23, 20),
'end_date': datetime(2017, 5, 16, 23, 45),
'touchfile_path': '/user/myname/touchfiles/',
}
dag = DAG(
'test',
default_args=default_args,
template_searchpath=['/Users/myname/Desktop/utils/airflow/resources'],
user_defined_macros=dict(SCHEMA_NAME='abc'),
#schedule_interval='*/2 * * * * ')
schedule_interval='@once')
def webhdfs_touchfile_create(ds, *args, **kwargs):
web_hdfs_hook = WebHDFSHook('webhdfs_default')
client = web_hdfs_hook.get_conn()
client.write("/user/myname/airflow_hdfs","stringToWrite")
pp.pprint(kwargs)
task1 = PythonOperator(
task_id='task1',
provide_context=True, #enabling this would allow to pass arguments automatically to your callable function
python_callable=webhdfs_touchfile_create,
templates_dict={'attr1': {{ default_args['touchfile_path'] }}},
dag=dag)
由于PythonOperator的模板dict是jinja模板工作的唯一属性,因此如何检索其中的“touchfile\u path”参数?因为它们在同一文件中定义,处于同一级别,您可以执行以下操作:
def webhdfs\u touchfile\u create(ds、*args、**kwargs):
web\u hdfs\u hook=WebHDFSHook(“webhdfs\u默认值”)
client=web\u hdfs\u hook.get\u conn()
client.write(“/user/myname/aiffort\u hdfs”,“stringToWrite”)
pp.pprint(kwargs)
pp.pprint(默认参数['touchfile\u path'])
默认参数应在*args中可用。您尝试过从那里访问touchfile\u path
吗?在气流中传递变量有两种机制:
- (1) 金贾模板
- (2) 专用运算符属性
用户定义的\u宏
属性传递。
使用(2)方法,您应该查看特定的运算符属性
请注意,一些运算符属性由Jinja处理,您可以使用模板语法
以下是一个工作示例:
from datetime import timedelta
from airflow import DAG
from airflow.operators.bash_operator import BashOperator
from airflow.operators.python_operator import PythonOperator
from airflow.utils.dates import days_ago
default_args = {
'owner': 'airflow',
'depends_on_past': False,
'start_date': days_ago(2),
'email': ['airflow@example.com'],
'email_on_failure': False,
'email_on_retry': False,
'retries': 1,
'retry_delay': timedelta(minutes=5),
'custom_key1': 'custom_value1',
'custom_key2': 'custom_value2'
}
dag = DAG(
'tutorial',
default_args=default_args,
description='A simple tutorial DAG',
schedule_interval=timedelta(days=1),
user_defined_macros=default_args ,
)
bash_command = """
echo "access via DAG's user_defined_macros = {{ custom_key1 }}"
echo "access via Operator's params = {{ params.custom_key2 }}"
"""
t1 = BashOperator(
task_id='print_in_bash_op',
bash_command=bash_command,
params=default_args,
dag=dag,
)
def myfunc(**context):
print(context['templates_dict']['custom_key1'])
print(context['templates_dict']['custom_key2'])
t2 = PythonOperator(
task_id='print_in_python_op',
python_callable=myfunc,
templates_dict=default_args,
provide_context=True,
dag=dag,
)
templates_dict={
'custom_key1': '{{ custom_key1 }}',
'custom_key2': '{{ custom_key2 }}'
}
t3 = PythonOperator(
task_id='print_in_python_op_2',
python_callable=myfunc,
templates_dict=templates_dict,
provide_context=True,
dag=dag,
)
t1 >> t2 >> t3
根据评论添加内容
使用变量的能力完全取决于运算符
在(2)种方法中,通常有传递信息的特殊属性,如:
- bash操作符中的bash_命令
- 蟒蛇机中的op_kwargs
- BigQueryOperator中的sql
模板化)。
例如,上面的属性是模板化的属性
到处
可以使用,也可以使用用户变量(通过user\u defined\u macros
定义)。有没有人想到这是否是个好主意?默认参数中是否包含touchfile\u路径?是的,这看起来很不错。Python操作符中是否有dag级别的用户定义的宏?是的,请查看上面示例中的t3任务。您可以在“templates\u dict”、“op\u args”、“op\u kwargs”属性中呈现jinja变量(通过用户定义的宏定义)。@IIya很好的建议。但是,默认参数不是仅用于在DAG级别定义,并且该DAG下的所有任务都应该继承它们吗?如果我们必须显式地将默认参数传递给每个任务(无论是通过templates\u dict
还是params
),那么default\u args
有什么意义?是否应该有一种更简单的方法来访问任务中的默认参数?