def get_check_date(context): # logging.info(context) ds = context.get('ds') dt = datetime.strptime(ds, '%Y-%m-%d') (y, w, wd) = datetime.strptime(ds, '%Y-%m-%d').isocalendar() params = context['params'] schedule = params.get('scheduling', 0) start = params.get('start', 0) if schedule > 0: sub_n = (7 + (wd - schedule)) % 7 else: sub_n = 0 sdt = timedelta(days=sub_n) chk_d = datetime.strftime(dt - sdt, '%Y-%m-%d') chk_d2 = datetime.strftime(dt - sdt, '%Y%m%d') # logging.info(context["ti"].task_id) logging.info(dt) logging.info(chk_d) logging.info(start) partition = "dt='{}'".format( chk_d if int(chk_d2) > int(start) else datetime. strftime(datetime.strptime(start, '%Y%m%d'), '%Y-%m-%d')) logging.info('Poking for table %s.%s, partition %s', params.get('hive_db'), params.get('hive_table'), partition) from airflow.hooks.hive_hooks import HiveMetastoreHook hook = HiveMetastoreHook(metastore_conn_id="metastore_default") return hook.check_for_partition(params.get('hive_db'), params.get('hive_table'), partition)
class HivePartitionSensor(BaseSensorOperator): """ Waits for a partition to show up in Hive. Note: Because ``partition`` supports general logical operators, it can be inefficient. Consider using NamedHivePartitionSensor instead if you don't need the full flexibility of HivePartitionSensor. :param table: The name of the table to wait for, supports the dot notation (my_database.my_table) :type table: string :param partition: The partition clause to wait for. This is passed as is to the metastore Thrift client ``get_partitions_by_filter`` method, and apparently supports SQL like notation as in ``ds='2015-01-01' AND type='value'`` and comparison operators as in ``"ds>=2015-01-01"`` :type partition: string :param metastore_conn_id: reference to the metastore thrift service connection id :type metastore_conn_id: str """ template_fields = ( 'schema', 'table', 'partition', ) ui_color = '#C5CAE9' @apply_defaults def __init__(self, table, partition="ds='{{ ds }}'", metastore_conn_id='metastore_default', schema='default', poke_interval=60 * 3, *args, **kwargs): super(HivePartitionSensor, self).__init__(poke_interval=poke_interval, *args, **kwargs) if not partition: partition = "ds='{{ ds }}'" self.metastore_conn_id = metastore_conn_id self.table = table self.partition = partition self.schema = schema def poke(self, context): if '.' in self.table: self.schema, self.table = self.table.split('.') self.log.info('Poking for table {self.schema}.{self.table}, ' 'partition {self.partition}'.format(**locals())) if not hasattr(self, 'hook'): from airflow.hooks.hive_hooks import HiveMetastoreHook self.hook = HiveMetastoreHook( metastore_conn_id=self.metastore_conn_id) return self.hook.check_for_partition(self.schema, self.table, self.partition)
class HivePartitionSensor(BaseSensorOperator): """ Waits for a partition to show up in Hive. Note: Because ``partition`` supports general logical operators, it can be inefficient. Consider using NamedHivePartitionSensor instead if you don't need the full flexibility of HivePartitionSensor. :param table: The name of the table to wait for, supports the dot notation (my_database.my_table) :type table: string :param partition: The partition clause to wait for. This is passed as is to the metastore Thrift client ``get_partitions_by_filter`` method, and apparently supports SQL like notation as in ``ds='2015-01-01' AND type='value'`` and comparison operators as in ``"ds>=2015-01-01"`` :type partition: string :param metastore_conn_id: reference to the metastore thrift service connection id :type metastore_conn_id: str """ template_fields = ('schema', 'table', 'partition',) ui_color = '#C5CAE9' @apply_defaults def __init__( self, table, partition="ds='{{ ds }}'", metastore_conn_id='metastore_default', schema='default', poke_interval=60*3, *args, **kwargs): super(HivePartitionSensor, self).__init__( poke_interval=poke_interval, *args, **kwargs) if not partition: partition = "ds='{{ ds }}'" self.metastore_conn_id = metastore_conn_id self.table = table self.partition = partition self.schema = schema def poke(self, context): if '.' in self.table: self.schema, self.table = self.table.split('.') self.log.info( 'Poking for table {self.schema}.{self.table}, ' 'partition {self.partition}'.format(**locals())) if not hasattr(self, 'hook'): from airflow.hooks.hive_hooks import HiveMetastoreHook self.hook = HiveMetastoreHook( metastore_conn_id=self.metastore_conn_id) return self.hook.check_for_partition( self.schema, self.table, self.partition)