简体   繁体   English

气流-如何使用另一个传感器任务中的传感器

[英]Airflow- how to use sensor from another sensor task

I am trying to check if a file exist on a remote server, if it does, check if the row count is 0. If the row count is greater than 0, The pipeline should proceed, if not, I want the sensor to keep checking ( the file has date in the name, so next day maybe the new file would be not empty) 我正在尝试检查远程服务器上是否存在文件,如果存在,请检查行计数是否为0。如果行计数大于0,则管道应该继续进行;如果不行,我希望传感器继续检查(文件名中带有日期,因此第二天新文件可能不为空)

Could anyone help shed some light on how to implement this? 任何人都可以帮助阐明如何实现这一目标吗? I am thinking can I use a SFTP sensor from within the python function that checks the rows? 我在想可以从检查行的python函数中使用SFTP传感器吗? If so how could I use a sensor from within another? 如果是这样,我该如何使用另一个传感器? Many thanks 非常感谢

You can just make a regular sensor that achieves both these tasks, here is an outline for how to implement this, you have to put this file within the plugins folder inside airflow and you can then import and use it as part of a DAG. 您可以制作一个常规传感器来完成这两个任务,这是实现方法的概述,您必须将此文件放在气流内部的plugins文件夹中,然后可以将其导入并用作DAG的一部分。

from airflow.operators.sensors import BaseSensorOperator
from airflow.utils.decorators  import apply_defaults
from airflow.plugins_manager   import AirflowPlugin

import requests
import logging
import json

DEFAULT_CONNECTION_DETAILS = { "host": "127.0.0.1", "password": "wololo" }

log = logging.getLogger( __name__ )

class Remote_File_Row_Sensor( BaseSensorOperator ):

    @apply_defaults
    def __init__( self, file_name, connection_details= DEFAULT_CONNECTION_DETAILS, *args, **kwargs ):
        super( Remote_File_Row_Sensor, self ).__init__( *args, **kwargs )
        self.connection_details = connection_details
        self.file_name          = file_name

    def poke( self, context ):
        connection_details   = self.connection_details
        file_name            = self.file_name

        ROW_COUNT = 0

        # Your code here to connect using SFTP and read the file for the row count

        if ROW_COUNT == 0:
            return False
        else:
            return True

class Remote_File_Row_Plugin( AirflowPlugin ):
    name      = "remote_file_row_sensor"
    operators = [ Remote_File_Row_Sensor ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM