Data Pipelines with Apache Airflow
By Julian de Ruiter and Bas Harenslak
()
About this ebook
Data Pipelines with Apache Airflow teaches you how to build and maintain effective data pipelines.
A successful pipeline moves data efficiently, minimizing pauses and blockages between tasks, keeping every process along the way operational. Apache Airflow provides a single customizable environment for building and managing data pipelines, eliminating the need for a hodgepodge collection of tools, snowflake code, and homegrown processes. Using real-world scenarios and examples, Data Pipelines with Apache Airflow teaches you how to simplify and automate data pipelines, reduce operational overhead, and smoothly integrate all the technologies in your stack.
Purchase of the print book includes a free eBook in PDF, Kindle, and ePub formats from Manning Publications.
About the technology
Data pipelines manage the flow of data from initial collection through consolidation, cleaning, analysis, visualization, and more. Apache Airflow provides a single platform you can use to design, implement, monitor, and maintain your pipelines. Its easy-to-use UI, plug-and-play options, and flexible Python scripting make Airflow perfect for any data management task.
About the book
Data Pipelines with Apache Airflow teaches you how to build and maintain effective data pipelines. You’ll explore the most common usage patterns, including aggregating multiple data sources, connecting to and from data lakes, and cloud deployment. Part reference and part tutorial, this practical guide covers every aspect of the directed acyclic graphs (DAGs) that power Airflow, and how to customize them for your pipeline’s needs.
What's inside
Build, test, and deploy Airflow pipelines as DAGs
Automate moving and transforming data
Analyze historical datasets using backfilling
Develop custom components
Set up Airflow in production environments
About the reader
For DevOps, data engineers, machine learning engineers, and sysadmins with intermediate Python skills.
About the author
Bas Harenslak and Julian de Ruiter are data engineers with extensive experience using Airflow to develop pipelines for major companies. Bas is also an Airflow committer.
Table of Contents
PART 1 - GETTING STARTED
1 Meet Apache Airflow
2 Anatomy of an Airflow DAG
3 Scheduling in Airflow
4 Templating tasks using the Airflow context
5 Defining dependencies between tasks
PART 2 - BEYOND THE BASICS
6 Triggering workflows
7 Communicating with external systems
8 Building custom components
9 Testing
10 Running tasks in containers
PART 3 - AIRFLOW IN PRACTICE
11 Best practices
12 Operating Airflow in production
13 Securing Airflow
14 Project: Finding the fastest way to get around NYC
PART 4 - IN THE CLOUDS
15 Airflow in the clouds
16 Airflow on AWS
17 Airflow on Azure
18 Airflow in GCP
Related to Data Pipelines with Apache Airflow
Related ebooks
Spark in Action: Covers Apache Spark 3 with Examples in Java, Python, and Scala Rating: 0 out of 5 stars0 ratingsDesigning Cloud Data Platforms Rating: 0 out of 5 stars0 ratingsData Engineering on Azure Rating: 0 out of 5 stars0 ratingsKafka in Action Rating: 0 out of 5 stars0 ratingsAzure Storage, Streaming, and Batch Analytics: A guide for data engineers Rating: 0 out of 5 stars0 ratingsBootstrapping Microservices with Docker, Kubernetes, and Terraform: A project-based guide Rating: 3 out of 5 stars3/5Event Streams in Action: Real-time event systems with Kafka and Kinesis Rating: 0 out of 5 stars0 ratingsServerless Architectures on AWS: With examples using AWS Lambda Rating: 0 out of 5 stars0 ratingsGoogle Cloud Platform in Action Rating: 0 out of 5 stars0 ratingsData Science with Python and Dask Rating: 0 out of 5 stars0 ratingsKubernetes in Action Rating: 0 out of 5 stars0 ratingsPipeline as Code: Continuous Delivery with Jenkins, Kubernetes, and Terraform Rating: 3 out of 5 stars3/5Learn Kubernetes in a Month of Lunches Rating: 0 out of 5 stars0 ratingsAWS Lambda in Action: Event-driven serverless applications Rating: 0 out of 5 stars0 ratingsLogging in Action: With Fluentd, Kubernetes and more Rating: 0 out of 5 stars0 ratingsInfrastructure as Code, Patterns and Practices: With examples in Python and Terraform Rating: 0 out of 5 stars0 ratingsCloud Native Patterns: Designing change-tolerant software Rating: 4 out of 5 stars4/5Terraform in Action Rating: 5 out of 5 stars5/5Microservices in .NET, Second Edition Rating: 0 out of 5 stars0 ratingsDeep Learning with PyTorch Rating: 5 out of 5 stars5/5MongoDB in Action: Covers MongoDB version 3.0 Rating: 0 out of 5 stars0 ratingsAkka in Action Rating: 0 out of 5 stars0 ratingsInfrastructure as Code (IAC) Cookbook Rating: 0 out of 5 stars0 ratingsKubernetes Native Microservices with Quarkus and MicroProfile Rating: 0 out of 5 stars0 ratingsPython Concurrency with asyncio Rating: 0 out of 5 stars0 ratingsNetty in Action Rating: 0 out of 5 stars0 ratingsDocker in Practice, Second Edition Rating: 0 out of 5 stars0 ratingsLearn Amazon Web Services in a Month of Lunches Rating: 0 out of 5 stars0 ratingsLinux in Action Rating: 0 out of 5 stars0 ratingsRust in Action Rating: 4 out of 5 stars4/5
Data Visualization For You
How to Lie with Maps Rating: 4 out of 5 stars4/5Data Visualization: A Practical Introduction Rating: 5 out of 5 stars5/5Effective Data Storytelling: How to Drive Change with Data, Narrative and Visuals Rating: 4 out of 5 stars4/5Data Analytics for Beginners: Introduction to Data Analytics Rating: 4 out of 5 stars4/5Hands-On Data Analysis with Pandas: Efficiently perform data collection, wrangling, analysis, and visualization using Python Rating: 0 out of 5 stars0 ratingsTeach Yourself VISUALLY Power BI Rating: 0 out of 5 stars0 ratingsLearning pandas - Second Edition Rating: 4 out of 5 stars4/5DAX Patterns: Second Edition Rating: 5 out of 5 stars5/5Learning Tableau Rating: 0 out of 5 stars0 ratingsUltimate Azure Data Engineering Rating: 0 out of 5 stars0 ratingsThe Big Book of Dashboards: Visualizing Your Data Using Real-World Business Scenarios Rating: 4 out of 5 stars4/5Simple Data Science (R) Rating: 5 out of 5 stars5/5Data Analytics & Visualization All-in-One For Dummies Rating: 0 out of 5 stars0 ratingsData Structures & Algorithms Interview Questions You'll Most Likely Be Asked Rating: 1 out of 5 stars1/5Learning Tableau 2019 - Third Edition: Tools for Business Intelligence, data prep, and visual analytics, 3rd Edition Rating: 0 out of 5 stars0 ratingsMastering Excel: Excel Apps Rating: 3 out of 5 stars3/5Advanced Splunk Rating: 5 out of 5 stars5/5How to be Clear and Compelling with Data: Principles, Practice and Getting Beyond the Basics Rating: 0 out of 5 stars0 ratingsTableau 10 Business Intelligence Cookbook Rating: 0 out of 5 stars0 ratingsTableau For Dummies Rating: 4 out of 5 stars4/5Fieldwork Handbook: A Practical Guide on the Go Rating: 0 out of 5 stars0 ratingsHow to Become a Data Analyst: My Low-Cost, No Code Roadmap for Breaking into Tech Rating: 0 out of 5 stars0 ratingsSplunk Developer's Guide - Second Edition Rating: 0 out of 5 stars0 ratingsHands-On Time Series Analysis with R: Perform time series analysis and forecasting using R Rating: 0 out of 5 stars0 ratings
Reviews for Data Pipelines with Apache Airflow
0 ratings0 reviews
Book preview
Data Pipelines with Apache Airflow - Julian de Ruiter
inside front cover
Data Pipelines with Apache Airflow
Bas Harenslak and Julian deRuiter
To comment go to liveBook
Manning
Shelter Island
For more information on this and other Manning titles go to
manning.com
Copyright
For online information and ordering of these and other Manning books, please visit manning.com. The publisher offers discounts on these books when ordered in quantity.
For more information, please contact
Special Sales Department
Manning Publications Co.
20 Baldwin Road
PO Box 761
Shelter Island, NY 11964
Email: [email protected]
©2021 by Manning Publications Co. All rights reserved.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps.
♾ Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine.
ISBN: 9781617296901
brief contents
Part 1. Getting started
1 Meet Apache Airflow
2 Anatomy of an Airflow DAG
3 Scheduling in Airflow
4 Templating tasks using the Airflow context
5 Defining dependencies between tasks
Part 2. Beyond the basics
6 Triggering workflows
7 Communicating with external systems
8 Building custom components
9 Testing
10 Running tasks in containers
Part 3. Airflow in practice
11 Best practices
12 Operating Airflow in production
13 Securing Airflow
14 Project: Finding the fastest way to get around NYC
Part 4. In the clouds
15 Airflow in the clouds
16 Airflow on AWS
17 Airflow on Azure
18 Airflow in GCP
appendix A. Running code samples
appendix B. Package structures Airflow 1 and 2
appendix C. Prometheus metric mapping
contents
preface
acknowledgments
about this book
about the authors
about the cover illustration
Part 1. Getting started
1 Meet Apache Airflow
1.1 Introducing data pipelines
Data pipelines as graphs
Executing a pipeline graph
Pipeline graphs vs. sequential scripts
Running pipeline using workflow managers
1.2 Introducing Airflow
Defining pipelines flexibly in (Python) code
Scheduling and executing pipelines
Monitoring and handling failures
Incremental loading and backfilling
1.3 When to use Airflow
Reasons to choose Airflow
Reasons not to choose Airflow
1.4 The rest of this book
2 Anatomy of an Airflow DAG
2.1 Collecting data from numerous sources
Exploring the data
2.2 Writing your first Airflow DAG
Tasks vs. operators
Running arbitrary Python code
2.3 Running a DAG in Airflow
Running Airflow in a Python environment
Running Airflow in Docker containers
Inspecting the Airflow UI
2.4 Running at regular intervals
2.5 Handling failing tasks
3 Scheduling in Airflow
3.1 An example: Processing user events
3.2 Running at regular intervals
Defining scheduling intervals
Cron-based intervals
Frequency-based intervals
3.3 Processing data incrementally
Fetching events incrementally
Dynamic time references using execution dates
Partitioning your data
3.4 Understanding Airflow’s execution dates
Executing work in fixed-length intervals
3.5 Using backfilling to fill in past gaps
Executing work back in time
3.6 Best practices for designing tasks
Atomicity
Idempotency
4 Templating tasks using the Airflow context
4.1 Inspecting data for processing with Airflow
Determining how to load incremental data
4.2 Task context and Jinja templating
Templating operator arguments
What is available for templating?
Templating the PythonOperator
Providing variables to the PythonOperator
Inspecting templated arguments
4.3 Hooking up other systems
5 Defining dependencies between tasks
5.1 Basic dependencies
Linear dependencies
Fan-in/-out dependencies
5.2 Branching
Branching within tasks
Branching within the DAG
5.3 Conditional tasks
Conditions within tasks
Making tasks conditional
Using built-in operators
5.4 More about trigger rules
What is a trigger rule?
The effect of failures
Other trigger rules
5.5 Sharing data between tasks
Sharing data using XComs
When (not) to use XComs
Using custom XCom backends
5.6 Chaining Python tasks with the Taskflow API
Simplifying Python tasks with the Taskflow API
When (not) to use the Taskflow API
Part 2. Beyond the basics
6 Triggering workflows
6.1 Polling conditions with sensors
Polling custom conditions
Sensors outside the happy flow
6.2 Triggering other DAGs
Backfilling with the TriggerDagRunOperator
Polling the state of other DAGs
6.3 Starting workflows with REST/CLI
7 Communicating with external systems
7.1 Connecting to cloud services
Installing extra dependencies
Developing a machine learning model
Developing locally with external systems
7.2 Moving data from between systems
Implementing a PostgresToS3Operator
Outsourcing the heavy work
8 Building custom components
8.1 Starting with a PythonOperator
Simulating a movie rating API
Fetching ratings from the API
Building the actual DAG
8.2 Building a custom hook
Designing a custom hook
Building our DAG with the MovielensHook
8.3 Building a custom operator
Defining a custom operator
Building an operator for fetching ratings
8.4 Building custom sensors
8.5 Packaging your components
Bootstrapping a Python package
Installing your package
9 Testing
9.1 Getting started with testing
Integrity testing all DAGs
Setting up a CI/CD pipeline
Writing unit tests
Pytest project structure
Testing with files on disk
9.2 Working with DAGs and task context in tests
Working with external systems
9.3 Using tests for development
Testing complete DAGs
9.4 Emulate production environments with Whirl
9.5 Create DTAP environments
10 Running tasks in containers
10.1 Challenges of many different operators
Operator interfaces and implementations
Complex and conflicting dependencies
Moving toward a generic operator
10.2 Introducing containers
What are containers?
Running our first Docker container
Creating a Docker image
Persisting data using volumes
10.3 Containers and Airflow
Tasks in containers
Why use containers?
10.4 Running tasks in Docker
Introducing the DockerOperator
Creating container images for tasks
Building a DAG with Docker tasks
Docker-based workflow
10.5 Running tasks in Kubernetes
Introducing Kubernetes
Setting up Kubernetes
Using the KubernetesPodOperator
Diagnosing Kubernetes-related issues
Differences with Docker-based workflows
Part 3. Airflow in practice
11 Best practices
11.1 Writing clean DAGs
Use style conventions
Manage credentials centrally
Specify configuration details consistently
Avoid doing any computation in your DAG definition
Use factories to generate common patterns
Group related tasks using task groups
Create new DAGs for big changes
11.2 Designing reproducible tasks
Always require tasks to be idempotent
Task results should be deterministic
Design tasks using functional paradigms
11.3 Handling data efficiently
Limit the amount of data being processed
Incremental loading/processing
Cache intermediate data
Don’t store data on local file systems
Offload work to external/source systems
11.4 Managing your resources
Managing concurrency using pools
Detecting long-running tasks using SLAs and alerts
12 Operating Airflow in production
12.1 Airflow architectures
Which executor is right for me?
Configuring a metastore for Airflow
A closer look at the scheduler
12.2 Installing each executor
Setting up the SequentialExecutor
Setting up the LocalExecutor
Setting up the CeleryExecutor
Setting up the KubernetesExecutor
12.3 Capturing logs of all Airflow processes
Capturing the webserver output
Capturing the scheduler output
Capturing task logs
Sending logs to remote storage
12.4 Visualizing and monitoring Airflow metrics
Collecting metrics from Airflow
Configuring Airflow to send metrics
Configuring Prometheus to collect metrics
Creating dashboards with Grafana
What should you monitor?
12.5 How to get notified of a failing task
Alerting within DAGs and operators
Defining service-level agreements
Scalability and performance
Controlling the maximum number of running tasks
System performance configurations
Running multiple schedulers
13 Securing Airflow
13.1 Securing the Airflow web interface
Adding users to the RBAC interface
Configuring the RBAC interface
13.2 Encrypting data at rest
Creating a Fernet key
13.3 Connecting with an LDAP service
Understanding LDAP
Fetching users from an LDAP service
13.4 Encrypting traffic to the webserver
Understanding HTTPS
Configuring a certificate for HTTPS
13.5 Fetching credentials from secret management systems
14 Project: Finding the fastest way to get around NYC
14.1 Understanding the data
Yellow Cab file share
Citi Bike REST API
Deciding on a plan of approach
14.2 Extracting the data
Downloading Citi Bike data
Downloading Yellow Cab data
14.3 Applying similar transformations to data
14.4 Structuring a data pipeline
14.5 Developing idempotent data pipelines
Part 4. In the clouds
15 Airflow in the clouds
15.1 Designing (cloud) deployment strategies
15.2 Cloud-specific operators and hooks
15.3 Managed services
Astronomer.io
Google Cloud Composer
Amazon Managed Workflows for Apache Airflow
15.4 Choosing a deployment strategy
16 Airflow on AWS
16.1 Deploying Airflow in AWS
Picking cloud services
Designing the network
Adding DAG syncing
Scaling with the CeleryExecutor
Further steps
16.2 AWS-specific hooks and operators
16.3 Use case: Serverless movie ranking with AWS Athena
Overview
Setting up resources
Building the DAG
Cleaning up
17 Airflow on Azure
17.1 Deploying Airflow in Azure
Picking services
Designing the network
Scaling with the CeleryExecutor
Further steps
17.2 Azure-specific hooks/operators
17.3 Example: Serverless movie ranking with Azure Synapse
Overview
Setting up resources
Building the DAG
Cleaning up
18 Airflow in GCP
18.1 Deploying Airflow in GCP
Picking services
Deploying on GKE with Helm
Integrating with Google services
Designing the network
Scaling with the CeleryExecutor
18.2 GCP-specific hooks and operators
18.3 Use case: Serverless movie ranking on GCP
Uploading to GCS
Getting data into BigQuery
Extracting top ratings
appendix A. Running code samples
appendix B. Package structures Airflow 1 and
appendix C. Prometheus metric mapping
index
front matter
preface
We’ve both been fortunate to be data engineers in interesting and challenging times. For better or worse, many companies and organizations are realizing that data plays a key role in managing and improving their operations. Recent developments in machine learning and AI have opened a slew of new opportunities to capitalize on. However, adopting data-centric processes is often difficult, as it generally requires coordinating jobs across many different heterogeneous systems and tying everything together in a nice, timely fashion for the next analysis or product deployment.
In 2014, engineers at Airbnb recognized the challenges of managing complex data workflows within the company. To address those challenges, they started developing Airflow: an open source solution that allowed them to write and schedule workflows and monitor workflow runs using the built-in web interface.
The success of the Airflow project quickly led to its adoption under the Apache Software Foundation, first as an incubator project in 2016 and later as a top-level project in 2019. As a result, many large companies now rely on Airflow for orchestrating numerous critical data processes.
Working as consultants at GoDataDriven, we’ve helped various clients adopt Airflow as a key component in projects involving the building of data lakes/platforms, machine learning models, and so on. In doing so, we realized that handing over these solutions can be challenging, as complex tools like Airflow can be difficult to learn overnight. For this reason, we also developed an Airflow training program at GoDataDriven, and have frequently organized and participated in meetings to share our knowledge, views, and even some open source packages. Combined, these efforts have helped us explore the intricacies of working with Airflow, which were not always easy to understand using the documentation available to us.
In this book, we aim to provide a comprehensive introduction to Airflow that covers everything from building simple workflows to developing custom components and designing/managing Airflow deployments. We intend to complement many of the excellent blogs and other online documentation by bringing several topics together in one place, using a concise and easy-to-follow format. In doing so, we hope to kickstart your adventures with Airflow by building on top of the experience we’ve gained through diverse challenges over the past years.
acknowledgments
This book would not have been possible without the support of many amazing people. Colleagues from GoDataDriven and personal friends supported us and provided valuable suggestions and critical insights. In addition, Manning Early Access Program (MEAP) readers posted useful comments in the online forum.
Reviewers from the development process also contributed helpful feedback: Al Krinker, Clifford Thurber, Daniel Lamblin, David Krief, Eric Platon, Felipe Ortega, Jason Rendel, Jeremy Chen, Jiri Pik, Jonathan Wood, Karthik Sirasanagandla, Kent R. Spillner, Lin Chen, Philip Best, Philip Patterson, Rambabu Posa, Richard Meinsen, Robert G. Gimbel, Roman Pavlov, Salvatore Campagna, Sebastián Palma Mardones, Thorsten Weber, Ursin Stauss, and Vlad Navitski.
At Manning, we owe special thanks to Brian Sawyer, our acquisitions editor, who helped us shape the initial book proposal and believed in us being able to see it through; Tricia Louvar, our development editor, who was very patient in answering all our questions and concerns, provided critical feedback on each of our draft chapters, and was an essential guide for us throughout this entire journey; and to the rest of the staff as well: Deirdre Hiam, our project editor; Michele Mitchell, our copyeditor; Keri Hales, our proofreader; and Al Krinker, our technical proofreader.
Bas Harenslak
I would like to thank my friends and family for their patience and support during this year-and-a-half adventure that developed from a side project into countless days, nights, and weekends. Stephanie, thank you for always putting up with me working at the computer. Miriam, Gerd, and Lotte, thank you for your patience and belief in me while writing this book. I would also like to thank the team at GoDataDriven for their support and dedication to always learn and improve, I could not have imagined being the author of a book when I started working five years ago.
Julian de Ruiter
First and foremost, I’d like to thank my wife, Anne Paulien, and my son, Dexter, for their endless patience during the many hours that I spent doing just a little more work
on the book. This book would not have been possible without their unwavering support. In the same vein, I’d also like to thank our family and friends for their support and trust. Finally, I’d like to thank our colleagues at GoDataDriven for their advice and encouragement, from whom I’ve also learned an incredible amount in the past years.
about this book
Data Pipelines with Apache Airflow was written to help you implement data-oriented workflows (or pipelines) using Airflow. The book begins with the concepts and mechanics involved in programmatically building workflows for Apache Airflow using the Python programming language. Then the book switches to more in-depth topics such as extending Airflow by building your own custom components and comprehensively testing your workflows. The final part of the book focuses on designing and managing Airflow deployments, touching on topics such as security and designing architectures for several cloud platforms.
Who should read this book
Data Pipelines with Apache Airflow is written both for scientists and engineers who are looking to develop basic workflows in Airflow, as well as engineers interested in more advanced topics such as building custom components for Airflow or managing Airflow deployments. As Airflow workflows and components are built in Python, we do expect readers to have intermediate experience with programming in Python (i.e., have a good working knowledge of building Python functions and classes, understanding concepts such as *args and **kwargs, etc.). Some experience with Docker is also beneficial, as most of our code examples are run using Docker (though they can also be run locally if you wish).
How this book is organized: A road map
The book consists of four sections that cover a total of 18 chapters.
Part 1 focuses on the basics of Airflow, explaining what Airflow is and outlining its basic concepts.
Chapter 1 discusses the concept of data workflows/pipelines and how these can be built using Apache Airflow. It also discusses the advantages and disadvantages of Airflow compared to other solutions, including in which situations you might not want to use Apache Airflow.
Chapter 2 goes into the basic structure of pipelines in Apache Airflow (also known as DAGs), explaining the different components involved and how these fit together.
Chapter 3 shows how you can use Airflow to schedule your pipelines to run at recurring time intervals so that you can (for example) build pipelines that incrementally load new data over time. The chapter also dives into some intricacies in Airflow’s scheduling mechanism, which is often a source of confusion.
Chapter 4 demonstrates how you can use templating mechanisms in Airflow to dynamically include variables in your pipeline definitions. This allows you to reference things such as schedule execution dates within your pipelines.
Chapter 5 demonstrates different approaches for defining relationships between tasks in your pipelines, allowing you to build more complex pipeline structures with branches, conditional tasks, and shared variables.
Part 2 dives deeper into using more complex Airflow topics, including interfacing with external systems, building your own custom components, and designing tests for your pipelines.
Chapter 6 shows how you can trigger workflows in other ways that don’t involve fixed schedules, such as files being loaded or via an HTTP call.
Chapter 7 demonstrates workflows using operators that orchestrate various tasks outside Airflow, allowing you to develop a flow of events through systems that are not connected.
Chapter 8 explains how you can build custom components for Airflow that allow you to reuse functionality across pipelines or integrate with systems that are not supported by Airflow’s built-in functionality.
Chapter 9 discusses various options for testing Airflow workflows, touching on several properties of operators and how to approach these during testing.
Chapter 10 demonstrates how you can use container-based workflows to run pipeline tasks within Docker or Kubernetes and discusses the advantages and disadvantages of these container-based approaches.
Part 3 focuses on applying Airflow in practice and touches on subjects such as best practices, running/securing Airflow, and a final demonstrative use case.
Chapter 11 highlights several best practices to use when building pipelines, which will help you to design and implement efficient and maintainable solutions.
Chapter 12 details several topics to account for when running Airflow in a production setting, such as architectures for scaling out, monitoring, logging, and alerting.
Chapter 13 discusses how to secure your Airflow installation to avoid unwanted access and to minimize the impact in the case a breach occurs.
Chapter 14 demonstrates an example Airflow project in which we periodically process rides from New York City’s Yellow Cab and Citi Bikes to determine the fastest means of transportation between neighborhoods.
Part 4 explores how to run Airflow in several cloud platforms and includes topics such as designing Airflow deployments for the different clouds and how to use built-in operators to interface with different cloud services.
Chapter 15 provides a general introduction by outlining which Airflow components are involved in (cloud) deployments, introducing the idea behind cloud-specific components built into Airflow, and weighing the options of rolling out your own cloud deployment versus using a managed solution.
Chapter 16 focuses on Amazon’s AWS cloud platform, expanding on the previous chapter by designing deployment solutions for Airflow on AWS and demonstrating how specific components can be used to leverage AWS services.
Chapter 17 designs deployments and demonstrates cloud-specific components for Microsoft’s Azure platform.
Chapter 18 addresses deployments and cloud-specific components for Google’s GCP platform.
People new to Airflow should read chapters 1 and 2 to get a good idea of what Airflow is and what it can do. Chapters 3–5 provide important information about Airflow’s key functionality. The rest of the book discusses topics such as building custom components, testing, best practices, and deployments and can be read out of order, based on the reader’s particular needs.
About the code
All source code in listings or text is in a fixed-width font like this to separate it from ordinary text. Sometimes code is also in bold to highlight code that has changed from previous steps in the chapter, such as when a new feature adds to an existing line of code.
In many cases, the original source code has been reformatted; we’ve added line breaks and reworked indentation to accommodate the available page space in the book. In rare cases, even this was not enough, and listings include line-continuation markers (➥). Additionally, comments in the source code have often been removed from the listings when the code is described in the text. Code annotations accompany many of the listings, highlighting important concepts.
References to elements in the code, scripts, or specific Airflow classes/variables/values are often in italics to help distinguish them from the surrounding text.
Source code for all examples and instructions to run them using Docker and Docker Compose are available in our GitHub repository (https://github.com/BasPH/data-pipelines-with-apache-airflow) and can be downloaded via the book’s website (www.manning.com/books/data-pipelines-with-apache-airflow).
Note Appendix A provides more detailed instructions on running the code examples.
All code samples have been tested with Airflow 2.0. Most examples should also run on older versions of Airflow (1.10), with small modifications. Where possible, we have included inline pointers on how to do so. To help you account for differences in import paths between Airflow 2.0 and 1.10, appendix B provides an overview of changed import paths between the two versions.
LiveBook discussion forum
Purchase of Data Pipelines with Apache Airflow includes free access to a private web forum run by Manning Publications where you can make comments about the book, ask technical questions, and receive help from the author and other users. To access the forum and subscribe to it, go to https://livebook.manning.com/#!/book/data-pipelines-with-apache-airflow/discussion. This page provides information on how to get on the forum once you’re registered, what kind of help is available, and its rules of conduct.
Manning’s commitment to our readers is to provide a venue where a meaningful dialogue between individual readers and between readers and the authors can take place. It is not a commitment to any specific amount of participation on the part of the authors, whose contribution to the forum remains voluntary (and unpaid). We suggest you try asking the authors some challenging questions lest their interest stray! The forum and the archives of previous discussions will be accessible from the publisher’s website as long as the book is in print.
about the authors
Bas Harenslak is a data engineer at GoDataDriven, a company developing data-driven solutions located in Amsterdam, Netherlands. With a background in software engineering and computer science, he enjoys working on software and data as if they are challenging puzzles. He favors working on open source software, is a committer on the Apache Airflow project, and is co-organizer of the Amsterdam Airflow meetup.
Julian de Ruiter is a machine learning engineer with a background in computer and life sciences and has a PhD in computational cancer biology. As an experienced software developer, he enjoys bridging the worlds of data science and engineering by using cloud and open source software to develop production-ready machine learning solutions. In his spare time, he enjoys developing his own Python packages, contributing to open source projects, and tinkering with electronics.
about the cover illustration
The figure on the cover of Data Pipelines with Apache Airflow is captioned Femme de l’Isle de Siphanto,
or Woman from Island Siphanto. The illustration is taken from a collection of dress costumes from various countries by Jacques Grasset de Saint-Sauveur (1757–1810), titled Costumes de Différents Pays, published in France in 1797. Each illustration is finely drawn and colored by hand. The rich variety of Grasset de Saint-Sauveur’s collection reminds us vividly of how culturally apart the world’s towns and regions were just 200 years ago. Isolated from each other, people spoke different dialects and languages. In the streets or in the countryside, it was easy to identify where they lived and what their trade or station in life was just by their dress.
The way we dress has changed since then and the diversity by region, so rich at the time, has faded away. It is now hard to tell apart the inhabitants of different continents, let alone different towns, regions, or countries. Perhaps we have traded cultural diversity for a more varied personal life—certainly for a more varied and fast-paced technological life.
At a time when it is hard to tell one computer book from another, Manning celebrates the inventiveness and initiative of the computer business with book covers based on the rich diversity of regional life of two centuries ago, brought back to life by Grasset de Saint-Sauveur’s pictures.
Part 1. Getting started
This part of the book will set the stage for your journey into building pipelines for all kinds of wonderful data processes using Apache Airflow. The first two chapters are aimed at giving you an overview of what Airflow is and what it can do for you.
First, in chapter 1, we’ll explore the concepts of data pipelines and sketch the role Apache Airflow plays in helping you implement these pipelines. To set expectations, we’ll also compare Airflow to several other technologies, and discuss when it might or might not be a good fit for your specific use case. Next, chapter 2 will teach you how to implement your first pipeline in Airflow. After building the pipeline, we’ll also examine how to run this pipeline and monitor its progress using Airflow’s web interface.
Chapters 3–5 dive deeper into key concepts of Airflow to give you a solid understanding of Airflow’s underpinnings.
Chapter 3 focuses on scheduling semantics, which allow you to configure Airflow to run your pipelines at regular intervals. This lets you (for example) write pipelines that load and process data efficiently on a daily, weekly, or monthly basis. Next, in chapter 4, we’ll discuss templating mechanisms in Airflow, which allow you to dynamically reference variables such as execution dates in your pipelines. Finally, in chapter 5, we’ll dive into different approaches for defining task dependencies in your pipelines, which allow you to define complex task hierarchies, including conditional tasks, branches, and so on.
If you’re new to Airflow, we recommend making sure you understand the main concepts described in chapters 3–5, as these are key to using it effectively. Airflow’s scheduling semantics (described in chapter 3) can be especially confusing for new users, as they can be somewhat counterintuitive when first encountered.
After finishing part 1, you should be well-equipped to write your own basic pipelines in Apache Airflow and be ready to dive into some more advanced topics in parts 2–4.
1 Meet Apache Airflow
This chapter covers
Showing how data pipelines can be represented in workflows as graphs of tasks
Understanding how Airflow fits into the ecosystem of workflow managers
Determining if Airflow is a good fit for you
People and companies are continuously becoming more data-driven and are developing data pipelines as part of their daily business. Data volumes involved in these business processes have increased substantially over the years, from megabytes per day to gigabytes per minute. Though handling this data deluge may seem like a considerable challenge, these increasing data volumes can be managed with the appropriate tooling.
This book focuses on Apache Airflow, a batch-oriented framework for building data pipelines. Airflow’s key feature is that it enables you to easily build scheduled data pipelines using a flexible Python framework, while also providing many building blocks that allow you to stitch together the many different technologies encountered in modern technological landscapes.
Airflow is best thought of as a spider in a web: it sits in the middle of your data processes and coordinates work happening across the different (distributed) systems. As such, Airflow is not a data processing tool in itself but orchestrates the different components responsible for processing your data in data pipelines.
In this chapter, we’ll first give you a short introduction to data pipelines in Apache Airflow. Afterward, we’ll discuss several considerations to keep in mind when evaluating whether Airflow is right for you and demonstrate how to make your first steps with Airflow.
1.1 Introducing data pipelines
Data pipelines generally consist of several tasks or actions that need to be executed to achieve the desired result. For example, say we want to build a small weather dashboard that tells us what the weather will be like in the coming week (figure 1.1). To implement this live weather dashboard, we need to perform something like the following steps:
Fetch weather forecast data from a weather API.
Clean or otherwise transform the fetched data (e.g., converting temperatures from Fahrenheit to Celsius or vice versa), so that the data suits our purpose.
Push the transformed data to the weather dashboard.
Figure 1.1 Overview of the weather dashboard use case, in which weather data is fetched from an external API and fed into a dynamic dashboard
As you can see, this relatively simple pipeline already consists of three different tasks that each perform part of the work. Moreover, these tasks need to be executed in a specific order, as it (for example) doesn’t make sense to try transforming the data before fetching it. Similarly, we can’t push any new data to the dashboard until it has undergone the required transformations. As such, we need to make sure that this implicit task order is also enforced when running this data process.
1.1.1 Data pipelines as graphs
One way to make dependencies between tasks more explicit is to draw the data pipeline as a graph. In this graph-based representation, tasks are represented as nodes in the graph, while dependencies between tasks are represented by directed edges between the task nodes. The direction of the edge indicates the direction of the dependency, with an edge pointing from task A to task B, indicating that task A needs to be completed before task B can start. Note that this type of graph is generally called a directed graph, due to the directions in the graph edges.
Applying this graph representation to our weather dashboard pipeline, we can see that the graph provides a relatively intuitive representation of the overall pipeline (figure 1.2). By just quickly glancing at the graph, we can see that our pipeline consists of three different tasks, each corresponding to one of the tasks outlined. Other than this, the direction of the edges clearly indicates the order in which the tasks need to be executed: we can simply follow the arrows to trace the execution.
Figure 1.2 Graph representation of the data pipeline for the weather dashboard. Nodes represent tasks and directed edges represent dependencies between tasks (with an edge pointing from task A to task B, indicating that task A needs to be run before task B).
This type of graph is typically called a directed acyclic graph (DAG), as the graph contains directed edges and does not contain any loops or cycles (acyclic). This acyclic property is extremely important, as it prevents us from running into circular dependencies (figure 1.3) between tasks (where task A depends on task B and vice versa). These circular dependencies become problematic when trying to execute the graph, as we run into a situation where task 2 can only execute once task 3 has been completed, while task 3 can only execute once task 2 has been completed. This logical inconsistency leads to a deadlock type of situation, in which neither task 2 nor 3 can run, preventing us from executing the graph.
Figure 1.3 Cycles in graphs prevent task execution due to circular dependencies. In acyclic graphs (top), there is a clear path to execute the three different tasks. However, in cyclic graphs (bottom), there is no longer a clear execution path due to the interdependency between tasks 2 and 3.
Note that this representation is different from cyclic graph representations, which can contain cycles to illustrate iterative parts of algorithms (for example), as are common in many machine learning applications. However, the acyclic property of DAGs is used by Airflow (and many other workflow managers) to efficiently resolve and execute these graphs of tasks.
1.1.2 Executing a pipeline graph
A nice property of this DAG representation is that it provides a relatively straightforward algorithm that we can use for running the pipeline. Conceptually, this algorithm consists of the following steps:
For each open (= uncompleted) task in the graph, do the following:
For each edge pointing toward the task, check if the upstream
task on the other end of the edge has been completed.
If all upstream tasks have been completed, add the task under consideration to a queue of tasks to be executed.
Execute the tasks in the execution queue, marking them completed once they finish performing their work.
Jump back to step 1 and repeat until all tasks in the graph have been completed.
To see how this works, let’s trace through a small execution of our dashboard pipeline (figure 1.4). On our first loop through the steps of our algorithm, we see that the clean and push tasks still depend on upstream tasks that have not yet been completed. As such, the dependencies of these tasks have not been satisfied, so at this point they can’t be added to the execution queue. However, the fetch task does not have any incoming edges, meaning that it does not have any unsatisfied upstream dependencies and can therefore be added to the execution queue.
Figure 1.4 Using the DAG structure to execute tasks in the data pipeline in the correct order: depicts each task’s state during each of the loops through the algorithm, demonstrating how this leads to the completed execution of the pipeline (end state)
After completing the fetch task, we can start the second loop by examining the dependencies of the clean and push tasks. Now we see that the clean task can be executed as its upstream dependency (the fetch task) has been completed. As such, we can add the task to the execution queue. The push task can’t be added to the queue, as it depends on the clean task, which we haven’t run yet.
In the third loop, after completing the clean task, the push task is finally ready for execution as its upstream dependency on the clean task has now been satisfied. As a result, we can add the task to the execution queue. After the push task has finished executing, we have no more tasks left to execute, thus finishing the execution of the overall pipeline.
1.1.3 Pipeline graphs vs. sequential scripts
Although the graph representation of a pipeline provides an intuitive overview of the tasks in the pipeline and their dependencies, you may find yourself wondering why we wouldn’t just use a simple script to run this linear chain of three steps. To illustrate some advantages of the graph-based approach, let’s jump to a slightly bigger example. In this new use case, we’ve been approached by the owner of an umbrella company, who was inspired by our weather dashboard and would like to try to use machine learning (ML) to increase the efficiency of their operation. To do so, the company owner would like us to implement a data pipeline that creates an ML model correlating umbrella sales with weather patterns. This model can then be used to predict how much demand there will be for the company’s umbrellas in the coming weeks, depending on the weather forecasts for those weeks (figure 1.5).
Figure 1.5 Overview of the umbrella demand use case, in which historical weather and sales data are used to train a model that predicts future sales demands depending on weather forecasts
To build a pipeline for training the ML model, we need to implement something like the following steps:
Prepare the sales data by doing the following:
Fetching the sales data from the source system
Cleaning/transforming the sales data to fit requirements
Prepare the weather data by doing the following:
Fetching the weather forecast data from an API
Cleaning/transforming the weather data to fit requirements
Combine the sales and weather data sets to create the combined data set that can be used as input for creating a predictive ML model.
Train the ML model using the combined data set.
Deploy the ML model so that it can be used by the business.
This pipeline can be represented using the same graph-based representation that we used before, by drawing tasks as nodes and data dependencies between tasks as edges.
One important difference from our previous example is that the first steps of this pipeline (fetching and clearing the weather/sales data) are in fact independent of each other, as they involve two separate data sets. This is clearly illustrated by the two separate branches in the graph representation of the pipeline (figure 1.6), which can be executed in parallel if we apply our graph execution algorithm, making better use of available resources and potentially decreasing the running time of a pipeline compared to executing the tasks sequentially.
Figure 1.6 Independence between sales and weather tasks in the graph representation of the data pipeline for the umbrella demand forecast model. The two sets of fetch/cleaning tasks are independent as they involve two different data sets (the weather and sales data sets). This independence is indicated by the lack of edges between the two sets of tasks.
Another useful property of the graph-based representation is that it clearly separates pipelines into small incremental tasks rather than having one monolithic script or process that does all the work. Although having a single monolithic script may not initially seem like that much of a problem, it can introduce some inefficiencies when tasks in the pipeline fail, as we would have to rerun the entire script. In contrast, in the graph representation, we need only to rerun any failing tasks (and any downstream dependencies).
1.1.4 Running pipeline using workflow managers
Of course, the challenge of running graphs of dependent tasks is hardly a new problem in computing. Over the years, many so-called workflow management
solutions have been developed to tackle this problem, which generally allow you to define and execute graphs of tasks as workflows or pipelines.
Some well-known workflow managers you may have heard of include those listed in table 1.1.
Table 1.1 Overview of several well-known workflow managers and their key characteristics.
Although each of these workflow managers has its own strengths and weaknesses, they all provide similar core functionality that allows you to define and run pipelines containing multiple tasks with dependencies.
One of the key differences between these tools is how they define their workflows. For example, tools such as Oozie use static (XML) files to define workflows, which provides legible workflows but limited flexibility. Other solutions such as Luigi and Airflow allow you to define workflows as code, which provides greater flexibility but can be more challenging to read and test (depending on the coding skills of the person implementing the workflow).
Other key differences lie in the extent of features provided by the workflow manager. For example, tools such as Make and Luigi do not provide built-in support for scheduling workflows, meaning that you’ll need an extra tool like Cron if you want to run your workflow on a recurring schedule. Other tools may provide extra functionality such as scheduling, monitoring, user-friendly web interfaces, and so on built into the platform, meaning that you don’t have to stitch together multiple tools yourself to get these features.
All in all, picking the right workflow management solution for your needs will require some careful consideration of the key features of the different solutions and how they fit your requirements. In the next section, we’ll dive into Airflow—the focus of this book—and explore several key features that make it particularly suited for handling data-oriented workflows or pipelines.
1.2 Introducing Airflow
In this book, we focus on Airflow, an open source solution for developing and monitoring workflows. In this section, we’ll provide a helicopter view of what Airflow does, after which we’ll jump into a more detailed examination of whether it is a good fit for your use case.
1.2.1 Defining pipelines flexibly in (Python) code
Similar to other workflow managers, Airflow allows you to define pipelines or workflows as DAGs of tasks. These graphs are very similar to the examples sketched in the previous section, with tasks being defined as nodes in the graph and dependencies as directed edges between the tasks.
In Airflow, you define your DAGs using Python code in DAG files, which are essentially Python scripts that describe the structure of the corresponding DAG. As such, each DAG file typically describes the set of tasks for a given DAG and the dependencies between the tasks, which are then parsed by Airflow to identify the DAG structure (figure 1.7). Other than this, DAG