5.2 Choosing a Preprocessing Tool: BigQuery, Dataflow, Spark or Python

Key Takeaways

  • BigQuery SQL is the lowest-overhead choice for large structured-data transformations when data already lives in BigQuery.
  • BigQuery DataFrames provides pandas and scikit-learn style APIs that push processing down to BigQuery through SQL.
  • Dataflow runs Apache Beam pipelines for both batch and streaming preprocessing, and its MLTransform class chains ML data transformations such as scaling, vocabularies, and embeddings.
  • Managed Service for Apache Spark, formerly Dataproc, is the natural choice for existing Spark or Hadoop preprocessing code, in serverless or cluster deployments.
  • In-memory Python libraries such as pandas and scikit-learn fit only when the data fits comfortably in a single machine's memory.
Last updated: September 2026

The exam guide lists choosing the right tool for data preprocessing based on scale and complexity, with examples: BigQuery (SQL), Dataflow, Apache Spark, and in-memory Python frameworks. Questions usually give you three signals: data size, batch or streaming, and what code or skills the team already has.

The Options at a Glance

ToolModelBest forWatch out for
BigQuery SQLServerless SQL engineStructured data already in BigQuery: joins, aggregations, window features, filtering at terabyte scaleComplex per-record logic or unstructured data processing
BigQuery ML TRANSFORM / preprocessing functionsSQL functions stored with the modelScaling, bucketizing, and encoding that must match at prediction (Chapter 2)Only applies to BigQuery ML models, or exported models that support the transforms
BigQuery DataFrames (bigframes)pandas and scikit-learn APIs that compile to BigQuery SQLPython-first teams working on big BigQuery tables without loading data into memoryNot every pandas feature is available
Dataflow (Apache Beam)Serverless, autoscaling batch and streaming pipelinesStreaming feature computation from Pub/Sub, large unstructured transforms, the same code for batch and streaming, MLTransform, RunInferenceBeam programming model has a learning curve
Managed Service for Apache Spark (formerly Dataproc)Managed Spark in serverless or cluster deploymentsExisting PySpark or Spark jobs, Spark MLlib, lift-and-shift from HadoopCluster tuning if not serverless
Ray on Agent PlatformManaged Ray clustersDistributed Python for data processing and training in one frameworkCluster sizing, team Ray skills
In-memory Python (pandas, NumPy, scikit-learn)Single machineSmall to medium data, prototyping, complex custom Python logicMemory limits. A 200 GB table won't fit on a notebook VM

Decision Rules

  1. Is the data structured and already in BigQuery? Start with BigQuery SQL, or BigQuery DataFrames if the team prefers pandas syntax. Moving terabytes out of BigQuery to preprocess them elsewhere is a common wrong answer.
  2. Is the data streaming, or must the same logic run on batch and stream? Use Dataflow. Apache Beam lets one pipeline definition handle both, which helps keep features consistent between training (batch backfill) and real-time serving.
  3. Does the organization have a large existing Spark codebase? Use Managed Service for Apache Spark and reuse the code instead of rewriting it.
  4. Is the data small enough for memory and the logic heavily custom? Use in-memory Python in a notebook or a single training job.
  5. Do you need distributed Python across both data processing and training? Consider Ray on Agent Platform (Chapter 16).

Dataflow ML Details Worth Knowing

  • MLTransform wraps several preprocessing operations in one class: computing vocabularies, scaling, and generating embeddings with Agent Platform or Hugging Face models. It needs the Apache Beam Python SDK 2.53.0 or later and default windowing. Transforms built on TensorFlow Transform (TFT) run in batch pipelines.
  • Full-pass transforms (such as scaling with the dataset mean and standard deviation) compute statistics once and save artifacts that can be reapplied later. That's how you avoid recomputing different statistics at serving time.
  • RunInference runs local models (PyTorch, scikit-learn, TensorFlow) or calls a remote Agent Platform endpoint from inside a pipeline, for batch or streaming scoring.

Worked Scenarios

ScenarioBest toolWhy
3 TB of clickstream in BigQuery, need 30-day rolling aggregates per user for a churn modelBigQuery SQL window functionsData is structured and already in BigQuery. Serverless with no data movement
IoT sensor events in Pub/Sub must become features within seconds, and the same logic must backfill historyDataflowStreaming plus batch with one Beam pipeline
2,000 existing PySpark jobs from an on-premises Hadoop clusterManaged Service for Apache SparkReuse Spark code with managed infrastructure
50 MB CSV of lab results with complex domain-specific cleaning in Pythonpandas in a notebookFits in memory, fastest to iterate
Python-savvy analysts want pandas syntax over a 1 TB BigQuery tableBigQuery DataFramespandas API with BigQuery execution
Generate embeddings for 100 million product descriptions as part of an ingestion pipelineDataflow MLTransform with an embedding model, or BigQuery AI.GENERATE_EMBEDDING if data is in BigQueryScalable embedding generation near the data

Keeping Preprocessing Consistent

Choosing a tool isn't only about speed. The exam links preprocessing to training-serving skew:

  • Put transformations where both training and serving can reuse them: BigQuery ML TRANSFORM, saved transformation artifacts, a shared feature pipeline feeding Feature Store, or preprocessing packaged in the serving container (Chapter 13).
  • Avoid writing the same feature logic twice in two languages, such as SQL for training and Java for serving.
  • Version preprocessing code alongside the model and record it in pipeline metadata.

Common Wrong Answers

  • "Export BigQuery to CSV, then preprocess with pandas" for terabyte-scale data. It is slow, fails on memory, and adds a security risk from copied data.
  • "Rewrite existing Spark jobs in Beam" when the requirement is minimal change. Reuse Spark on the managed Spark service.
  • "Use scheduled SQL for real-time features" when the scenario needs seconds-level freshness. That calls for a streaming pipeline.
  • "Run a long-lived self-managed cluster" when a serverless option meets the need. The exam favors managed, Google Cloud-native services when they fit.

Cost and Operations Considerations

  • Serverless first: BigQuery and Dataflow scale automatically and bill for what you use. Serverless Spark removes cluster management.
  • Ephemeral clusters: if you run Spark clusters, create them per job and delete them afterward, and consider Spot VMs for fault-tolerant work.
  • Data locality: process data in the region where it's stored.
  • Orchestrate preprocessing as a pipeline step (Agent Platform Pipelines components exist for BigQuery, Dataflow, and Managed Spark jobs) so runs are repeatable and tracked.
Test Your Knowledge

A ride-sharing company needs driver features computed from a Pub/Sub event stream within seconds for real-time pricing, and the same logic must backfill two years of history for training. Which preprocessing tool fits best?

A
B
C
D
Test Your Knowledge

A bank has 1,500 PySpark preprocessing jobs from its on-premises Hadoop cluster and wants to move them to Google Cloud with minimal code changes. Which service should it choose?

A
B
C
D
Test Your Knowledge

A data science team knows pandas well and needs to explore and transform an 800 GB table in BigQuery without exporting it. What is the most efficient approach?

A
B
C
D