concept datum in category apache airflow

This is an excerpt from Manning's book Data Pipelines with Apache Airflow.
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 impliciting task order is also enforced when running this data process.
Figure 15.1 Docker Compose file creates several services, our task is to load data from the REST API & file share, and transform it to eventually view the fastest method of transportation on the result webpage.
![]()
Our goal throughout this chapter to use these “building blocks” to extract data from the REST API and file share, and develop a data pipeline connecting these dots. We choose for MinIO since AWS S3 is often used for data storage, and MinIO supports the S3 protocol. Results of the analysis will be written to the PostgreSQL database, which the result webpage will query to display the results. To get started, ensure your current directory holds the docker-compose.yml file, and create all containers:
Listing 15.11
import logging from airflow.models import BaseOperator from airflow.utils.decorators import apply_defaults class PandasOperator(BaseOperator): template_fields = ( "_input_callable_kwargs", #A "_transform_callable_kwargs", #A "_output_callable_kwargs", #A ) @apply_defaults def __init__( self, input_callable, output_callable, transform_callable=None, input_callable_kwargs=None, transform_callable_kwargs=None, output_callable_kwargs=None, **kwargs, ): super().__init__(**kwargs) # Attributes for reading data self._input_callable = input_callable self._input_callable_kwargs = input_callable_kwargs or {} # Attributes for transformations self._transform_callable = transform_callable self._transform_callable_kwargs = transform_callable_kwargs or {} # Attributes for writing data self._output_callable = output_callable self._output_callable_kwargs = output_callable_kwargs or {} def execute(self, context): df = self._input_callable(**self._input_callable_kwargs) #B logging.info("Read DataFrame with shape: %s.", df.shape) if self._transform_callable: df = self._transform_callable(df, **self._transform_callable_kwargs) #C logging.info("DataFrame shape after transform: %s.", df.shape) self._output_callable(df, **self._output_callable_kwargs) #D
As you read in the previous section, we created folders “raw” and “processed” in a bucket named “datalake”. How did we get to those and why? In terms of efficiency, we could in principle write a single Python function which extracts data, transforms it, and writes the results to a database, all while keeping the data in memory and never touching the file system. This would be much faster, so why don’t we?
First, data is often used by more than one person or data pipeline. In order to distribute and reuse it, it’s stored in a location where other people and processes can read the data from.
But more importantly, we want to make our pipeline reproducible. And what does “reproducibility” imply in terms of a data pipeline? Data is never perfect and software is always in progress - this means we want to be able to go back to previous DAG runs and re-run a pipeline with the data that processed back then. If we’re extracting data from a web service such as a REST API which only returns a result for the state at that given point in time, we cannot go back to the API and ask “please give me the same result you gave me two months ago”.
In that situation, it’s best to keep an unedited copy of the result. For privacy reasons, sometimes certain parts of the data are redacted which is inevitable, but the starting point of a reproducible data pipeline should be to store an - as little as possible edited - copy of the input data. This data is typically stored in a “raw” folder.