← GCP Data Engineering

GCP Data Engineering · Chapter 04

Orchestrating Batch Loads with Cloud Composer

Chapter 3 loaded data by hand. Orchestration automates it: what runs, in what order, on what schedule, and what happens when something is late or fails. Airflow is the tool, and Cloud Composer is Airflow managed by Google.

0 of – checks answered

01

How Airflow thinks

Airflow does three jobs: chain tasks by their dependencies, schedule them, and integrate with other systems. All three are written in Python. Learning Cloud Composer is, practically, learning Airflow.

A workflow written as Python

A directed acyclic graph is a set of tasks chained by their dependencies, plus a schedule. "Directed" means arrows have a direction; "acyclic" means no loops. Every DAG has an ID that must be unique across the whole environment.

Defined by
DAG ID, owner, start date, schedule
Each execution
A "DAG Run"
Written in
Python, not drag-and-drop
Why code
Testable, versioned in git, automatable

02

Run the bike-sharing DAG

This is the Level 3 pipeline from the book: three tables loaded in parallel, then the fact and dimension tables built once everything has landed, each followed by a row-count check. Click a ready task to run it, or run everything that is ready at once.

    Grey tasks are waiting on upstream tasks, blue ones are ready, green ones are done. Tasks with no dependency on each other can run side by side, which is why "run all ready" finishes the graph in far fewer rounds than one-at-a-time.

    03

    The five levels

    The chapter builds orchestration maturity step by step. You can follow every practice or none; Airflow will not stop you. Each level adds one idea.

    Create a DAG and deploy it

    A dummy workflow with two Bash tasks. Learn how a DAG, task and dependency look in code, how timing works, and how to deploy by copying the file to the environment’s dags/ folder.

    New idea
    DAG, tasks, >>, deployment
    Watch out
    Start date and schedule interplay
    Tip
    Version DAG names (_v1) when there is no built-in versioning
    Tools
    Airflow UI: Clear, View Log

    04

    Time, reruns and idempotency

    Data pipelines constantly touch the past. Three Airflow terms, one confusing date rule, and one property that makes reruns safe.

    Start date and catchup calculator

    The key idea: a run is labelled by the interval it covers (its execution date) and fires when that interval has ended. So the "today" run happens tomorrow, and a start date of yesterday makes a daily DAG run straight away.

    Backfill, rerun or catchup?

    1. 01 A pipeline has run daily for a week. The business now wants the previous month loaded too, without disturbing the running schedule.

    2. 02 Last Tuesday’s load failed. You click Clear on that run in the Airflow UI.

    3. 03 You deploy a new DAG whose start date is two weeks ago, and Airflow immediately launches runs for every missed day.

    Is this task idempotent?

    A task is idempotent if running it again gives the same result. That is what makes reruns and backfills safe.

    1. 01 Each daily run appends that day’s events to the whole table with WRITE_APPEND. A run is repeated.

    2. 02 Each daily run writes with WRITE_TRUNCATE to only that day’s partition of a date-partitioned table.

    3. 03 Each run truncates and reloads the entire 1 TB table from all files.

    Partitioning divides a table into segments by a key: a date or timestamp column, ingestion time, or an integer range. Daily is the usual grain. Loading to table$20210101 targets a single day’s partition, so truncating rewrites one day, not the whole table. Partitioning also cuts query cost (Chapter 10).

    05

    Best practice

    Python inside a DAG can do anything, which is exactly the danger. Keep DAGs as configuration that triggers other systems.

    Good practice or avoid?

    1. 01 Use the BigQuery native operators for every load and transform step.

    2. 02 Download a Cloud Storage file to the worker’s disk with the storage client, then process it in Python.

    3. 03 Set priority='BATCH' on BigQuery query tasks in pipelines.

    4. 04 Load a BigQuery table into a pandas DataFrame inside a task for a quick calculation.

    Where should this value live?

    1. 01 A bucket name and dataset names shared by many DAGs, editable in the Airflow UI as one JSON value.

    2. 02 A constant that only one DAG needs, written at the top of its script.

    3. 03 A Cloud SQL instance name supplied when the Composer environment is created or updated.

    LevelSet where
    DAG variable One DAG onlyIn the DAG script
    Airflow variable All DAGsAirflow UI, Admin then Variables (JSON recommended)
    Environment variable Whole environmentComposer environment settings or at deployment
    Macro Per DAG runFilled in by Airflow, for example the execution date

    Other habits from the chapter: keep table schemas in JSON files under the environment’s data/ folder, version DAG names when you change them, add row-count check tasks so an empty result fails the run, and set use_legacy_sql to false.

    06

    Hands-on: the five levels

    Do these in your own project. The main cost is the environment itself, so work in a focused block of time and delete it at the end.

    1. Environments cost money for as long as they exist, not per DAG or task. Create one only when you are ready to work, and delete it afterwards. Pick a current image version and a region close to your bucket and BigQuery.

    2. Look at Tree View, Graph View, the run indicators (circles are runs, squares are tasks) and the Code button.

    3. Write the DAG, then copy it to the environment’s dags/ folder.

      
      						gcloud composer environments storage dags import \
        --environment YOUR_ENV --location YOUR_REGION \
        --source level_1_dag.py
      					
    4. Clear a run circle (reruns the DAG) and a task square (reruns that task), then read a task log.

    5. Chain three native-operator tasks. Use standard SQL and batch priority. Recreate the Cloud SQL source only if you need it, and delete it afterwards.

    6. Create one Airflow variable holding JSON, use the execution-date macro, load three tables in parallel, then build the fact and dimension tables and add a check task after each.

    7. Partition the table by date and truncate only the run date’s partition. Rerun a day and prove there are no duplicates.

    8. Have the first DAG write a success file; have the second start on the same schedule but wait on it.

    9. This is the step that saves you money.

    07

    Key terms

    Orchestration
    Automating tasks, their dependencies and their schedule.
    Airflow
    Open-source workflow tool where workflows are Python code.
    Cloud Composer
    Google’s managed Airflow.
    DAG / DAG Run
    A workflow definition, and one execution of it.
    Operator / task
    A connector to a system, and one configured use of it.
    Cron
    Five-field schedule syntax: minute, hour, day, month, weekday.
    Execution date
    The interval a run is responsible for, not the wall-clock time.
    Backfill / rerun / catchup
    Load the past; repeat something that ran; auto-run missed intervals.
    Idempotent
    Safe to run repeatedly with the same result.
    Partition
    A segment of a table split by a date, timestamp or integer key.
    Sensor
    A task that waits for a condition before continuing.
    Macro
    A template value such as the execution date, filled in at run time.

    08

    Check yourself

    Pick an answer to see the reasoning straight away.

    Q1

    What is a DAG in Airflow?

    Q2

    Where must a DAG file go for Composer to pick it up and run it?

    Q3

    Why is it a bad idea to process large data with pandas inside a DAG task?

    Q4

    A run for one day fails and you rerun it. Which load design leaves the table correct?

    Q5

    Why use a sensor between dependent DAGs instead of a later fixed schedule?

    Q6

    You want a daily-midnight DAG to run for today’s date as soon as you deploy it. What start date follows Airflow’s rule?

    Study notes in my own words, following the structure of Adi Widjaja's Data Engineering with Google Cloud Platform (Packt, 2022). GCP changes quickly, so check each service's current documentation before relying on names, prices or console steps.