📖 Scenario: You work for a company that collects website visit data. You want to process this data using Google Cloud Dataflow. Sometimes data comes in batches, and sometimes it streams in real-time. You will create a simple Dataflow pipeline configuration to handle both batch and streaming data.
🎯 Goal: Build a Google Cloud Dataflow pipeline configuration that can run in both batch and streaming modes by setting up the input source, pipeline options, and the runner configuration.
📋 What You'll Learn
Create a pipeline options dictionary with the project ID and region
Add a boolean configuration to specify streaming mode
Configure the input source as a Pub/Sub subscription for streaming or a Cloud Storage path for batch
Complete the pipeline options with the runner and input source
💡 Why This Matters
🌍 Real World
Dataflow pipelines are used to process large amounts of data in real-time or batch mode for analytics, monitoring, and reporting.
💼 Career
Understanding how to configure Dataflow pipelines is essential for cloud engineers and data engineers working with Google Cloud Platform.
Progress0 / 4 steps
1
Create initial pipeline options dictionary
Create a dictionary called pipeline_options with these exact entries: 'project': 'my-gcp-project' and 'region': 'us-central1'.
GCP
Hint
Use a Python dictionary with keys 'project' and 'region' and their exact values.
2
Add streaming mode configuration
Add a boolean key 'streaming' with the value True to the existing pipeline_options dictionary.
GCP
Hint
Add the key 'streaming' with value True to the dictionary using square brackets.
3
Configure input source based on streaming mode
Create a variable called input_source. Set it to the string 'projects/my-gcp-project/subscriptions/my-subscription' if pipeline_options['streaming'] is True. Otherwise, set it to the string 'gs://my-bucket/data/*.json'.
GCP
Hint
Use a conditional expression to assign input_source based on the streaming flag.
4
Complete pipeline options with runner and input source
Add the key 'runner' with value 'DataflowRunner' and the key 'input' with the value of input_source to the pipeline_options dictionary.
GCP
Hint
Add the keys 'runner' and 'input' to the dictionary with the specified values.
Practice
(1/5)
1. What is the main difference between streaming and batch processing in Google Cloud Dataflow?
easy
A. Batch processing is faster than streaming in all cases.
B. Streaming processes data continuously as it arrives; batch processes fixed sets of data.
C. Streaming only reads data; batch only writes data.
D. Batch processes data continuously; streaming processes fixed sets of data.
Solution
Step 1: Understand streaming processing
Streaming processes data continuously as new data arrives in real time.
Step 2: Understand batch processing
Batch processes data in fixed sets or chunks, not continuously.
Final Answer:
Streaming processes data continuously as it arrives; batch processes fixed sets of data. -> Option B
Quick Check:
Streaming = continuous, Batch = fixed sets [OK]
Hint: Streaming = live data; batch = fixed data sets [OK]
Common Mistakes:
Confusing batch as continuous processing
Thinking streaming only reads data
Assuming batch is always faster
2. Which option correctly sets a Dataflow pipeline to run in streaming mode using the Python SDK?
easy
A. options.view_as(StandardOptions).batch = True
B. options.view_as(StandardOptions).stream = False
C. options.view_as(StandardOptions).mode = 'batch'
D. options.view_as(StandardOptions).streaming = True
Solution
Step 1: Identify the correct property for streaming
In Dataflow Python SDK, streaming mode is enabled by setting streaming = True on StandardOptions.
Step 2: Check other options
Batch is default and does not require setting batch = True; mode property does not exist; stream = False disables streaming.
Final Answer:
options.view_as(StandardOptions).streaming = True -> Option D
Quick Check:
streaming = True enables streaming mode [OK]
Hint: Set streaming=True in StandardOptions for streaming mode [OK]
Common Mistakes:
Using batch=True which is invalid
Setting mode='batch' which is not a valid property
Confusing stream with streaming property
3. Given this Python Dataflow pipeline snippet, what will be the output if run in batch mode on input [1, 2, 3]?
import apache_beam as beam
with beam.Pipeline() as p:
result = (
p | beam.Create([1, 2, 3])
| beam.Map(lambda x: x * 2)
| beam.Map(lambda x: x + 1)
)
result | beam.Map(print)
medium
A. 3, 5, 7
B. 1, 2, 3
C. 2, 4, 6
D. Error due to missing streaming option
Solution
Step 1: Trace the transformations
Input list is [1, 2, 3]. First Map multiplies each by 2: [2, 4, 6]. Second Map adds 1 to each: [3, 5, 7].
Step 2: Confirm batch mode output
Batch mode processes all data at once, so output will be printed as 3, 5, 7.
Final Answer:
3, 5, 7 -> Option A
Quick Check:
Multiply by 2 then add 1 = 3,5,7 [OK]
Hint: Apply transformations stepwise to input list [OK]
Common Mistakes:
Forgetting to multiply before adding
Assuming streaming mode affects output here
Expecting original input as output
4. You wrote a Dataflow streaming pipeline but it fails to process live data. Which fix is most likely correct?
medium
A. Set options.view_as(StandardOptions).streaming = True before running the pipeline.
B. Change the input source to a batch file instead of a streaming source.
C. Remove all Map transforms to simplify the pipeline.
D. Run the pipeline without specifying any options.
Solution
Step 1: Identify streaming mode requirement
Streaming pipelines require setting streaming=True in options to process live data.
Step 2: Evaluate other options
Changing input to batch file disables streaming; removing transforms breaks logic; no options means default batch mode.
Final Answer:
Set options.view_as(StandardOptions).streaming = True before running the pipeline. -> Option A
Quick Check:
Enable streaming option to fix live data processing [OK]
Hint: Enable streaming=True to fix live data issues [OK]
Common Mistakes:
Not enabling streaming mode in options
Switching to batch input source mistakenly
Removing necessary transforms
5. You want to build a Dataflow pipeline that reads live sensor data, filters out values below 50, and writes results to BigQuery. Which approach is best?
hard
A. Use streaming mode with a Cloud Storage file source, apply a Map transform, then write to BigQuery sink.
B. Use batch mode with a Cloud Storage file source, apply a Map transform, then write to BigQuery sink.
C. Use streaming mode with a Pub/Sub source, apply a Filter transform, then write to BigQuery sink.
D. Use batch mode with a Pub/Sub source, apply a Filter transform, then write to BigQuery sink.
Solution
Step 1: Choose streaming mode for live sensor data
Live sensor data requires streaming mode to process data as it arrives.
Step 2: Select Pub/Sub as source and apply Filter transform
Pub/Sub is the streaming data source; Filter removes values below 50; BigQuery sink stores results.
Final Answer:
Use streaming mode with a Pub/Sub source, apply a Filter transform, then write to BigQuery sink. -> Option C