Athena Spark Operators and Sensors¶
Amazon Athena supports Apache Spark calculations through session-based APIs. This page documents the provider support for submitting and monitoring those calculations from Airflow.
Prerequisite Tasks¶
To use these operators, you must do a few things:
Create necessary resources using AWS Console or AWS CLI.
Install API libraries via pip.
pip install 'apache-airflow[amazon]'Detailed information is available Installation of Airflow®
Generic Parameters¶
- aws_conn_id
Reference to Amazon Web Services Connection ID. If this parameter is set to
Nonethen the default boto3 behaviour is used without a connection lookup. Otherwise use the credentials stored in the Connection. Default:aws_default- region_name
AWS Region Name. If this parameter is set to
Noneor omitted then region_name from AWS Connection Extra Parameter will be used. Otherwise use the specified value instead of the connection value. Default:None- verify
Whether or not to verify SSL certificates.
False- Do not validate SSL certificates.path/to/cert/bundle.pem - A filename of the CA cert bundle to use. You can specify this argument if you want to use a different CA cert bundle than the one used by botocore.
If this parameter is set to
Noneor is omitted then verify from AWS Connection Extra Parameter will be used. Otherwise use the specified value instead of the connection value. Default:None- botocore_config
The provided dictionary is used to construct a botocore.config.Config. This configuration can be used to configure Avoid Throttling exceptions, timeouts, etc.
Example, for more detail about parameters please have a look botocore.config.Config¶{ "signature_version": "unsigned", "s3": { "us_east_1_regional_endpoint": True, }, "retries": { "mode": "standard", "max_attempts": 10, }, "connect_timeout": 300, "read_timeout": 300, "tcp_keepalive": True, }
If this parameter is set to
Noneor omitted then config_kwargs from AWS Connection Extra Parameter will be used. Otherwise use the specified value instead of the connection value. Default:NoneNote
Specifying an empty dictionary,
{}, will overwrite the connection configuration for botocore.config.Config
Operators¶
Submit Spark code to an Athena session¶
Use AthenaSparkOperator
to submit Spark code to an existing Athena Spark session.
In the following example, we submit PySpark code to an existing Athena Spark session and wait for the calculation to complete. For more examples of how to use this operator, please see the Sample Dag.
run_spark_calculation = AthenaSparkOperator(
task_id="run_spark_calculation",
session_id=idle_session_id,
code_block="print('hello from athena spark')",
waiter_delay=30,
waiter_max_attempts=120,
)
Deferrable mode¶
Set deferrable=True to wait for the calculation asynchronously using the
Airflow triggerer, releasing the worker slot while the calculation runs.
This requires a running triggerer and the aiobotocore package.
The waiter_delay and waiter_max_attempts parameters control polling
in both synchronous and deferrable modes. Both modes return the same execution
metadata when the calculation completes successfully and raise an exception
if it fails or is canceled.
Note
When the trigger reports that polling attempts have been exhausted, the
operator requests cancellation of the calculation before failing.
Killing the task while it is deferred does not invoke on_kill(),
so the calculation may continue running in Athena.
Sensors¶
Wait for an Athena Spark calculation¶
Use AthenaSparkSensor
to wait for an existing Athena Spark calculation execution to reach a terminal
state.
The sensor expects a calculation execution ID that was already created, for example by another Airflow task that submits a calculation without waiting, or by a system outside Airflow.
await_spark_calculation = AthenaSparkSensor(
task_id="await_spark_calculation",
calculation_execution_id=calculation_execution_id,
poke_interval=30,
timeout=3600,
)