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.
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
| Tool | Model | Best for | Watch out for |
|---|---|---|---|
| BigQuery SQL | Serverless SQL engine | Structured data already in BigQuery: joins, aggregations, window features, filtering at terabyte scale | Complex per-record logic or unstructured data processing |
BigQuery ML TRANSFORM / preprocessing functions | SQL functions stored with the model | Scaling, 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 SQL | Python-first teams working on big BigQuery tables without loading data into memory | Not every pandas feature is available |
| Dataflow (Apache Beam) | Serverless, autoscaling batch and streaming pipelines | Streaming feature computation from Pub/Sub, large unstructured transforms, the same code for batch and streaming, MLTransform, RunInference | Beam programming model has a learning curve |
| Managed Service for Apache Spark (formerly Dataproc) | Managed Spark in serverless or cluster deployments | Existing PySpark or Spark jobs, Spark MLlib, lift-and-shift from Hadoop | Cluster tuning if not serverless |
| Ray on Agent Platform | Managed Ray clusters | Distributed Python for data processing and training in one framework | Cluster sizing, team Ray skills |
| In-memory Python (pandas, NumPy, scikit-learn) | Single machine | Small to medium data, prototyping, complex custom Python logic | Memory limits. A 200 GB table won't fit on a notebook VM |
Decision Rules
- 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.
- 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.
- Does the organization have a large existing Spark codebase? Use Managed Service for Apache Spark and reuse the code instead of rewriting it.
- Is the data small enough for memory and the logic heavily custom? Use in-memory Python in a notebook or a single training job.
- Do you need distributed Python across both data processing and training? Consider Ray on Agent Platform (Chapter 16).
Dataflow ML Details Worth Knowing
MLTransformwraps 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.
RunInferenceruns local models (PyTorch, scikit-learn, TensorFlow) or calls a remote Agent Platform endpoint from inside a pipeline, for batch or streaming scoring.
Worked Scenarios
| Scenario | Best tool | Why |
|---|---|---|
| 3 TB of clickstream in BigQuery, need 30-day rolling aggregates per user for a churn model | BigQuery SQL window functions | Data 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 history | Dataflow | Streaming plus batch with one Beam pipeline |
| 2,000 existing PySpark jobs from an on-premises Hadoop cluster | Managed Service for Apache Spark | Reuse Spark code with managed infrastructure |
| 50 MB CSV of lab results with complex domain-specific cleaning in Python | pandas in a notebook | Fits in memory, fastest to iterate |
| Python-savvy analysts want pandas syntax over a 1 TB BigQuery table | BigQuery DataFrames | pandas API with BigQuery execution |
| Generate embeddings for 100 million product descriptions as part of an ingestion pipeline | Dataflow MLTransform with an embedding model, or BigQuery AI.GENERATE_EMBEDDING if data is in BigQuery | Scalable 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.
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 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 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?