turbaszek commented on a change in pull request #11308: URL: https://github.com/apache/airflow/pull/11308#discussion_r500432065
########## File path: airflow/example_dags/tutorial_etl_dag.py ########## @@ -0,0 +1,134 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +""" +### ETL DAG Tutorial Documentation +This ETL DAG is compatible with Airflow 1.10.x (specifically tested with 1.10.12) and is referenced +as part of the documentation that goes along with the Airflow Functional DAG tutorial located +[here](https://airflow.apache.org/tutorial_functional_dag.html) +""" +# [START tutorial] Review comment: Will we use the whole example in docs? If yes this is ok 👌 ########## File path: airflow/example_dags/tutorial_etl_dag.py ########## @@ -0,0 +1,134 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +""" +### ETL DAG Tutorial Documentation +This ETL DAG is compatible with Airflow 1.10.x (specifically tested with 1.10.12) and is referenced +as part of the documentation that goes along with the Airflow Functional DAG tutorial located +[here](https://airflow.apache.org/tutorial_functional_dag.html) +""" +# [START tutorial] +# [START import_module] +from datetime import timedelta +import json + +# The DAG object; we'll need this to instantiate a DAG +from airflow import DAG +# Operators; we need this to operate! +from airflow.operators.python_operator import PythonOperator +from airflow.utils.dates import days_ago + +# [END import_module] + +# [START default_args] +# These args will get passed on to each operator +# You can override them on a per-task basis during operator initialization +default_args = { + 'owner': 'airflow', + 'depends_on_past': False, + 'email': ['[email protected]'], + 'email_on_failure': False, + 'email_on_retry': False, + 'retries': 1, + 'retry_delay': timedelta(minutes=5), +} Review comment: Should we limit this to minimum? ########## File path: airflow/example_dags/tutorial_etl_dag.py ########## @@ -0,0 +1,134 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +""" +### ETL DAG Tutorial Documentation +This ETL DAG is compatible with Airflow 1.10.x (specifically tested with 1.10.12) and is referenced +as part of the documentation that goes along with the Airflow Functional DAG tutorial located +[here](https://airflow.apache.org/tutorial_functional_dag.html) +""" +# [START tutorial] +# [START import_module] +from datetime import timedelta +import json + +# The DAG object; we'll need this to instantiate a DAG +from airflow import DAG +# Operators; we need this to operate! +from airflow.operators.python_operator import PythonOperator +from airflow.utils.dates import days_ago + +# [END import_module] + +# [START default_args] +# These args will get passed on to each operator +# You can override them on a per-task basis during operator initialization +default_args = { + 'owner': 'airflow', + 'depends_on_past': False, + 'email': ['[email protected]'], + 'email_on_failure': False, + 'email_on_retry': False, + 'retries': 1, + 'retry_delay': timedelta(minutes=5), +} +# [END default_args] + +# [START instantiate_dag] +with DAG( + 'tutorial_etl_dag', + default_args=default_args, + description='ETL DAG tutorial', + schedule_interval=timedelta(days=1), + start_date=days_ago(2), Review comment: I'm personally in favor of using `None` as schedule in most of examples because when on turns one the example scheduler will not try to create multiple runs ########## File path: airflow/example_dags/tutorial_etl_dag.py ########## @@ -0,0 +1,134 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +""" +### ETL DAG Tutorial Documentation +This ETL DAG is compatible with Airflow 1.10.x (specifically tested with 1.10.12) and is referenced +as part of the documentation that goes along with the Airflow Functional DAG tutorial located +[here](https://airflow.apache.org/tutorial_functional_dag.html) +""" +# [START tutorial] +# [START import_module] +from datetime import timedelta +import json + +# The DAG object; we'll need this to instantiate a DAG +from airflow import DAG +# Operators; we need this to operate! +from airflow.operators.python_operator import PythonOperator +from airflow.utils.dates import days_ago + +# [END import_module] + +# [START default_args] +# These args will get passed on to each operator +# You can override them on a per-task basis during operator initialization +default_args = { + 'owner': 'airflow', + 'depends_on_past': False, + 'email': ['[email protected]'], + 'email_on_failure': False, + 'email_on_retry': False, + 'retries': 1, + 'retry_delay': timedelta(minutes=5), +} +# [END default_args] + +# [START instantiate_dag] +with DAG( + 'tutorial_etl_dag', + default_args=default_args, + description='ETL DAG tutorial', + schedule_interval=timedelta(days=1), + start_date=days_ago(2), Review comment: I'm personally in favor of using `None` as schedule in most of examples because when users turn on one the example DAG scheduler will not try to create multiple runs ########## File path: airflow/example_dags/tutorial_etl_dag.py ########## @@ -0,0 +1,134 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +""" +### ETL DAG Tutorial Documentation +This ETL DAG is compatible with Airflow 1.10.x (specifically tested with 1.10.12) and is referenced +as part of the documentation that goes along with the Airflow Functional DAG tutorial located +[here](https://airflow.apache.org/tutorial_functional_dag.html) +""" +# [START tutorial] +# [START import_module] +from datetime import timedelta +import json + +# The DAG object; we'll need this to instantiate a DAG +from airflow import DAG +# Operators; we need this to operate! +from airflow.operators.python_operator import PythonOperator +from airflow.utils.dates import days_ago + +# [END import_module] + +# [START default_args] +# These args will get passed on to each operator +# You can override them on a per-task basis during operator initialization +default_args = { + 'owner': 'airflow', + 'depends_on_past': False, + 'email': ['[email protected]'], + 'email_on_failure': False, + 'email_on_retry': False, + 'retries': 1, + 'retry_delay': timedelta(minutes=5), +} +# [END default_args] + +# [START instantiate_dag] +with DAG( + 'tutorial_etl_dag', + default_args=default_args, + description='ETL DAG tutorial', + schedule_interval=timedelta(days=1), + start_date=days_ago(2), + tags=['example'], +) as dag: +# [END instantiate_dag] +# [START documentation] + dag.doc_md = __doc__ +# [END documentation] Review comment: Should we move this to DAG invocation? ########## File path: airflow/example_dags/tutorial_etl_dag.py ########## @@ -0,0 +1,134 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +""" +### ETL DAG Tutorial Documentation +This ETL DAG is compatible with Airflow 1.10.x (specifically tested with 1.10.12) and is referenced +as part of the documentation that goes along with the Airflow Functional DAG tutorial located +[here](https://airflow.apache.org/tutorial_functional_dag.html) +""" +# [START tutorial] +# [START import_module] +from datetime import timedelta +import json + +# The DAG object; we'll need this to instantiate a DAG +from airflow import DAG +# Operators; we need this to operate! +from airflow.operators.python_operator import PythonOperator +from airflow.utils.dates import days_ago + +# [END import_module] + +# [START default_args] +# These args will get passed on to each operator +# You can override them on a per-task basis during operator initialization +default_args = { + 'owner': 'airflow', + 'depends_on_past': False, + 'email': ['[email protected]'], + 'email_on_failure': False, + 'email_on_retry': False, + 'retries': 1, + 'retry_delay': timedelta(minutes=5), +} +# [END default_args] + +# [START instantiate_dag] +with DAG( + 'tutorial_etl_dag', + default_args=default_args, + description='ETL DAG tutorial', + schedule_interval=timedelta(days=1), + start_date=days_ago(2), + tags=['example'], +) as dag: +# [END instantiate_dag] +# [START documentation] + dag.doc_md = __doc__ +# [END documentation] + +# [START extract_function] + def extract(**kwargs): Review comment: ```suggestion @task() def extract(**kwargs): ``` ########## File path: airflow/example_dags/tutorial_etl_dag.py ########## @@ -0,0 +1,134 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +""" +### ETL DAG Tutorial Documentation +This ETL DAG is compatible with Airflow 1.10.x (specifically tested with 1.10.12) and is referenced +as part of the documentation that goes along with the Airflow Functional DAG tutorial located +[here](https://airflow.apache.org/tutorial_functional_dag.html) +""" +# [START tutorial] +# [START import_module] +from datetime import timedelta +import json + +# The DAG object; we'll need this to instantiate a DAG +from airflow import DAG +# Operators; we need this to operate! +from airflow.operators.python_operator import PythonOperator +from airflow.utils.dates import days_ago + +# [END import_module] + +# [START default_args] +# These args will get passed on to each operator +# You can override them on a per-task basis during operator initialization +default_args = { + 'owner': 'airflow', + 'depends_on_past': False, + 'email': ['[email protected]'], + 'email_on_failure': False, + 'email_on_retry': False, + 'retries': 1, + 'retry_delay': timedelta(minutes=5), +} +# [END default_args] + +# [START instantiate_dag] +with DAG( + 'tutorial_etl_dag', + default_args=default_args, + description='ETL DAG tutorial', + schedule_interval=timedelta(days=1), + start_date=days_ago(2), + tags=['example'], +) as dag: +# [END instantiate_dag] +# [START documentation] + dag.doc_md = __doc__ +# [END documentation] + +# [START extract_function] + def extract(**kwargs): + ti = kwargs['ti'] + data_string = u'{"1001": 301.27, "1002": 433.21, "1003": 502.22}' + ti.xcom_push('order_data', data_string) +# [END extract_function] + +# [START transform_function] + def transform(**kwargs): Review comment: ```suggestion @task() def transform(**kwargs): ``` ########## File path: airflow/example_dags/tutorial_etl_dag.py ########## @@ -0,0 +1,134 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +""" +### ETL DAG Tutorial Documentation +This ETL DAG is compatible with Airflow 1.10.x (specifically tested with 1.10.12) and is referenced +as part of the documentation that goes along with the Airflow Functional DAG tutorial located +[here](https://airflow.apache.org/tutorial_functional_dag.html) +""" +# [START tutorial] +# [START import_module] +from datetime import timedelta +import json + +# The DAG object; we'll need this to instantiate a DAG +from airflow import DAG +# Operators; we need this to operate! +from airflow.operators.python_operator import PythonOperator +from airflow.utils.dates import days_ago + +# [END import_module] + +# [START default_args] +# These args will get passed on to each operator +# You can override them on a per-task basis during operator initialization +default_args = { + 'owner': 'airflow', + 'depends_on_past': False, + 'email': ['[email protected]'], + 'email_on_failure': False, + 'email_on_retry': False, + 'retries': 1, + 'retry_delay': timedelta(minutes=5), +} +# [END default_args] + +# [START instantiate_dag] +with DAG( + 'tutorial_etl_dag', + default_args=default_args, + description='ETL DAG tutorial', + schedule_interval=timedelta(days=1), + start_date=days_ago(2), + tags=['example'], +) as dag: +# [END instantiate_dag] +# [START documentation] + dag.doc_md = __doc__ +# [END documentation] + +# [START extract_function] + def extract(**kwargs): + ti = kwargs['ti'] + data_string = u'{"1001": 301.27, "1002": 433.21, "1003": 502.22}' + ti.xcom_push('order_data', data_string) +# [END extract_function] + +# [START transform_function] + def transform(**kwargs): + ti = kwargs['ti'] + extract_data_string = ti.xcom_pull(task_ids='extract', key='order_data') + order_data = json.loads(extract_data_string) + + total_order_value = 0 + for value in order_data.values(): + total_order_value += value + + total_value = {"total_order_value": total_order_value} + total_value_json_string = json.dumps(total_value) + ti.xcom_push('total_order_value', total_value_json_string) +# [END transform_function] + +# [START load_function] + def load(**kwargs): + ti = kwargs['ti'] + total_value_string = ti.xcom_pull(task_ids='transform', key='total_order_value') + total_order_value = json.loads(total_value_string) + + print(total_order_value) +# [END load_function] + +# [START extract_task] + extract_task = PythonOperator( + task_id='extract', + python_callable=extract, + ) Review comment: We should use `@task()` decorator instead of `PythonOperator`, that was the main point of functional DAGs ########## File path: airflow/example_dags/tutorial_etl_dag.py ########## @@ -0,0 +1,134 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +""" +### ETL DAG Tutorial Documentation +This ETL DAG is compatible with Airflow 1.10.x (specifically tested with 1.10.12) and is referenced +as part of the documentation that goes along with the Airflow Functional DAG tutorial located +[here](https://airflow.apache.org/tutorial_functional_dag.html) +""" +# [START tutorial] +# [START import_module] +from datetime import timedelta +import json + +# The DAG object; we'll need this to instantiate a DAG +from airflow import DAG +# Operators; we need this to operate! +from airflow.operators.python_operator import PythonOperator +from airflow.utils.dates import days_ago + +# [END import_module] + +# [START default_args] +# These args will get passed on to each operator +# You can override them on a per-task basis during operator initialization +default_args = { + 'owner': 'airflow', + 'depends_on_past': False, + 'email': ['[email protected]'], + 'email_on_failure': False, + 'email_on_retry': False, + 'retries': 1, + 'retry_delay': timedelta(minutes=5), +} +# [END default_args] + +# [START instantiate_dag] +with DAG( + 'tutorial_etl_dag', + default_args=default_args, + description='ETL DAG tutorial', + schedule_interval=timedelta(days=1), + start_date=days_ago(2), + tags=['example'], +) as dag: +# [END instantiate_dag] +# [START documentation] + dag.doc_md = __doc__ +# [END documentation] + +# [START extract_function] + def extract(**kwargs): + ti = kwargs['ti'] + data_string = u'{"1001": 301.27, "1002": 433.21, "1003": 502.22}' + ti.xcom_push('order_data', data_string) +# [END extract_function] + +# [START transform_function] + def transform(**kwargs): + ti = kwargs['ti'] + extract_data_string = ti.xcom_pull(task_ids='extract', key='order_data') + order_data = json.loads(extract_data_string) + + total_order_value = 0 + for value in order_data.values(): + total_order_value += value + + total_value = {"total_order_value": total_order_value} + total_value_json_string = json.dumps(total_value) + ti.xcom_push('total_order_value', total_value_json_string) +# [END transform_function] + +# [START load_function] + def load(**kwargs): + ti = kwargs['ti'] + total_value_string = ti.xcom_pull(task_ids='transform', key='total_order_value') + total_order_value = json.loads(total_value_string) + + print(total_order_value) +# [END load_function] + +# [START extract_task] + extract_task = PythonOperator( + task_id='extract', + python_callable=extract, + ) +# [END basic_task] + extract_task.doc_md = """\ Review comment: If I'm not mistaken we should be able to do: ``` @task(doc_md="here goes the docs") def extract(**kwargs): ``` ... ``` ########## File path: airflow/example_dags/tutorial_etl_dag.py ########## @@ -0,0 +1,134 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +""" +### ETL DAG Tutorial Documentation +This ETL DAG is compatible with Airflow 1.10.x (specifically tested with 1.10.12) and is referenced +as part of the documentation that goes along with the Airflow Functional DAG tutorial located +[here](https://airflow.apache.org/tutorial_functional_dag.html) +""" +# [START tutorial] +# [START import_module] +from datetime import timedelta +import json + +# The DAG object; we'll need this to instantiate a DAG +from airflow import DAG +# Operators; we need this to operate! +from airflow.operators.python_operator import PythonOperator +from airflow.utils.dates import days_ago + +# [END import_module] + +# [START default_args] +# These args will get passed on to each operator +# You can override them on a per-task basis during operator initialization +default_args = { + 'owner': 'airflow', + 'depends_on_past': False, + 'email': ['[email protected]'], + 'email_on_failure': False, + 'email_on_retry': False, + 'retries': 1, + 'retry_delay': timedelta(minutes=5), +} +# [END default_args] + +# [START instantiate_dag] +with DAG( + 'tutorial_etl_dag', + default_args=default_args, + description='ETL DAG tutorial', + schedule_interval=timedelta(days=1), + start_date=days_ago(2), + tags=['example'], +) as dag: +# [END instantiate_dag] +# [START documentation] + dag.doc_md = __doc__ +# [END documentation] + +# [START extract_function] + def extract(**kwargs): + ti = kwargs['ti'] Review comment: If we want to show the functional approach in full swing we should use: ``` from airflow.operators.python import get_current_context def extract(): ctx = get_current_context() ti = ctx['ti'] ... ``` ########## File path: airflow/example_dags/tutorial_etl_dag.py ########## @@ -0,0 +1,134 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +""" +### ETL DAG Tutorial Documentation +This ETL DAG is compatible with Airflow 1.10.x (specifically tested with 1.10.12) and is referenced +as part of the documentation that goes along with the Airflow Functional DAG tutorial located +[here](https://airflow.apache.org/tutorial_functional_dag.html) +""" +# [START tutorial] +# [START import_module] +from datetime import timedelta +import json + +# The DAG object; we'll need this to instantiate a DAG +from airflow import DAG +# Operators; we need this to operate! +from airflow.operators.python_operator import PythonOperator +from airflow.utils.dates import days_ago + +# [END import_module] + +# [START default_args] +# These args will get passed on to each operator +# You can override them on a per-task basis during operator initialization +default_args = { + 'owner': 'airflow', + 'depends_on_past': False, + 'email': ['[email protected]'], + 'email_on_failure': False, + 'email_on_retry': False, + 'retries': 1, + 'retry_delay': timedelta(minutes=5), +} +# [END default_args] + +# [START instantiate_dag] +with DAG( + 'tutorial_etl_dag', + default_args=default_args, + description='ETL DAG tutorial', + schedule_interval=timedelta(days=1), + start_date=days_ago(2), + tags=['example'], +) as dag: +# [END instantiate_dag] +# [START documentation] + dag.doc_md = __doc__ +# [END documentation] + +# [START extract_function] + def extract(**kwargs): + ti = kwargs['ti'] Review comment: If we want to show the functional approach in full swing we should use: ```python from airflow.operators.python import get_current_context def extract(): ctx = get_current_context() ti = ctx['ti'] ... ``` ########## File path: airflow/example_dags/tutorial_etl_dag.py ########## @@ -0,0 +1,134 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +""" +### ETL DAG Tutorial Documentation +This ETL DAG is compatible with Airflow 1.10.x (specifically tested with 1.10.12) and is referenced +as part of the documentation that goes along with the Airflow Functional DAG tutorial located +[here](https://airflow.apache.org/tutorial_functional_dag.html) +""" +# [START tutorial] +# [START import_module] +from datetime import timedelta +import json + +# The DAG object; we'll need this to instantiate a DAG +from airflow import DAG +# Operators; we need this to operate! +from airflow.operators.python_operator import PythonOperator +from airflow.utils.dates import days_ago + +# [END import_module] + +# [START default_args] +# These args will get passed on to each operator +# You can override them on a per-task basis during operator initialization +default_args = { + 'owner': 'airflow', + 'depends_on_past': False, + 'email': ['[email protected]'], + 'email_on_failure': False, + 'email_on_retry': False, + 'retries': 1, + 'retry_delay': timedelta(minutes=5), +} +# [END default_args] + +# [START instantiate_dag] +with DAG( + 'tutorial_etl_dag', + default_args=default_args, + description='ETL DAG tutorial', + schedule_interval=timedelta(days=1), + start_date=days_ago(2), + tags=['example'], +) as dag: +# [END instantiate_dag] +# [START documentation] + dag.doc_md = __doc__ +# [END documentation] + +# [START extract_function] + def extract(**kwargs): + ti = kwargs['ti'] + data_string = u'{"1001": 301.27, "1002": 433.21, "1003": 502.22}' + ti.xcom_push('order_data', data_string) +# [END extract_function] + +# [START transform_function] + def transform(**kwargs): + ti = kwargs['ti'] + extract_data_string = ti.xcom_pull(task_ids='extract', key='order_data') + order_data = json.loads(extract_data_string) + + total_order_value = 0 + for value in order_data.values(): + total_order_value += value + + total_value = {"total_order_value": total_order_value} + total_value_json_string = json.dumps(total_value) + ti.xcom_push('total_order_value', total_value_json_string) +# [END transform_function] + +# [START load_function] + def load(**kwargs): + ti = kwargs['ti'] + total_value_string = ti.xcom_pull(task_ids='transform', key='total_order_value') + total_order_value = json.loads(total_value_string) + + print(total_order_value) +# [END load_function] + +# [START extract_task] + extract_task = PythonOperator( + task_id='extract', + python_callable=extract, + ) +# [END basic_task] + extract_task.doc_md = """\ Review comment: If I'm not mistaken we should be able to do: ``` @task(doc_md="here goes the docs") def extract(**kwargs): ... ``` ########## File path: airflow/example_dags/tutorial_etl_dag.py ########## @@ -0,0 +1,134 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +""" +### ETL DAG Tutorial Documentation +This ETL DAG is compatible with Airflow 1.10.x (specifically tested with 1.10.12) and is referenced +as part of the documentation that goes along with the Airflow Functional DAG tutorial located +[here](https://airflow.apache.org/tutorial_functional_dag.html) +""" +# [START tutorial] +# [START import_module] +from datetime import timedelta +import json + +# The DAG object; we'll need this to instantiate a DAG +from airflow import DAG +# Operators; we need this to operate! +from airflow.operators.python_operator import PythonOperator +from airflow.utils.dates import days_ago + +# [END import_module] + +# [START default_args] +# These args will get passed on to each operator +# You can override them on a per-task basis during operator initialization +default_args = { + 'owner': 'airflow', + 'depends_on_past': False, + 'email': ['[email protected]'], + 'email_on_failure': False, + 'email_on_retry': False, + 'retries': 1, + 'retry_delay': timedelta(minutes=5), +} +# [END default_args] + +# [START instantiate_dag] +with DAG( + 'tutorial_etl_dag', + default_args=default_args, + description='ETL DAG tutorial', + schedule_interval=timedelta(days=1), + start_date=days_ago(2), + tags=['example'], +) as dag: +# [END instantiate_dag] +# [START documentation] + dag.doc_md = __doc__ +# [END documentation] + +# [START extract_function] + def extract(**kwargs): + ti = kwargs['ti'] Review comment: Ok, I see it 😄 ########## File path: airflow/example_dags/tutorial_functional_etl_dag.py ########## @@ -0,0 +1,119 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +""" +### Functional DAG Tutorial Documentation + +This is a simple ETL data pipeline example which demonstrates the use of Functional DAGs +using three simple tasks for Extract, Transform, and Load. + +Documentation that goes along with the Airflow Functional DAG tutorial located +[here](https://airflow.apache.org/tutorial_functional_dag.html) +""" +# [START tutorial] +# [START import_module] +from datetime import timedelta +import json + +# The DAG object; we'll need this to instantiate a DAG +from airflow import DAG +# Operators; we need this to operate! +from airflow.operators.python_operator import PythonOperator +from airflow.utils.dates import days_ago +from airflow.decorators import task + +# [END import_module] + +# [START default_args] +# These args will get passed on to each operator +# You can override them on a per-task basis during operator initialization +default_args = { + 'owner': 'airflow', + 'depends_on_past': False, + 'email': ['[email protected]'], + 'email_on_failure': False, + 'email_on_retry': False, + 'retries': 1, + 'retry_delay': timedelta(minutes=5), +} +# [END default_args] + +# [START instantiate_dag] +with DAG( + 'tutorial_functional_etl_dag', + default_args=default_args, + description='Functional ETL DAG tutorial', + schedule_interval=timedelta(days=1), + start_date=days_ago(2), + tags=['example'], +) as dag: +# [END instantiate_dag] +# [START documentation] + dag.doc_md = __doc__ +# [END documentation] + +# [START extract] + @dag.task() + def extract(): + data_string = u'{"1001": 301.27, "1002": 433.21, "1003": 502.22}' + return data_string +# [END extract] + extract.doc_md = """\ Review comment: If I'm not mistaken we should be able to do: ``` @task(doc_md="here goes the docs") def extract(**kwargs): ... ``` ########## File path: airflow/example_dags/tutorial_functional_etl_dag.py ########## @@ -0,0 +1,119 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +""" +### Functional DAG Tutorial Documentation + +This is a simple ETL data pipeline example which demonstrates the use of Functional DAGs +using three simple tasks for Extract, Transform, and Load. + +Documentation that goes along with the Airflow Functional DAG tutorial located +[here](https://airflow.apache.org/tutorial_functional_dag.html) +""" +# [START tutorial] +# [START import_module] +from datetime import timedelta +import json + +# The DAG object; we'll need this to instantiate a DAG +from airflow import DAG +# Operators; we need this to operate! +from airflow.operators.python_operator import PythonOperator +from airflow.utils.dates import days_ago +from airflow.decorators import task + +# [END import_module] + +# [START default_args] +# These args will get passed on to each operator +# You can override them on a per-task basis during operator initialization +default_args = { + 'owner': 'airflow', + 'depends_on_past': False, + 'email': ['[email protected]'], + 'email_on_failure': False, + 'email_on_retry': False, + 'retries': 1, + 'retry_delay': timedelta(minutes=5), +} +# [END default_args] + +# [START instantiate_dag] +with DAG( + 'tutorial_functional_etl_dag', + default_args=default_args, + description='Functional ETL DAG tutorial', + schedule_interval=timedelta(days=1), + start_date=days_ago(2), + tags=['example'], +) as dag: +# [END instantiate_dag] +# [START documentation] + dag.doc_md = __doc__ +# [END documentation] + +# [START extract] + @dag.task() + def extract(): + data_string = u'{"1001": 301.27, "1002": 433.21, "1003": 502.22}' + return data_string +# [END extract] + extract.doc_md = """\ + #### Extract task + A simple Extract task to get data ready for the rest of the data pipeline. + In this case, getting data is simulated by reading from a hardcoded JSON string. + """ + +# [START transform] + @dag.task() + def transform(order_data_string: str): + order_data = json.loads(order_data_string) + + total_order_value = 0 + for value in order_data.values(): + total_order_value += value + + total_value = {"total_order_value": total_order_value} + total_value_json_string = json.dumps(total_value) + return total_value_json_string +# [END transform] + transform.doc_md = """\ + #### Transform task + A simple Transform task which takes in the collection of order data and computes + the total order value. + """ + +# [START load] + @dag.task() Review comment: I'm wondering if it would be better to define the task before DAG so they can be reusable? No strong opinion here ########## File path: airflow/example_dags/tutorial_functional_etl_dag.py ########## @@ -0,0 +1,119 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +""" +### Functional DAG Tutorial Documentation + +This is a simple ETL data pipeline example which demonstrates the use of Functional DAGs +using three simple tasks for Extract, Transform, and Load. + +Documentation that goes along with the Airflow Functional DAG tutorial located +[here](https://airflow.apache.org/tutorial_functional_dag.html) +""" +# [START tutorial] +# [START import_module] +from datetime import timedelta +import json + +# The DAG object; we'll need this to instantiate a DAG +from airflow import DAG +# Operators; we need this to operate! +from airflow.operators.python_operator import PythonOperator +from airflow.utils.dates import days_ago +from airflow.decorators import task + +# [END import_module] + +# [START default_args] +# These args will get passed on to each operator +# You can override them on a per-task basis during operator initialization +default_args = { + 'owner': 'airflow', + 'depends_on_past': False, + 'email': ['[email protected]'], + 'email_on_failure': False, + 'email_on_retry': False, + 'retries': 1, + 'retry_delay': timedelta(minutes=5), +} +# [END default_args] + +# [START instantiate_dag] +with DAG( + 'tutorial_functional_etl_dag', + default_args=default_args, + description='Functional ETL DAG tutorial', + schedule_interval=timedelta(days=1), + start_date=days_ago(2), + tags=['example'], +) as dag: +# [END instantiate_dag] +# [START documentation] + dag.doc_md = __doc__ +# [END documentation] + +# [START extract] + @dag.task() + def extract(): + data_string = u'{"1001": 301.27, "1002": 433.21, "1003": 502.22}' + return data_string +# [END extract] + extract.doc_md = """\ + #### Extract task + A simple Extract task to get data ready for the rest of the data pipeline. + In this case, getting data is simulated by reading from a hardcoded JSON string. + """ + +# [START transform] + @dag.task() + def transform(order_data_string: str): + order_data = json.loads(order_data_string) + + total_order_value = 0 + for value in order_data.values(): + total_order_value += value + + total_value = {"total_order_value": total_order_value} + total_value_json_string = json.dumps(total_value) + return total_value_json_string +# [END transform] + transform.doc_md = """\ + #### Transform task + A simple Transform task which takes in the collection of order data and computes + the total order value. + """ Review comment: And there will be indentation unless we do: ```suggestion #### Transform task A simple Transform task which takes in the collection of order data and computes the total order value. """ ``` ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
