RDD API Reference
Low-level Resilient Distributed Dataset (RDD) transformations, actions, partition controls, and memory persistence.
Applies a transformation function to each individual element of the RDD and returns a new RDD.
Used In: Element Parsing, Data Formatting
rdd.map(func)rdd = sc.parallelize([1, 2, 3, 4])
rdd2 = rdd.map(lambda x: (x, x * 10))
rdd2.collect()[(1, 10), (2, 20), (3, 30), (4, 40)]Applies a function returning an iterable to each element, then flattens the result into a single stream.
Used In: Tokenization, Text Processing, Flattening Nested Lists
rdd.flatMap(func)lines = sc.parallelize(['hello world', 'apache spark'])
words = lines.flatMap(lambda line: line.split(' '))
words.collect()['hello', 'world', 'apache', 'spark']Applies a transformation function to each partition block of the RDD (passes an iterator).
Used In: External DB Writes, Model Inference, Batching
rdd.mapPartitions(func)def process_partition(iterator):
# Initialize expensive DB connection once per partition
db = connect_db()
for item in iterator:
yield db.transform(item)
rdd.mapPartitions(process_partition)Transformed RDD iterator streamMerges values for each key using an associative and commutative reduce function.
Used In: Word Count, Key-wise Aggregations
rdd.reduceByKey(func)pairs = sc.parallelize([('a', 1), ('b', 1), ('a', 2)])
counts = pairs.reduceByKey(lambda x, y: x + y)
counts.collect()[('a', 3), ('b', 1)]vs groupByKey: reduceByKey combines data locally on each mapper node before sending across the network. groupByKey sends ALL raw pairs across the network.
Groups values for each key in the RDD into a single sequence (Iterable).
Used In: Grouping without Aggregation
rdd.groupByKey()pairs = sc.parallelize([('dept1', 100), ('dept1', 200), ('dept2', 300)])
grouped = pairs.groupByKey().mapValues(list)
grouped.collect()[('dept1', [100, 200]), ('dept2', [300])]Aggregates values of each key using an initial zero value, a sequence op (within partition), and a combine op (between partitions).
Used In: Custom Multi-metric Aggregations, Average Computation
rdd.aggregateByKey(zeroValue, seqOp, combOp)# Compute average per key: zeroValue=(sum, count)
zero = (0, 0)
seqOp = lambda acc, val: (acc[0] + val, acc[1] + 1)
combOp = lambda acc1, acc2: (acc1[0] + acc2[0], acc1[1] + acc2[1])
avg_rdd = rdd.aggregateByKey(zero, seqOp, combOp).mapValues(lambda x: x[0]/x[1])[('dept1', 150.0), ('dept2', 300.0)]Changes the number of partitions in the RDD.
Used In: Partition Tuning, Output File Consolidation
rdd.coalesce(numPartitions, shuffle=False) | rdd.repartition(numPartitions)# Reduce 100 partitions to 10 without shuffle
rdd_small = rdd.coalesce(10)
# Increase 10 partitions to 50 with full shuffle
rdd_large = rdd.repartition(50)Resized RDD with target partition countCore RDD action operations that trigger execution and retrieve data.
Used In: Driver Inspection, Quick Testing
rdd.collect() | rdd.take(n) | rdd.count()print('Count:', rdd.count())
print('First 3:', rdd.take(3))Count: 500000
First 3: [1, 2, 3]Persists RDD in memory or disk for fast reuse across multiple downstream actions.
Used In: Iterative Machine Learning, Multi-action Pipelines
rdd.cache() | rdd.persist(StorageLevel.MEMORY_AND_DISK)from pyspark import StorageLevel
rdd.persist(StorageLevel.MEMORY_AND_DISK_SER)
rdd.count() # Triggers caching
rdd.take(10) # Uses cached memoryCached RDD