You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2020/11/07 22:16:15 UTC

[GitHub] [airflow] ldacey commented on a change in pull request #12170: Added a second example for TaskFlow API

ldacey commented on a change in pull request #12170:
URL: https://github.com/apache/airflow/pull/12170#discussion_r519226202



##########
File path: airflow/example_dags/tutorial_taskflow_api_pandas.py
##########
@@ -0,0 +1,153 @@
+#
+# 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.
+
+# pylint: disable=missing-function-docstring
+"""
+### TaskFlow API Example using Pandas
+
+This is a simple ETL data pipeline example which demonstrates the use of the
+TaskFlow API using three simple tasks for Extract, Transform, and Load, while
+using Pandas and loading data from files.
+
+"""
+# [START tutorial]
+# [START import_module]
+import pandas as pd
+
+# The DAG object; we'll need this to instantiate a DAG
+from airflow import DAG
+from airflow.operators.python_operator import PythonOperator
+from airflow.sensors.filesystem import FileSensor
+from airflow.utils.dates import days_ago
+
+# [END import_module]
+
+
+def generate_data_file():
+    tmp_data_file = open('/tmp/order_data.csv', 'w')
+    tmp_data_file.write('order_id,order_value\n')
+    tmp_data_file.write('"1001", 301.27\n')
+    tmp_data_file.write('"1002", 433.21\n')
+    tmp_data_file.write('"1003", 502.22\n')
+    tmp_data_file.close()
+
+
+# [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',
+}
+# [END default_args]
+
+
+# [START instantiate_dag]
+with DAG(
+    'tutorial_taskflow_api_pandas',
+    default_args=default_args,
+    description='TaskFlow API ETL Pandas',
+    schedule_interval=None,
+    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():
+        """
+        #### Extract task
+        A simple Extract task to get data ready for the rest of the data
+        pipeline. In this case, data is read from a file.
+        """
+        order_data_file = '/tmp/order_data.csv'
+
+        order_data_df = pd.read_csv(order_data_file)
+
+        # convert to JSON so that it be returned using xcom
+        order_data_df_str = order_data_df.to_json(orient='split')

Review comment:
       Looks cool. Yeah, I generally pass xcoms with blob paths or filters (for parquet datasets) in order pass data to other tasks and would not really ever pass an actual dataframe or even json version of the dataframe through xcom.
   
   Is it feasible for Airflow to support generating tasks similar to .pipe() with pandas (https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.pipe.html)? Or how your write queries with Spark.
   
   `etl = (extract().pipe(transform).pipe(load, var="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:
users@infra.apache.org