Skip to Content
Course content

81: Spark DataFrames and Datasets

Click on the "Edit" button in the top corner of the screen to edit your slide content.

A few years ago, I was working with a colleague who was trying to migrate a legacy ETL pipeline to Spark. He had spent an entire weekend writing these incredibly complex RDD transformations—nested map calls, reduceByKey operations that looked like alphabet soup, and a lot of manual casting. When we finally ran it on a massive dataset of e-commerce clickstreams, it crawled. The CPU was pegged, but the execution plan was a mess because Spark had no idea what was actually inside those RDDs; it just saw a stream of opaque Java objects.

I showed him how to rewrite the core logic using DataFrames. We replaced about 150 lines of "manual" Scala with 20 lines of declarative transformations. Not only was the code readable, but the execution time dropped by nearly 60%. That's the "magic" of Spark's Catalyst optimizer: when you use DataFrames, Spark understands the structure of your data and can rewrite your query to be more efficient before it ever touches a cluster.

The Untyped Power of DataFrames

Think of a DataFrame as a distributed table. It's conceptually similar to a table in a relational database or a Pandas DataFrame in Python. In Scala, a DataFrame is actually just an alias for Dataset[Row]. A Row is a generic object that holds columns, but it doesn't have a compile-time type. You access data by column name or index.

The real win here is the Catalyst Optimizer. When you call .select("userId").filter(col("amount") > 100), Spark doesn't just execute those steps in order. It builds a logical plan, optimizes it (e.g., pushing the filter down so it reads less data from disk), and then generates optimized bytecode. If you use RDDs, you're essentially telling Spark how to do something; with DataFrames, you're telling it what you want, and letting Spark figure out the most efficient way to get there.

import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.functions._

val spark = SparkSession.builder().appName("Example").master("local").getOrCreate()
import spark.implicits._

// Creating a DataFrame from a list of tuples
val salesData = Seq(
  ("Laptop", 1200, "Electronics"),
  ("Mouse", 25, "Electronics"),
  ("Book", 15, "Media")
).toDF("product", "price", "category")

// Declarative transformations
val expensiveElectronics = salesData
  .filter(col("category") === "Electronics")
  .filter(col("price") > 100)

expensiveElectronics.show()

Regaining Type Safety with Datasets

While DataFrames are fast, the lack of type safety can be nerve-wracking. If you typo a column name as "prcie" instead of "price", you won't find out until the job crashes in production. This is where Datasets come in. A Dataset is a strongly typed collection of JVM objects.

By defining a case class, you can tell Spark exactly what your data looks like. This gives you the best of both worlds: the optimization of the DataFrame API and the compile-time safety of Scala. However, there is a trade-off. Datasets require "encoders" to convert JVM objects into Spark's internal binary format (Tungsten). This serialization overhead means that for very simple operations, a raw DataFrame can actually be slightly faster than a Dataset.

I usually recommend starting with DataFrames for exploratory analysis and switching to Datasets for complex business logic where a runtime AnalysisException would be a disaster.

case class Product(product: String, price: Int, category: String)

// Converting the DataFrame to a typed Dataset
val productsDS = salesData.as[Product]

// Now we have compile-time safety. 
// If I try to access .price, the compiler knows it's an Int.
val highValueProducts = productsDS.filter(p => p.price > 100)

highValueProducts.collect().foreach(p => println(s"Expensive item: ${p.product}"))



📋 Practical Task

Processing Telemetry Logs with Typed Datasets

You are tasked with analyzing a stream of system telemetry logs. You need to identify "Critical" errors and calculate the average response time for those errors to determine if a specific service is degrading.

Requirements:

  • Define a case class LogEntry with fields: timestamp (String), level (String), service (String), and responseTimeMs (Int).
  • Create a Dataset using a provided list of log data.
  • Filter the Dataset to keep only entries where the level is exactly "CRITICAL".
  • Use the DataFrame API (by casting or using the Dataset's underlying capabilities) to calculate the average responseTimeMs for these critical logs.
  • Print the resulting average to the console.

Starter Data:

val logs = Seq(
  ("2023-10-01 10:00", "INFO", "AuthService", 50),
  ("2023-10-01 10:01", "CRITICAL", "PaymentService", 5000),
  ("2023-10-01 10:02", "ERROR", "AuthService", 200),
  ("2023-10-01 10:03", "CRITICAL", "PaymentService", 7000),
  ("2023-10-01 10:04", "INFO", "PaymentService", 100)
)
Rating
0 0

There are no comments for now.

to be the first to leave a comment.