Convert python script to Airflow PythonOperator(s)

痞子三分冷 提交于 2019-12-24 07:14:41

问题


I have a working python script with runs from CronJob. I want to convert it to DAG with PythonOperator(s) as we now are converting to Airflow.

Say that I have functions: a(),b(),c(),d() And their execution order is : a->b->c->d

Lets say that the function codes are:

def a(): 
    print("Happy")

def b(): 
    print("Birthday")

def c(): 
    print("to")

def d(): 
    print("you!")

** This is just an example my code for all functions is more complex

I have this DAG:

args = {
    'owner': 'airflow',
    'start_date': airflow.utils.dates.days_ago(2),
    'schedule_interval': '0 10 * * *'
}

dag = DAG(dag_id='example', default_args=args)

a = PythonOperator(task_id='a', dag=dag)
b = PythonOperator(task_id='b', dag=dag)
c = PythonOperator(task_id='c', dag=dag)
d = PythonOperator(task_id='d', dag=dag)

a.set_downstream(b)
b.set_downstream(c)
c.set_downstream(d)

What I don't understand is where do I put the codes of a(),b(),c(),d() and where do I specify thier names in the execution of the PythonOperator.

You could say that I'm looking for a way to convert my Python script into Airflow as each function will be a separate operator.

I thought this should be very simple and basic but I didn't find any information about how to do that.


回答1:


In the python operator, the python function that should be executed is passed into the operator. So you will want to pass a python_callable kwarg like so:

def do_a():
    print('running a')

a = PythonOperator(task_id='a', python_callable=do_a, dag=dag)

The source for the operators will usually document the params for them. Python operator docs



来源:https://stackoverflow.com/questions/50784368/convert-python-script-to-airflow-pythonoperators

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!