|
| 1 | +# Copyright 2020 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 | +# [START composer_dataproc_workflow_instantiate_operator_tutorial] |
| 16 | + |
| 17 | +"""Example Airflow DAG that kicks off a Cloud Dataproc Template that runs a |
| 18 | +Spark Pi Job. |
| 19 | +
|
| 20 | +This DAG relies on an Airflow variable |
| 21 | +https://airflow.apache.org/concepts.html#variables |
| 22 | +* project_id - Google Cloud Project ID to use for the Cloud Dataproc Template. |
| 23 | +""" |
| 24 | + |
| 25 | +import datetime |
| 26 | + |
| 27 | +from airflow import models |
| 28 | +from airflow.contrib.operators import dataproc_operator |
| 29 | +from airflow.utils.dates import days_ago |
| 30 | + |
| 31 | +project_id = models.Variable.get("project_id") |
| 32 | + |
| 33 | + |
| 34 | +default_args = { |
| 35 | + # Tell airflow to start one day ago, so that it runs as soon as you upload it |
| 36 | + "start_date": days_ago(1), |
| 37 | + "project_id": project_id, |
| 38 | +} |
| 39 | + |
| 40 | +# Define a DAG (directed acyclic graph) of tasks. |
| 41 | +# Any task you create within the context manager is automatically added to the |
| 42 | +# DAG object. |
| 43 | +with models.DAG( |
| 44 | + # The id you will see in the DAG airflow page |
| 45 | + "dataproc_workflow_dag", |
| 46 | + default_args=default_args, |
| 47 | + # The interval with which to schedule the DAG |
| 48 | + schedule_interval=datetime.timedelta(days=1), # Override to match your needs |
| 49 | +) as dag: |
| 50 | + |
| 51 | + start_template_job = dataproc_operator.DataprocWorkflowTemplateInstantiateOperator( |
| 52 | + # The task id of your job |
| 53 | + task_id="dataproc_workflow_dag", |
| 54 | + # The template id of your workflow |
| 55 | + template_id="sparkpi", |
| 56 | + project_id=project_id, |
| 57 | + # The region for the template |
| 58 | + region="us-central1", |
| 59 | + ) |
| 60 | + |
| 61 | +# [END composer_dataproc_workflow_instantiate_operator_tutorial] |
0 commit comments