|
| 1 | +# Copyright 2018 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 | +"""Demonstrates how to use connections in an Airflow DAG.""" |
| 16 | + |
| 17 | +import datetime |
| 18 | + |
| 19 | +from airflow import models |
| 20 | +from airflow.contrib.operators import bigquery_operator |
| 21 | + |
| 22 | + |
| 23 | +yesterday = datetime.datetime.combine( |
| 24 | + datetime.datetime.today() - datetime.timedelta(1), |
| 25 | + datetime.datetime.min.time()) |
| 26 | + |
| 27 | +default_dag_args = { |
| 28 | + # Setting start date as yesterday starts the DAG immediately when it is |
| 29 | + # detected in the Cloud Storage bucket. |
| 30 | + 'start_date': yesterday, |
| 31 | +} |
| 32 | + |
| 33 | +# Define a DAG (directed acyclic graph) of tasks. |
| 34 | +# Any task you create within the context manager is automatically added to the |
| 35 | +# DAG object. |
| 36 | +with models.DAG( |
| 37 | + 'composer_sample_connections', |
| 38 | + schedule_interval=datetime.timedelta(days=1), |
| 39 | + default_args=default_dag_args) as dag: |
| 40 | + # [START composer_connections_default] |
| 41 | + task_default = bigquery_operator.BigQueryOperator( |
| 42 | + task_id='task_default_connection', |
| 43 | + bql='SELECT 1', use_legacy_sql=False) |
| 44 | + # [END composer_connections_default] |
| 45 | + # [START composer_connections_explicit] |
| 46 | + task_explicit = bigquery_operator.BigQueryOperator( |
| 47 | + task_id='task_explicit_connection', |
| 48 | + bql='SELECT 1', use_legacy_sql=False, |
| 49 | + # Composer creates a 'google_cloud_default' connection by default. |
| 50 | + bigquery_conn_id='google_cloud_default') |
| 51 | + # [END composer_connections_explicit] |
| 52 | + # [START composer_connections_custom] |
| 53 | + task_custom = bigquery_operator.BigQueryOperator( |
| 54 | + task_id='task_custom_connection', |
| 55 | + bql='SELECT 1', use_legacy_sql=False, |
| 56 | + # Set a connection ID to use a connection that you have created. |
| 57 | + bigquery_conn_id='my_gcp_connection') |
| 58 | + # [END composer_connections_custom] |
0 commit comments