|
| 1 | +# Copyright 2019 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Example Airflow DAG that checks if a local file exists, creates a Cloud Dataproc cluster, runs the Hadoop |
| 16 | +wordcount example, and deletes the cluster. |
| 17 | +
|
| 18 | +This DAG relies on three Airflow variables |
| 19 | +https://airflow.apache.org/concepts.html#variables |
| 20 | +* gcp_project - Google Cloud Project to use for the Cloud Dataproc cluster. |
| 21 | +* gce_zone - Google Compute Engine zone where Cloud Dataproc cluster should be |
| 22 | + created. |
| 23 | +* gcs_bucket - Google Cloud Storage bucket to use for result of Hadoop job. |
| 24 | + See https://cloud.google.com/storage/docs/creating-buckets for creating a |
| 25 | + bucket. |
| 26 | +""" |
| 27 | + |
| 28 | +import datetime |
| 29 | +import os |
| 30 | + |
| 31 | +from airflow import models |
| 32 | +from airflow.contrib.operators import dataproc_operator |
| 33 | +from airflow.operators import BashOperator |
| 34 | +from airflow.utils import trigger_rule |
| 35 | + |
| 36 | +# Output file for Cloud Dataproc job. |
| 37 | +output_file = os.path.join( |
| 38 | + models.Variable.get('gcs_bucket'), 'wordcount', |
| 39 | + datetime.datetime.now().strftime('%Y%m%d-%H%M%S')) + os.sep |
| 40 | +# Path to Hadoop wordcount example available on every Dataproc cluster. |
| 41 | +WORDCOUNT_JAR = ( |
| 42 | + 'file:///usr/lib/hadoop-mapreduce/hadoop-mapreduce-examples.jar' |
| 43 | +) |
| 44 | + |
| 45 | +# Path to input file for Hadoop job. |
| 46 | +input_file = '/home/airflow/gcs/data/rose.txt’ |
| 47 | + |
| 48 | +# Arguments to pass to Cloud Dataproc job. |
| 49 | +wordcount_args = ['wordcount', input_file, output_file] |
| 50 | + |
| 51 | +yesterday = datetime.datetime.combine( |
| 52 | + datetime.datetime.today() - datetime.timedelta(1), |
| 53 | + datetime.datetime.min.time()) |
| 54 | + |
| 55 | +default_dag_args = { |
| 56 | + # Setting start date as yesterday starts the DAG immediately when it is |
| 57 | + # detected in the Cloud Storage bucket. |
| 58 | + 'start_date': yesterday, |
| 59 | + # To email on failure or retry set 'email' arg to your email and enable |
| 60 | + # emailing here. |
| 61 | + 'email_on_failure': False, |
| 62 | + 'email_on_retry': False, |
| 63 | + # If a task fails, retry it once after waiting at least 5 minutes |
| 64 | + 'retries': 1, |
| 65 | + 'retry_delay': datetime.timedelta(minutes=5), |
| 66 | + 'project_id': models.Variable.get('gcp_project') |
| 67 | +} |
| 68 | + |
| 69 | +with models.DAG( |
| 70 | + 'Composer_sample_quickstart', |
| 71 | + # Continue to run DAG once per day |
| 72 | + schedule_interval=datetime.timedelta(days=1), |
| 73 | + default_args=default_dag_args) as dag: |
| 74 | + |
| 75 | + # Check if the input file exists. |
| 76 | + check_file_existence = BashOperator( |
| 77 | + task_id=’check_file_existence’, |
| 78 | + bash_command=’if [ ! -f \“{}\” ]; then exit 1; fi’.format(input_file)) |
| 79 | + |
| 80 | + # Create a Cloud Dataproc cluster. |
| 81 | + create_dataproc_cluster = dataproc_operator.DataprocClusterCreateOperator( |
| 82 | + task_id='create_dataproc_cluster', |
| 83 | + # Give the cluster a unique name by appending the date scheduled. |
| 84 | + # See https://airflow.apache.org/code.html#default-variables |
| 85 | + cluster_name='quickstart-cluster-{{ ds_nodash }}', |
| 86 | + num_workers=2, |
| 87 | + zone=models.Variable.get('gce_zone'), |
| 88 | + master_machine_type='n1-standard-1', |
| 89 | + worker_machine_type='n1-standard-1') |
| 90 | + |
| 91 | + |
| 92 | + # Run the Hadoop wordcount example installed on the Cloud Dataproc cluster |
| 93 | + # master node. |
| 94 | + run_dataproc_hadoop = dataproc_operator.DataProcHadoopOperator( |
| 95 | + task_id='run_dataproc_hadoop', |
| 96 | + main_jar=WORDCOUNT_JAR, |
| 97 | + cluster_name='quickstart-cluster-{{ ds_nodash }}', |
| 98 | + arguments=wordcount_args) |
| 99 | + |
| 100 | + # Delete Cloud Dataproc cluster. |
| 101 | + delete_dataproc_cluster = dataproc_operator.DataprocClusterDeleteOperator( |
| 102 | + task_id='delete_dataproc_cluster', |
| 103 | + cluster_name='quickstart-cluster-{{ ds_nodash }}', |
| 104 | + # Setting trigger_rule to ALL_DONE causes the cluster to be deleted |
| 105 | + # even if the Dataproc job fails. |
| 106 | + trigger_rule=trigger_rule.TriggerRule.ALL_DONE) |
| 107 | + |
| 108 | + # Define DAG dependencies. |
| 109 | + check_file_existence >> create_dataproc_cluster >> run_dataproc_hadoop >> delete_dataproc_cluster |
| 110 | + |
0 commit comments