簡體   English   中英

python 雲作曲家 bashoperator 的腳本路徑計算引擎

[英]python script path compute engine for cloud composer bashoperator

我是第一次在 GCP 上工作,我正在嘗試使用 bashoperator 在計算引擎上執行 python 腳本。 我將我的 python 腳本存儲在 composer 腳本所在的存儲桶中,但是當我嘗試運行 bashoperator 時,它會拋出一個錯誤文件未找到。 我能知道我應該把我的 python 腳本放在哪里嗎,這樣我就可以在計算引擎上執行那個 python 腳本。

`

 bash_task = bash_operator.BashOperator(
    task_id='script_execution',
    bash_command='gcloud compute ssh --project '+PROJECT_ID+ ' --zone '+REGION+' '+GCE_INSTANCE+ '--command python3 script.py',
    dag=dag)

python3: 無法打開文件 '/home/airflow/gcs/dags/script.py': [Errno 2] No such file or directory`

  • 解決方案 1:

您可以使用 PythonOperator 在 Composer 中直接執行 Python 代碼,而PythonOperatorCloud Composer的單獨Compute Engine VM 實例中執行Python腳本:

from airflow import DAG
from airflow.operators.dummy_operator import DummyOperator
from airflow.operators.python_operator import PythonOperator
from time import sleep
from datetime import datetime

def my_func(*op_args):
   print(op_args)
   # your Python script and logic here 

with DAG('python_dag', description='Python DAG', schedule_interval='*/5 * * * *', start_date=datetime(2018, 11, 1), catchup=False) as dag:
   dummy_task = DummyOperator(task_id='dummy_task', retries=3)
   python_task = PythonOperator(task_id='python_task', python_callable=my_func, op_args=['one', 'two', 'three'])

dummy_task >> python_task
  • 解決方案 2:

在 Cloud Composer 中使用 SSHOperator,本主題可以幫助您:

ssh 啟動腳本 VM Composer

在這種情況下,腳本位於 VM 中。

  • 解決方案 3:

你也可以考慮在 Beam 和 Dataflow Python 中重寫你的 Python 腳本,如果重寫它沒有那么復雜的話。 Dataflow 具有無服務器的優勢,Airflow 建議內置運算符來啟動 Dataflow 作業。

暫無
暫無

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

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