Databricks Fundamentals for Beginners:
What Actually Happens When You Execute a Spark DataFrame?
Coming from SQL Server, understanding how Spark handles data is an important first step in learning Databricks.
Consider this simple PySpark example.
.
1. Read a Parquet dataset
df = spark.read.parquet("/sales/sales.parquet")
This creates a Spark DataFrame representing the dataset. Spark does not normally load every row into memory when this statement runs.
2. Apply a transformation
filtered_df = df.filter("Region = 'Kerala'")
This defines a filter operation. Spark generally builds a logical execution plan rather than immediately processing and storing the filtered rows.
3. Trigger execution
filtered_df.show()
"show()" is an action.
It triggers execution of the required computation, reads the necessary data, applies the filter, and displays a limited number of rows.
Does "show()" store the result inside the DataFrame?
No. A DataFrame is not a container holding a complete copy of the dataset. It represents a distributed dataset and its computation plan.
During execution, Spark processes data across its executors. Intermediate data may occupy memory or disk, but that does not mean the entire result is permanently stored inside the DataFrame.
This behavior is part of Spark's lazy evaluation model: transformations are generally evaluated when an action requires a result.
When is data physically stored?
To persist the filtered result as a Delta dataset, we can write it to storage:
filtered_df.write \
.format("delta") \
.mode("overwrite") \
.save("/sales_delta")
This executes the computation and writes the result to the specified location. A Delta dataset includes Parquet data files and a transaction log.
Key concepts to remember
- DataFrame: Represents a distributed dataset and its computation plan.
- Transformation: Defines an operation, such as filtering or selecting columns.
- Action: Triggers computation, such as "show()" or "count()".
- Caching: Can retain computed data for reuse.
- Write operation: Persists the result to physical storage.
For SQL Server developers transitioning to Databricks, understanding the distinction between a query plan, execution, and physical storage provides a foundation for learning Spark optimization, caching, and distributed processing.
x
Comments
Post a Comment