簡體   English   中英

如何在 Airflow 中安排 Python 腳本?

[英]How to schedule a Python script in Airflow?

我在 Windows 中有以下目錄/文件結構:

Learn Airflow
        |
        project
           |_dags
               |_file_mover.py
               |_first_dag.py
            |_dockerfiles
                |_Dockerfile
            |_docker-compose.yml

file_mover.py我有一個簡單的腳本將一些文件從LocationA移動到LocationB first_dag.py我有一個觸發file_mover.py的腳本。 因此,當我在終端中執行docker-compose up --build並檢查webserver localhost:8080時,我確實在 Airflow 中看到了first_dag 因此,當我打開那個 DAG 時,我希望文件從LocationA移動到LocationB eq 會觸發file_mover.py .. 但是,這並沒有發生,我不知道為什么。

這是 file_mover.py

import os
import shutil  

location_a = r'c:\data\GG\Desktop\LocationA'
location_b = r'c:\data\GG\Desktop\LocationB'

files = os.listdir(location_a)

for f in files:
    file_path = os.path.join(location_a, f)
    shutil.move(file_path, location_b)

這是 first_dag.py

    try: 
     from datetime import timedelta
     from airflow import DAG
     from airflow.operators.python_operator import PythonOperator
     from datetime import datetime
     import os
     import sys
     print('All dag modules are ok.....')

except Exception as e:
    print('Error {}'.format(e))

def first_function_execute():
     os.system('python c:\data\GG\Desktop\Python Microsoft Visual Studio\Learn Airflow\project\dags\file_mover.py')
  
with DAG (
     dag_id = 'first_dag',
     schedule_interval='@daily',
     default_args={
          'owner': 'airflow',
          'retries': 1,
          'retry_delay':  timedelta(minutes=5), 
          'start_date': datetime(2021, 1, 1),
     },
     catchup=False) as f:

     first_function_execute = PythonOperator(
          task_id='first_function_execute',
          python_callable=first_function_execute)
      

我最終想要的是通過 Airflow localhost 來安排和監視file_mover.py應用程序,但是上面的試用似乎不起作用......

正如您已正確解決的那樣,應該使用PythonOperator來運行您的腳本。 我會把它作為一個 function 來做,它會被導出,然后你可以簡單地從一個模塊中導入它,只要它可以在你的PYTHONPATH中訪問。

import os
import shutil  

def move(location_a, location_b):
    files = os.listdir(location_a)
    for f in files:
        file_path = os.path.join(location_a, f)
        shutil.move(file_path, location_b)

然后,您可以使用默認參數和調度間隔來調度 DAG:

from airflow import DAG
from airflow.operators.python_operator import PythonOperator

from my_script import my_python_function

default_args = {
        'owner': 'airflow',
        'depends_on_past': False,
        'start_date': datetime.today(),
        'email': ['airflow@airflow.com'],
        'email_on_failure': False,
        'email_on_retry': False,
        'retries': 1,
        'retry_delay': timedelta(minutes=5),
}

dag = DAG('tutorial', default_args=default_args,schedule_interval="* * * * *")

PythonOperator(dag=dag,
               task_id='my_move_task',
               provide_context=False,
               python_callable=move,
               op_args=['arguments_passed_to_callable'],
               op_kwargs={'keyword_argument':'which will be passed to function'})

然后在開始 Airflow 之前,您可以將腳本的路徑添加到PYTHONPATH ,如下所示:

export PYTHONPATH=/path/to/my/scripts/dir/:$PYTHONPATH

More information about the Python operation and how to pass arguments to the function: https://airflow.incubator.apache.org/code.html#airflow.operators.PythonOperator

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM