Created
February 10, 2023 23:51
-
-
Save jbenninghoff/e90c6ae14009f6cbc5a970635377e7a3 to your computer and use it in GitHub Desktop.
Airflow example DAG attach to EMR
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | |
Permission is hereby granted, free of charge, to any person obtaining a copy of | |
this software and associated documentation files (the "Software"), to deal in | |
the Software without restriction, including without limitation the rights to | |
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | |
the Software, and to permit persons to whom the Software is furnished to do so. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | |
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | |
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | |
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
""" | |
from airflow import DAG | |
from airflow.contrib.operators.emr_add_steps_operator import EmrAddStepsOperator | |
from airflow.operators.python_operator import PythonOperator | |
from airflow.contrib.sensors.emr_step_sensor import EmrStepSensor | |
from airflow.contrib.hooks.emr_hook import EmrHook | |
from airflow.utils.dates import days_ago | |
from datetime import timedelta | |
import os | |
DAG_ID = os.path.basename(__file__).replace(".py", "") | |
DEFAULT_ARGS = { | |
'owner': 'airflow', | |
'depends_on_past': False, | |
'email': ['airflow@example.com'], | |
'email_on_failure': False, | |
'email_on_retry': False, | |
} | |
SPARK_STEPS = [ | |
{ | |
'Name': 'calculate_pi', | |
'ActionOnFailure': 'CONTINUE', | |
'HadoopJarStep': { | |
'Jar': 'command-runner.jar', | |
'Args': ['/usr/bin/spark-submit', '--master', 'yarn', '--deploy-mode', 'cluster', '/usr/lib/spark/examples/src/main/python/pi.py', '150'], | |
}, | |
} | |
] | |
# 'Args': ['/usr/lib/spark/bin/run-example', 'SparkPi', '10'], | |
CLUSTER_NAME="My cluster" | |
def find_cluster_fn(**kwargs): | |
hook = EmrHook() | |
return hook.get_cluster_id_by_name(emr_cluster_name=CLUSTER_NAME, | |
cluster_states=['RUNNING','WAITING','STARTING','BOOTSTRAPPING']) | |
with DAG( | |
dag_id=DAG_ID, | |
default_args=DEFAULT_ARGS, | |
dagrun_timeout=timedelta(hours=2), | |
start_date=days_ago(1), | |
schedule_interval='@once', | |
tags=['emr'], | |
) as dag: | |
cluster_creator = PythonOperator(task_id="create_job_flow", | |
python_callable=find_cluster_fn, provide_context=True) | |
step_adder = EmrAddStepsOperator( | |
task_id='add_steps', | |
job_flow_id="{{ task_instance.xcom_pull(task_ids='create_job_flow', key='return_value') }}", | |
aws_conn_id='aws_default', | |
steps=SPARK_STEPS, | |
) | |
step_checker = EmrStepSensor( | |
task_id='watch_step', | |
job_flow_id="{{ task_instance.xcom_pull('create_job_flow', key='return_value') }}", | |
step_id="{{ task_instance.xcom_pull(task_ids='add_steps', key='return_value')[0] }}", | |
aws_conn_id='aws_default', | |
) | |
cluster_creator >> step_adder >> step_checker |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment