45.1 Spark Architecture
Driver Program
│
├── SparkContext → Cluster Manager (YARN/K8s/Standalone)
│ │
│ ┌──────┼──────┐
│ ▼ ▼ ▼
│ Worker Worker Worker
│ (Executor)(Executor)(Executor)
│ │ │ │
│ Tasks Tasks Tasks
│
└── DAG Scheduler → Task Scheduler → Executors
45.2 Spark ML Pipeline Example
from pyspark.sql import SparkSession
from pyspark.ml import Pipeline
from pyspark.ml.feature import StringIndexer, VectorAssembler, StandardScaler
from pyspark.ml.classification import GBTClassifier
spark = SparkSession.builder \
.appName("RiskPrediction") \
.config("spark.sql.shuffle.partitions", "200") \
.getOrCreate()
df = spark.read.parquet("s3://data-lake/records/")
features_df = df \
.withColumn("days_to_filing", datediff(col("filing_date"), col("event_date"))) \
.withColumn("historical_rate", calculate_historical_rate(col("entity_id")))
indexers = [StringIndexer(inputCol=c, outputCol=f"{c}_idx") for c in categorical_cols]
assembler = VectorAssembler(inputCols=feature_cols, outputCol="features")
scaler = StandardScaler(inputCol="features", outputCol="scaled_features")
gbt = GBTClassifier(featuresCol="scaled_features", labelCol="target",
maxDepth=6, maxIter=100)
pipeline = Pipeline(stages=indexers + [assembler, scaler, gbt])
model = pipeline.fit(train_df)
predictions = model.transform(test_df) # processes millions of records in minutes
45.3 Spark Optimization Tips
| Optimization | What It Does | When to Apply |
|---|---|---|
| Partitioning | Distribute data evenly | Always — repartition(N) |
| Caching | Keep frequently used data in memory | Iterative ML, joins |
| Broadcast joins | Send small table to all nodes | Small lookup tables |
| Columnar formats | Parquet/Delta for efficient reads | Always for data lake |
| Predicate pushdown | Filter before loading | Partitioned data, date ranges |
| Adaptive Query Execution | Auto-optimize at runtime | Spark 3.x, on by default |
45.4 Hadoop Ecosystem vs. Cloud Equivalents
| Hadoop | Purpose | AWS Equivalent | Azure Equivalent |
|---|---|---|---|
| HDFS | Distributed storage | S3 | ADLS Gen2 |
| YARN | Resource management | EMR/EKS | HDInsight/AKS |
| MapReduce | Batch processing | EMR (Spark) | HDInsight |
| Hive | SQL-on-Hadoop | Athena, Redshift Spectrum | Synapse Analytics |
| HBase | NoSQL, real-time | DynamoDB | Cosmos DB |
| ZooKeeper | Coordination | MSK (managed Kafka) | Event Hubs |
EMR cluster sizing pattern:
Master: m5.4xlarge (16 vCPU, 64 GB)
Core: r5.2xlarge × 5-10 (memory-optimized for joins)
Task: m5.xlarge × 10-20 (spot instances for cost)
Runtime: Spark 3.5+ on EMR 7.x
Storage: S3 (not HDFS) — decouple compute from storage
Use cases: nightly batch processing, multi-year historical analysis,
feature engineering for ML, data quality/reconciliation jobs
45.5 Q&A
Q: When would you still reach for classic Hadoop/HDFS today? Almost never for greenfield builds — S3/ADLS + Spark on EMR/Databricks/HDInsight gives the same or better throughput with decoupled compute/storage (scale each independently, pay only when jobs run) and none of the operational burden of managing HDFS replication and NameNode HA. Legacy on-prem Hadoop clusters persist mainly due to sunk infrastructure investment or data-residency constraints that rule out cloud storage.
Q: What is Adaptive Query Execution (AQE) and why does it matter? Spark 3.x runtime optimizer that re-plans a query mid-execution based on actual data statistics (not just static estimates) — it can dynamically switch a sort-merge join to a broadcast join, coalesce shuffle partitions, and handle skewed joins automatically. It matters because static query plans on real-world skewed data (e.g., one payer or region dominating claim volume) routinely underperform without it.