Bird
Raised Fist0
GCPcloud~10 mins

Dataflow for stream/batch processing in GCP - Commands & Configuration

Choose your learning style10 modes available
Introduction
Dataflow helps you process data as it arrives or in batches. It makes it easy to handle large amounts of data without managing servers.
When you want to analyze live data from sensors or user activity in real time.
When you need to clean and transform large files stored in cloud storage.
When you want to combine data from different sources and prepare it for reports.
When you want to build a pipeline that runs automatically on a schedule.
When you want to scale your data processing without worrying about infrastructure.
Config File - dataflow_pipeline.py
dataflow_pipeline.py
import apache_beam as beam
from apache_beam.options.pipeline_options import PipelineOptions

class SimpleOptions(PipelineOptions):
    @classmethod
    def _add_argparse_args(cls, parser):
        parser.add_argument('--input', help='Input file to process')
        parser.add_argument('--output', help='Output file to write results')


def run():
    options = PipelineOptions()
    simple_options = options.view_as(SimpleOptions)

    with beam.Pipeline(options=options) as p:
        (p
         | 'ReadInput' >> beam.io.ReadFromText(simple_options.input)
         | 'FilterEmpty' >> beam.Filter(lambda line: line.strip() != '')
         | 'WriteOutput' >> beam.io.WriteToText(simple_options.output))

if __name__ == '__main__':
    run()

This Python file defines a simple Dataflow pipeline using Apache Beam.

  • SimpleOptions: Defines input and output file arguments.
  • run function: Creates the pipeline, reads lines from the input file, filters out empty lines, and writes the results to the output file.
  • This pipeline can run on Google Cloud Dataflow service or locally.
Commands
This command runs the Dataflow pipeline locally or on the cloud, reading from the input file and writing filtered results to the output location.
Terminal
python3 dataflow_pipeline.py --input gs://example-bucket/input.txt --output gs://example-bucket/output/result
Expected OutputExpected
INFO:apache_beam.runners.direct.direct_runner:Starting pipeline execution INFO:apache_beam.runners.direct.direct_runner:Pipeline execution complete
→
--input - Specifies the input file path in Google Cloud Storage.
→
--output - Specifies the output file prefix in Google Cloud Storage.
Lists all Dataflow jobs in your Google Cloud project to check the status of running or completed pipelines.
Terminal
gcloud dataflow jobs list
Expected OutputExpected
JOB_ID NAME TYPE CREATION_TIME STATE 2023-06-01_01_23_45 dataflow_pipeline.py Batch 2023-06-01T01:23:45Z Running
Shows detailed information about a specific Dataflow job, including its current state and any errors.
Terminal
gcloud dataflow jobs describe 2023-06-01_01_23_45
Expected OutputExpected
id: 2023-06-01_01_23_45 name: dataflow_pipeline.py type: Batch currentState: Running startTime: 2023-06-01T01:23:45Z
Key Concept

If you remember nothing else from this pattern, remember: Dataflow lets you build pipelines that process data continuously or in batches without managing servers.

Common Mistakes
Using local file paths instead of cloud storage paths for input/output when running on Dataflow service.
Dataflow service cannot access local files on your computer; it requires files in cloud storage.
Always use Google Cloud Storage paths starting with gs:// for input and output files when running on Dataflow.
Not specifying pipeline options correctly, causing the job to run locally instead of on the cloud.
Without proper options, the pipeline may not use the Dataflow runner and won't scale or run in the cloud.
Use PipelineOptions to specify the Dataflow runner and project details when running on Google Cloud.
Summary
Create a Python pipeline file that reads, processes, and writes data using Apache Beam.
Run the pipeline with input and output paths pointing to Google Cloud Storage.
Use gcloud commands to list and check the status of Dataflow jobs.

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

  1. Step 1: Understand streaming processing

    Streaming processes data continuously as new data arrives in real time.
  2. Step 2: Understand batch processing

    Batch processes data in fixed sets or chunks, not continuously.
  3. Final Answer:

    Streaming processes data continuously as it arrives; batch processes fixed sets of data. -> Option B
  4. 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

  1. Step 1: Identify the correct property for streaming

    In Dataflow Python SDK, streaming mode is enabled by setting streaming = True on StandardOptions.
  2. Step 2: Check other options

    Batch is default and does not require setting batch = True; mode property does not exist; stream = False disables streaming.
  3. Final Answer:

    options.view_as(StandardOptions).streaming = True -> Option D
  4. 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

  1. 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].
  2. Step 2: Confirm batch mode output

    Batch mode processes all data at once, so output will be printed as 3, 5, 7.
  3. Final Answer:

    3, 5, 7 -> Option A
  4. 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

  1. Step 1: Identify streaming mode requirement

    Streaming pipelines require setting streaming=True in options to process live data.
  2. Step 2: Evaluate other options

    Changing input to batch file disables streaming; removing transforms breaks logic; no options means default batch mode.
  3. Final Answer:

    Set options.view_as(StandardOptions).streaming = True before running the pipeline. -> Option A
  4. 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

  1. Step 1: Choose streaming mode for live sensor data

    Live sensor data requires streaming mode to process data as it arrives.
  2. 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.
  3. Final Answer:

    Use streaming mode with a Pub/Sub source, apply a Filter transform, then write to BigQuery sink. -> Option C
  4. Quick Check:

    Streaming + Pub/Sub + Filter + BigQuery = correct pipeline [OK]
Hint: Live data needs streaming + Pub/Sub + Filter + BigQuery [OK]
Common Mistakes:
  • Using batch mode for live data
  • Using Cloud Storage as streaming source
  • Applying Map instead of Filter for filtering
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard