Running Spark: Databricks, EMR, Dataproc & Kubernetes
Apache Spark · MLlib & Ecosystem
Theory
The same PySpark code can run on many platforms; what changes is who manages the cluster. Every deployment has a driver (runs your main program and plans the work) and executors (run tasks), plus a cluster manager that provides them: Spark standalone, YARN (used by Hadoop-style clusters such as Amazon EMR and Google Cloud Dataproc) or Kubernetes (driver and executors run as pods). You submit with spark-submit or the platform's job service.
Managed platforms hide most of that. Databricks offers managed clusters, serverless compute, notebooks, jobs and governance on AWS, Azure and Google Cloud. Amazon EMR runs open-source Spark on EC2, on EKS or as EMR Serverless. Google Cloud Dataproc runs Spark clusters and Dataproc Serverless batches. On Kubernetes you run Spark yourself with spark-submit or an operator. Good job code reads paths and settings from arguments or config and does not hard-code master, so it moves between platforms.
Why we use it
Interviews and real projects expect you to know where Spark runs and what changes between platforms: cluster managers, deploy modes, how code and dependencies are shipped. Writing portable jobs avoids rewrites when a company changes cloud or platform.
When to use it
Choose Databricks when you want a managed lakehouse platform with notebooks, jobs and governance; EMR or Dataproc when you are on AWS or Google Cloud and want managed open-source Spark; Kubernetes when your company already runs everything on Kubernetes and has the skills to operate it.
How it works
- Write the job as functions plus a main()
- Package code and dependencies (wheel, zip or container image)
- Managed platform or self-managed? Yes: Submit via Databricks Jobs, EMR steps or Dataproc batches. No: spark-submit to YARN or Kubernetes.
- Cluster manager starts the driver and executors
- Job runs; logs and the Spark UI are available on the platform
- Results written to cloud storage or tables
Syntax
spark-submit --master yarn --deploy-mode cluster \
--conf spark.executor.memory=4g --py-files deps.zip job.py --date 2026-01-05
spark-submit --master k8s://https://<api-server>:443 --deploy-mode cluster \
--conf spark.kubernetes.container.image=<image> local:///app/job.py
# never hard-code .master() in job code; let the platform set itExample
# Submit the SAME file everywhere; only the launcher and its flags change:
# Local : spark-submit --master local[*] daily_sales.py 2026-01-05
# YARN/EMR : spark-submit --master yarn --deploy-mode cluster daily_sales.py 2026-01-05
# Dataproc : gcloud dataproc jobs submit pyspark daily_sales.py --cluster=c1 --region=<region> -- 2026-01-05
# K8s : spark-submit --master k8s://https://<api-server>:443 --deploy-mode cluster \
# --conf spark.kubernetes.container.image=<image> local:///app/daily_sales.py 2026-01-05
# Databricks: add the file as a Python script task in a Job (the platform creates `spark`)
import sys
from pyspark.sql import SparkSession, functions as F
def build_report(spark, run_date):
"""Pure transformation logic: identical on Databricks, EMR, Dataproc or Kubernetes."""
orders = spark.createDataFrame([
(1, 101, "North", "Laptop", 1, 55000.0, "2026-01-05"),
(2, 102, "West", "Phone", 2, 30000.0, "2026-01-05"),
(3, 101, "North", "Mouse", 3, 1500.0, "2026-01-06"),
], "order_id INT, customer_id INT, region STRING, product STRING, qty INT, amount DOUBLE, order_date STRING")
return (orders.filter(F.col("order_date") == run_date)
.groupBy("region").agg(F.sum("amount").alias("sales"))
.orderBy("region"))
def main(argv):
run_date = argv[1] if len(argv) > 1 else "2026-01-05"
# No .master() here: spark-submit / the platform decides where this runs
spark = SparkSession.builder.appName(f"daily-sales-{run_date}").getOrCreate()
sc = spark.sparkContext
print("master:", sc.master, "| app:", sc.appName, "| default parallelism:", sc.defaultParallelism)
report = build_report(spark, run_date)
report.show()
# report.write.mode("overwrite").parquet(f"s3://bucket/reports/date={run_date}")
if __name__ == "__main__":
main(sys.argv)
# Output:
# master: local[*] | app: daily-sales-2026-01-05 | default parallelism: 2
# +------+-------+
# |region| sales|
# +------+-------+
# | North|55000.0|
# | West|30000.0|
# +------+-------+
Key points
- Driver plans and coordinates; executors run tasks; a cluster manager supplies both
- Cluster managers: standalone, YARN (EMR, Dataproc), Kubernetes
- deploy-mode cluster runs the driver on the cluster; client keeps it on your machine
- Databricks, EMR and Dataproc also offer serverless options with no cluster to size
- Do not hard-code master or paths: pass them as arguments or config