If Spark is implemented in Scala, how can you develop a Spark application in Python?

June 13, 2025

While looking up Spark’s expr(), I came across two interesting questions. One person knew a little Python, had just heard about ā€œSpark Scalaā€ at work, and wanted to know how the two fit together. Another had used PySpark for data processing but kept getting asked about Scala in interviews. To answer either question, we first have to ask which program we mean when we say ā€œwritten in Scala.ā€12

Much of Spark’s core is written in Scala. Users can write their own programs through its Python API, PySpark. Spark calls such a program an application. An operation such as collect() asks Spark to retrieve results. Operations that trigger computation are called actions. An action can trigger one or more jobs, each a parallel computation.3

Suppose you want to summarize a year of lab orders by department. You can describe in Python which column to group by and which records to count. Spark SQL takes those operations and decides how to read and process the data. It also accepts queries expressed in Scala or SQL. The official documentation says these interfaces use ā€œthe same execution engineā€.4

When I write expr() in PySpark, where does the formula run?

Suppose the data has already been aggregated into a DataFrame, a table with named columns and one row per department. ordered is the number of lab orders placed, and reported is the number with results. The emergency department placed 80 orders and has results for 60. To add a column showing the proportion with results, you could write this in an interactive PySpark session:

from pyspark.sql import functions as F

counts = spark.createDataFrame(
    [("ę€„čÆŠē§‘", 80, 60)], ["department", "ordered", "reported"]
)
rate = F.expr("reported / ordered")
report = counts.select("department", rate.alias("reported_rate"))
report.show()

At rate = ..., rate is a Column describing how to derive a new column from two others. Inside the quoted string, reported and ordered name columns in the table. select places that expression in the query plan describing the computation Spark should perform. show() asks it to fetch the results for display. The emergency department’s proportion is 0.75. Spark SQL executes the division. If you select the same columns from the same table and write the same division in Scala, Spark SQL recognizes that operation too.54

When does Python actually do the calculation?

Take the same division and put it in an ordinary Python function, then register it as a Python UDF, a user-defined function that can be called from a query. Execution now involves an extra round of data transfer. The process responsible for executing computations, the executor, sends the two column values to a Python worker. That process runs the Python function and sends the calculated proportion back to the executor. Built-in division can run directly in Spark SQL. If a query is slow, check whether the step taking the time calls a Python UDF.6

Back to the two original questions. If your work involves reading, joining, grouping, and aggregating tables, familiarity with Python is enough to start with PySpark. If you are taking over a Spark application your team developed in Scala, you will have to read and modify that code. Some projects also use Scala’s Dataset[T] to give each record a particular type so the compiler can check field access. PySpark offers the column-oriented DataFrame API. If an interviewer asks about Scala, it is worth asking which Scala features their existing Spark applications use.4

Footnotes

  1. WadieXkiller. (2024). Python and Spark Scala [Forum post]. Reddit, r/dataengineering. Original question about Python and Spark Scala. ↩

  2. idreamoffood101. (2021). Pyspark vs Scala spark [Forum post]. Reddit, r/dataengineering. Interview experience and discussion of DataFrames and UDFs. ↩

  3. Apache Software Foundation. (n.d.). Cluster mode overview. Apache Spark 3.5.6. Application and Job in the glossary. ↩

  4. Apache Software Foundation. (n.d.). Spark SQL, DataFrames and Datasets guide. Apache Spark 3.5.6. Introduction, second paragraph, on the shared execution engine Ā· Datasets and DataFrames, on types and language support. ↩ ↩2 ↩3

  5. Apache Software Foundation. (n.d.). pyspark.sql.functions.expr. PySpark 3.5.6. Signature, return value, and examples. ↩

  6. Apache Software Foundation. (n.d.). BatchEvalPythonExec.scala (v3.5.6). Apache Spark, GitHub. Inputs and results for ordinary Python UDFs, lines 43–64. ↩

Ā© 2025 borui. All rights reserved. This content may be freely reproduced, displayed, modified, or distributed with proper attribution to borui and a link to the article: borui (2025-06-13). If Spark is implemented in Scala, how can you develop a Spark application in Python?. https://caiborui.com/blog/2025-06-13-zh-pyspark-scala-expr-entrans

@misc{borui2025-b278bec9,
  author = {borui},
  title = {{If Spark is implemented in Scala, how can you develop a Spark application in Python?}},
  year = {2025},
  publisher = {borui's blog},
  url = {https://caiborui.com/blog/2025-06-13-zh-pyspark-scala-expr-entrans}
}