如何定义STFP操作员对气流的操作?

时间:2020-01-08 04:05:03

标签: python operators airflow directed-acyclic-graphs operation

class SFTPOperation(object):
    PUT = 'put'
    GET = 'get'  

operation=SFTPOperation.GET,
NameError: name 'SFTPOperation' is not defined

我在这里定义了运算符,但在互联网上找不到与运算有关的任何内容

class sftpplugin(AirflowPlugin):
    name = "sftp_plugin"
    operators = [SFTPOperator]

任何帮助将不胜感激!

谢谢

1 个答案:

答案 0 :(得分:5)

注意到SFTP操作员使用ssh_hook打开sftp传输通道,您应该需要提供ssh_hookssh_conn_id进行文件传输。首先,让我们看一个提供参数ssh_conn_id的示例。

from airflow.providers.sftp.operators import sftp_operator
from airflow import DAG
import datetime

dag = DAG(
'test_dag',
start_date = datetime.datetime(2020,1,8,0,0,0),
schedule_interval = '@daily'
)

put_operation = SFTPOperator(
            task_id="operation",
            ssh_conn_id="ssh_default",
            local_filepath="route_to_local_file",
            remote_filepath="remote_route_to_copy",
            operation="put",
            dag=dag
            )
get_operation = SFTPOperator(....,
            operation = "get",
            dag = dag
            )

put_operation >> get_operation

请注意,应该根据任务的需要安排dag,这里的示例考虑的是每天中午开始的时间表。现在,如果您要提供SSHhook,则必须对上述代码进行以下更改

from airflow.contrib.hooks.ssh_hook import SSHHook
...

put_operation = SFTPOperator(
            task_id="operation",
            ssh_hook=SSHHook("Name_of_variable_defined"),
            ...
            dag=dag
            )
....

其中"Name_of_variable_defined"在Airflow界面的Admin-> Connections中创建。