Source code for airflow.providers.amazon.aws.sensors.athena_spark

# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

from __future__ import annotations

from collections.abc import Sequence
from typing import TYPE_CHECKING

from airflow.providers.amazon.aws.hooks.athena import AthenaHook
from airflow.providers.amazon.aws.sensors.base_aws import AwsBaseSensor
from airflow.providers.amazon.aws.utils.mixins import aws_template_fields

if TYPE_CHECKING:
    from airflow.sdk import Context


[docs] class AthenaSparkSensor(AwsBaseSensor[AthenaHook]): """ Waits for an Amazon Athena Spark calculation to reach a terminal state. :param calculation_execution_id: Athena Spark calculation execution ID. (templated) :param aws_conn_id: The Airflow connection used for AWS credentials. If this is ``None`` or empty then the default boto3 behaviour is used. If running Airflow in a distributed manner and aws_conn_id is None or empty, then default boto3 configuration would be used (and must be maintained on each worker node). (templated) :param region_name: AWS region_name. If not specified then the default boto3 behaviour is used. (templated) """
[docs] aws_hook_class = AthenaHook
[docs] template_fields: Sequence[str] = aws_template_fields("calculation_execution_id")
def __init__( self, *, calculation_execution_id: str, aws_conn_id: str = "aws_default", region_name: str | None = None, **kwargs, ) -> None: super().__init__(aws_conn_id=aws_conn_id, region_name=region_name, **kwargs)
[docs] self.calculation_execution_id = calculation_execution_id
[docs] def poke(self, context: Context) -> bool: state = self.hook.check_spark_calculation_status( calculation_execution_id=self.calculation_execution_id, ) self.log.info("Calculation %s state is: %s", self.calculation_execution_id, state) if state in AthenaHook.SPARK_SUCCESS_STATES: self.log.info("Athena Spark calculation %s completed.", self.calculation_execution_id) return True if state in AthenaHook.SPARK_FAILURE_STATES: reason = self.hook.get_spark_calculation_state_change_reason( calculation_execution_id=self.calculation_execution_id, ) raise RuntimeError( f"Athena Spark calculation {self.calculation_execution_id} failed with state {state}. " f"Reason: {reason or 'Unknown'}" ) if state in AthenaHook.SPARK_INTERMEDIATE_STATES: return False raise RuntimeError( f"Unexpected Athena Spark calculation state for {self.calculation_execution_id}: {state!r}" )

Was this entry helpful?