我正在Windows上工作,通过Docker设置了Airflow。我在Windows中有许多python脚本,可以从Windows中的多个位置(SSH连接,Windows文件夹等)进行读写。将所有这些输入复制到我的docker镜像中将需要大量工作,因此我要做的就是让Airflow像在Windows中一样运行这些脚本。

如果可能的话,这可能吗?

这是我作为DAG运行的脚本:

from airflow import DAG
from airflow.operators.bash_operator import BashOperator
from airflow.operators.python_operator import PythonOperator
from datetime import datetime, timedelta

# Following are defaults which can be overridden later on
default_args = {
    'owner': 'test',
    'depends_on_past': False,
    'start_date': datetime(2018, 11, 27),
    'email': ['test@gmail.com'],
    'email_on_failure': True,
    'email_on_retry': True,
    'retries': 1,
    'retry_delay': timedelta(minutes=1),
}

dag = DAG('Helloworld', default_args=default_args)

###########################################################
# Here's where I want to execute my windows python script #
###########################################################
t1=PythonOperator(dag=dag,
               task_id='my_task_powered_by_python',
               provide_context=False,
               python_callable=r"C:\Users\user\Documents\script.py")

t2 = BashOperator(
    task_id='task_2',
    bash_command='echo "Hello World from Task 2"',
    dag=dag)

t3 = BashOperator(
    task_id='task_3',
    bash_command='echo "Hello World from Task 3"',
    dag=dag)

t4 = BashOperator(
    task_id='task_4',
    bash_command='echo "Hello World from Task 4"',
    dag=dag)

t2.set_upstream(t1)
t3.set_upstream(t1)
t4.set_upstream(t2)
t4.set_upstream(t3)

最佳答案

您有一个传递给PythonOperator的python脚本路径,PythonOperator在寻找Python代码对象,而不是脚本文件路径。

您有两种不同的选择来完成对这些python脚本的调用。

直接通过Bash和BashOperator调用脚本

您可以像上面一样使用BashOperator直接调用python脚本。

您可以通过使用BashOperator中的以下命令,以与不使用Airflow时相同的方式调用Python脚本来完成操作

python script.py

移动脚本并使用PythonOperator

如果仅从Airflow调用这些脚本,我会考虑将它们移入您的Python代码库,并根据需要调用您拥有的任何入口点函数。

防爆
airflowHome/
  dags/
  plugins/
  scripts/
    __init__.py
    script1.py
    script2.py

现在,您将能够使用Python导入访问脚本模块中的脚本。从那里,您可以使用PythonOperator从DAG内部调用特定的函数。

关于python - 通过 Airflow docker容器执行Windows python脚本,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53499974/

10-12 00:36
查看更多