MOBI BOOT CAMP CORP. logoLearning Buddy
  • SIGN IN
  • Foundations
  • The Hadoop Ecosystem: Batch at Scale
  • The Spark Ecosystem: In-Memory Processing
    • Apache Spark Intro
    • RDD
    • Spark DataFrame and SQL
    • Spark Streaming
    • MLlib in Spark
    • Slides
  • Data Pipelines and Transport
  • Search & Information Retrieval
  • The Modern Data Stack
  • Glossary

RDD: Spark's Foundational Abstraction

At the core of early versions of Spark is the RDD (Resilient Distributed Dataset). While modern Spark applications often use higher-level APIs like DataFrames and Datasets, understanding RDDs is key to grasping how Spark works under the hood.

An RDD is an immutable, partitioned collection of records that can be operated on in parallel. Let's break that down:

  • Resilient: RDDs are fault-tolerant. They track their data's lineage (the series of transformations used to create them). If a partition of data is lost due to a node failure, Spark can automatically recompute it from the original source data.
  • Distributed: The data within an RDD is partitioned and distributed across the worker nodes of the cluster, allowing for parallel processing.
  • Dataset: It is a collection of data, similar to a list or a set, that you can load from an external source or create from an existing collection in your driver program.

Working with RDDs

You can create RDDs in two main ways: by parallelizing an existing collection in your driver program or by loading a dataset from an external storage system like HDFS, cloud storage, or any data source offering a Hadoop InputFormat.

# Create an RDD from a local Python list
data = [1, 2, 3, 4, 5]
distData = sc.parallelize(data)

# Create an RDD from a text file in HDFS
# sc is the SparkContext, the entry point for RDD operations
distFile = sc.textFile("hdfs:///path/to/your/file.txt")

RDD Operations: Transformations and Actions

RDDs support two types of operations:

  1. Transformations: These operations create a new RDD from an existing one. Examples include map(), filter(), and flatMap().
  2. Actions: These operations compute a result based on an RDD and either return it to the driver program or save it to an external storage system. Examples include count(), collect(), and reduce().

A crucial concept in Spark is Lazy Evaluation. Transformations are lazy; they do not execute immediately. Spark builds up a lineage graph of the transformations. The computation is only triggered when an action is called.

Common Transformations

  • map(func): Returns a new RDD by applying a function to each element of the original RDD.
  • flatMap(func): Similar to map, but each input item can be mapped to 0 or more output items (so the function should return a sequence rather than a single item).
  • filter(func): Returns a new RDD containing only the elements that satisfy a given function.

Common Actions

  • collect(): Returns all the elements of the RDD as a list to the driver program. (Use with caution on large datasets!)
  • count(): Returns the number of elements in the RDD.
  • reduce(func): Aggregates the elements of the RDD using a function that is commutative and associative.
  • take(n): Returns the first n elements of the RDD.

The RDD Reduce Action

Example: Word Count in PySpark using RDDs

Here is the classic word count example implemented using the RDD API.

# 1. Create an RDD from a text file. Each element is a line of text.
lines = sc.textFile("hdfs:///path/to/input.txt")

# 2. Use flatMap to split each line into a list of words.
# The result is an RDD where each element is a single word.
words = lines.flatMap(lambda line: line.split(" "))

# 3. Use map to create a key-value pair for each word.
# The result is an RDD of (word, 1) tuples.
pairs = words.map(lambda word: (word, 1))

# 4. Use reduceByKey to sum the counts for each unique word.
# This is a transformation that shuffles the data and aggregates by key.
counts = pairs.reduceByKey(lambda a, b: a + b)

# 5. Use an action to save the final counts back to a text file.
counts.saveAsTextFile("hdfs:///path/to/output")

RDD Persistence (Caching)

By default, RDDs are recomputed each time you run an action on them. If you plan to reuse an RDD multiple times, you can ask Spark to persist (or cache) it in memory across the cluster.

RDD Persistence Options

# Persist the 'counts' RDD in memory
counts.cache()

# Now, any action on 'counts' will be much faster
total_word_count = counts.count()
top_10_words = counts.take(10)

While RDDs are a powerful, low-level API, for most modern applications involving structured or semi-structured data, the DataFrame and Dataset APIs are the recommended best practice.

Privacy Policy | Terms & Conditions