Using GCP Operators in Airflow to Manage Data Pipelines
📖 Scenario: You work as a data engineer managing data pipelines on Google Cloud Platform (GCP). You want to automate tasks like uploading files to Google Cloud Storage (GCS), running queries in BigQuery, and launching Dataflow jobs using Apache Airflow.
🎯 Goal: Build an Airflow DAG that uses GCP operators to upload a file to GCS, run a BigQuery SQL query, and start a Dataflow job.
📋 What You'll Learn
Create a Python dictionary with GCP connection details
Define a GCS bucket name variable
Use the GCSCreateBucketOperator to create a bucket
Use the BigQueryExecuteQueryOperator to run a SQL query
Use the DataflowCreatePythonJobOperator to launch a Dataflow job
💡 Why This Matters
🌍 Real World
Automating cloud data workflows is common in data engineering to ensure reliable and repeatable data processing.
💼 Career
Knowing how to use Airflow with GCP operators is valuable for roles involving cloud data pipelines and workflow orchestration.
Progress0 / 4 steps
1
Set up GCP connection details dictionary
Create a Python dictionary called gcp_conn with these exact entries: 'project_id': 'my-gcp-project', 'location': 'us-central1', and 'gcp_conn_id': 'google_cloud_default'.
Apache Airflow
Hint
Use curly braces to create a dictionary and include the exact keys and values as strings.
2
Define the GCS bucket name variable
Create a variable called bucket_name and set it to the string 'my-data-bucket'.
Apache Airflow
Hint
Assign the exact string to the variable bucket_name.
3
Create a GCS bucket using GCSCreateBucketOperator
Write a line that creates a variable called create_bucket and assigns it to a GCSCreateBucketOperator with these parameters: task_id='create_bucket', bucket_name=bucket_name, project_id=gcp_conn['project_id'], and gcp_conn_id=gcp_conn['gcp_conn_id'].
Apache Airflow
Hint
Import the operator and use the exact parameter names and values.
4
Add BigQuery and Dataflow operators to the DAG
Add two variables: run_query assigned to BigQueryExecuteQueryOperator with task_id='run_query', sql='SELECT 1', use_legacy_sql=false, and gcp_conn_id=gcp_conn['gcp_conn_id']; and start_dataflow assigned to DataflowCreatePythonJobOperator with task_id='start_dataflow', py_file='gs://my-data-bucket/dataflow_job.py', location=gcp_conn['location'], and gcp_conn_id=gcp_conn['gcp_conn_id'].
Apache Airflow
Hint
Import both operators and use the exact parameters as shown.
Practice
(1/5)
1. What is the main purpose of GCP operators in Airflow when working with BigQuery, GCS, and Dataflow?
easy
A. To create virtual machines in Google Cloud
B. To manually write SQL queries for BigQuery
C. To replace Google Cloud Console for all cloud services
D. To automate and manage cloud tasks like queries, storage, and data processing
Solution
Step 1: Understand the role of GCP operators in Airflow
GCP operators are designed to automate cloud tasks such as running queries in BigQuery, managing files in GCS, and launching Dataflow jobs.
Step 2: Identify the correct purpose among options
Only To automate and manage cloud tasks like queries, storage, and data processing correctly describes automation and management of cloud tasks, while others describe unrelated or manual tasks.
Final Answer:
To automate and manage cloud tasks like queries, storage, and data processing -> Option D
Quick Check:
GCP operators automate cloud tasks = A [OK]
Hint: GCP operators automate cloud tasks, not manual or unrelated actions [OK]
Common Mistakes:
Thinking GCP operators replace all Google Cloud Console functions
Confusing manual SQL writing with automation
Assuming GCP operators create virtual machines
2. Which of the following is the correct way to import the BigQuery operator in an Airflow DAG?
easy
A. from airflow.providers.google.cloud.operators.bigquery import BigQueryInsertJobOperator
B. import airflow.gcp.bigquery as bq
C. from airflow.operators.bigquery import BigQueryOperator
D. from google.cloud import bigquery
Solution
Step 1: Recall the correct import path for BigQuery operators in Airflow
Airflow uses provider packages; the BigQuery operator is imported from airflow.providers.google.cloud.operators.bigquery.
Step 2: Match the correct operator class and import syntax
from airflow.providers.google.cloud.operators.bigquery import BigQueryInsertJobOperator uses the correct module and operator class name. Options B and C use incorrect paths or outdated names. from google.cloud import bigquery imports the Google Cloud client library, not Airflow operator.
Final Answer:
from airflow.providers.google.cloud.operators.bigquery import BigQueryInsertJobOperator -> Option A
Quick Check:
Correct Airflow BigQuery import = D [OK]
Hint: Airflow GCP operators import from airflow.providers.google.cloud.operators [OK]
Common Mistakes:
Using outdated or incorrect import paths
Confusing Google Cloud client libraries with Airflow operators
Importing non-existent modules
3. Given this Airflow task using BigQueryInsertJobOperator:
A. GCSToBigQueryOperator is used incorrectly to upload to GCS; it loads data from GCS to BigQuery
B. The bucket name must be omitted for this operator
C. source_objects should be a string, not a list
D. destination_project_dataset_table should be a GCS path
Solution
Step 1: Understand GCSToBigQueryOperator purpose
This operator loads data from Google Cloud Storage (GCS) into BigQuery tables; it does not upload files to GCS.
Step 2: Identify the misuse in the task
The task tries to upload a file to GCS using GCSToBigQueryOperator, which is incorrect. Uploading to GCS requires a different operator like GCSUploadOperator.
Final Answer:
GCSToBigQueryOperator is used incorrectly to upload to GCS; it loads data from GCS to BigQuery -> Option A
Quick Check:
GCSToBigQueryOperator loads data from GCS, not upload to GCS [OK]
Using GCSToBigQueryOperator to upload files to GCS
Confusing source_objects type
Misunderstanding destination table format
5. You want to run a Dataflow job in Airflow that reads data from GCS, processes it, and writes results back to BigQuery. Which combination of operators is best to build this workflow?
hard
A. Use DataflowCreatePythonJobOperator to run the Dataflow job, and BigQueryInsertJobOperator to run SQL queries
B. Use DataflowCreatePythonJobOperator to run Dataflow job that reads/writes data, no separate BigQuery operator needed
C. Use DataflowCreateJavaJobOperator to run Dataflow, and GCSToBigQueryOperator to load data from GCS to BigQuery
D. Use GCSCreateBucketOperator to create buckets, then DataflowTemplatedJobStartOperator to run Dataflow
Solution
Step 1: Understand Dataflow job capabilities
Dataflow jobs can read from GCS, process data, and write directly to BigQuery within the same job.
Step 2: Choose the operator that runs the Dataflow job end-to-end
DataflowCreatePythonJobOperator runs Python Dataflow jobs that handle reading and writing data, so no separate BigQuery operator is needed for writing results.
Step 3: Evaluate other options
Use DataflowCreatePythonJobOperator to run the Dataflow job, and BigQueryInsertJobOperator to run SQL queries runs Dataflow and BigQuery separately, which is unnecessary if Dataflow handles both. Use GCSCreateBucketOperator to create buckets, then DataflowTemplatedJobStartOperator to run Dataflow involves bucket creation, not relevant here. Use DataflowCreateJavaJobOperator to run Dataflow, and GCSToBigQueryOperator to load data from GCS to BigQuery uses Java job and GCSToBigQueryOperator, which is redundant.
Final Answer:
Use DataflowCreatePythonJobOperator to run Dataflow job that reads/writes data, no separate BigQuery operator needed -> Option B
Quick Check:
Dataflow job can read/write data itself = B [OK]
Hint: Dataflow jobs can handle GCS to BigQuery in one step [OK]
Common Mistakes:
Running separate BigQuery operators unnecessarily
Confusing Dataflow Java and Python operators
Adding unrelated bucket creation tasks
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.