0Pricing
Scala for Backend Engineering & Functional Programming · Lesson

RDDs and DataFrames

Spark data abstractions.

RDDs and DataFrames is a free Scala for Backend Engineering & Functional Programming lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Scala for Backend Engineering & Functional Programming learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is Apache Spark?

Apache Spark is a distributed engine for large-scale data processing. It splits data across a cluster and runs computations in parallel. Scala is Spark's native language, giving a concise, type-aware API.

The RDD

The Resilient Distributed Dataset (RDD) is Spark's low-level abstraction: an immutable, partitioned collection that can be processed in parallel and rebuilt from lineage if a node fails.

SparkContext and SparkSession

You enter Spark through a SparkSession. Its sparkContext creates RDDs; the session itself creates DataFrames and runs SQL.

import org.apache.spark.sql.SparkSession

val spark = SparkSession.builder()
  .appName("Demo")
  .master("local[*]")
  .getOrCreate()

Creating an RDD

Build an RDD by parallelizing a local collection or by reading a file. Each partition is processed by a separate task.

val sc = spark.sparkContext
val numbers = sc.parallelize(Seq(1, 2, 3, 4, 5))
val lines   = sc.textFile("data.txt")

The DataFrame

A DataFrame is a distributed table with named, typed columns — like an RDD of rows plus a schema. The Catalyst optimizer can plan and optimize DataFrame queries.

Creating a DataFrame

Create DataFrames from collections (with toDF) or by reading structured files such as CSV, JSON, or Parquet.

import spark.implicits._

val df = Seq(("Alice", 30), ("Bob", 25)).toDF("name", "age")
val csv = spark.read.option("header", "true").csv("people.csv")

Inspecting a DataFrame

Use show to print rows, printSchema to view column types, and count for the number of rows.

df.printSchema()
df.show()
println(df.count())

Datasets and Type Safety

A Dataset is a typed DataFrame: Dataset[T] where T is a case class. It combines DataFrame optimization with compile-time type checks.

import spark.implicits._

case class Person(name: String, age: Int)
val ds = Seq(Person("Alice", 30)).toDS()

RDD vs DataFrame vs Dataset

Choose based on needs:

  • RDD — full control, no schema, no optimizer
  • DataFrame — schema + Catalyst optimization, untyped rows
  • Dataset — schema + optimization + type safety

Converting Between Them

Convert a DataFrame to an RDD with .rdd, or an RDD of case classes to a DataFrame with .toDF. Datasets convert to DataFrames with .toDF too.

val rdd = df.rdd          // DataFrame -> RDD[Row]
val back = ds.toDF()      // Dataset -> DataFrame

Plain Scala Collection

Conceptually a DataFrame behaves like a Scala collection but distributed. Here is the local, self-contained analog for intuition.

object Main {
  case class Person(name: String, age: Int)
  def main(args: Array[String]): Unit = {
    val people = Seq(Person("Alice", 30), Person("Bob", 25))
    people.foreach(p => println(s"${p.name}: ${p.age}"))
  }
}

Quick Check

Which abstraction provides a schema, Catalyst optimization, and compile-time type safety?

Recap

You met Spark's data abstractions:

  • SparkSession as the entry point
  • RDD — low-level distributed collection
  • DataFrame — distributed table with schema
  • Dataset — typed DataFrame

Next: transformations and actions.

Frequently asked questions

Is the “RDDs and DataFrames” lesson free?

Yes — the full text of “RDDs and DataFrames” is free to read here on the web, and the Scala for Backend Engineering & Functional Programming course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Scala for Backend Engineering & Functional Programming course, upgrade to CoddyKit PRO.

What will I learn in “RDDs and DataFrames”?

Spark data abstractions. You practise Scala for Backend Engineering & Functional Programming with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Scala for Backend Engineering & Functional Programming?

No prior experience is required. Scala for Backend Engineering & Functional Programming on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “RDDs and DataFrames” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Scala for Backend Engineering & Functional Programming lesson?

Yes. Every Scala for Backend Engineering & Functional Programming lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. RDDs and DataFrames
  2. Transformations and Actions
  3. Spark SQL
  4. Aggregations
← Back to Scala for Backend Engineering & Functional Programming