Introduction to RDD

Spark tutorial · PySpark.in

Introduction to RDD in Apache Spark

Before Spark, Hadoop MapReduce dominated big data processing. However, MapReduce had some major limitations:

Spark introduced RDDs to overcome these issues:

 What is an RDD?

RDD = Resilient Distributed Dataset

 Analogy: RDD as a Classroom of Students

Imagine you are a teacher with 1000 students:

RDD works the same way:

Tip: Use RDDs for low-level control or unstructured data. For most modern workloads, prefer DataFrames/Datasets.

 When to Use RDDs

 Example: Word Count Using RDD:

```
from pyspark.sql import SparkSession
# Initialize SparkSession
spark = SparkSession.builder.appName("WordCountExample").getOrCreate()
# Hardcoded list of words (simulating text input)
data = [
    "hello world this is pyspark",
    "pyspark is great for big data processing",
    "hello spark hello python",
    "data engineering with pyspark and spark"
]
# Create RDD from hardcoded data
text_rdd = spark.sparkContext.parallelize(data)
# Split lines into words
words = text_rdd.flatMap(lambda line: line.split(" "))
# Filter for words containing 'spark'
spark_words = words.filter(lambda w: "spark" in w.lower())
# Map each word to (word, 1) and reduce counts
counts = spark_words.map(lambda x: (x, 1)).reduceByKey(lambda a, b: a + b)
# Action: get top results
print(counts.collect())
# Stop SparkSession
spark.stop()

```

  1. Load Big Data → Spark reads large dataset (text_rdd = spark.sparkContext.textFile("data.txt"))

  2. Split into Partitions → Data is divided across multiple nodes

  3. Transformations (Lazy) → Operations like map(), filter() applied per partition

  4. Actions Trigger Execution  collect(), count(), take() run transformations in parallel

  5. Fault Tolerance → Lost partitions are recomputed using lineage

Introduction to RDD


Limitations of RDDs

Modern Spark tip: Prefer DataFrames/Datasets, use RDDs for low-level or unstructured data.

Note:

More Spark tutorials

All tutorials · Try the free PySpark compiler · Practice challenges