home
diamond Go Premium
Data Engineering Path  ·  PySpark

RDD - Transformation Map

The map() transformation is one of the most fundamental operations in Apache Spark. It takes an existing RDD, applies a user-defined function to each individual element, and returns a new RDD containing the results.


Key Characteristics

  • 1-to-1 Mapping: The number of input elements in the parent RDD is exactly equal to the number of output elements in the child RDD.
  • Narrow Dependency: It does not require moving data across the network (shuffling). Each partition is processed locally on its executor node, making it extremely fast.
  • Type Safety / Flexibility: The output elements can have a different data type than the input elements (e.g., mapping strings to integers).

PySpark Code Examples

Setup Spark Session

from pyspark.sql import SparkSession

spark = SparkSession.builder \
    .appName("RDD Transformation Map") \
    .master("local[*]") \
    .getOrCreate()

sc = spark.sparkContext

Example A: Modifying Numeric Values

Let's double every integer inside a distributed RDD:

# 1. Create a raw RDD
numbers = sc.parallelize([1, 2, 3, 4, 5])

# 2. Apply map transformation (lazy)
doubled_numbers = numbers.map(lambda x: x * 2)

# 3. Trigger action to see results
print("Doubled Numbers:", doubled_numbers.collect())
# Output: Doubled Numbers: [2, 4, 6, 8, 10]

Example B: Extracting Fields from Key-Value Strings

Let's parse raw click logs and extract only the username:

# 1. Raw log lines RDD
raw_logs = sc.parallelize([
    "user_alice,login,2026-05-23",
    "user_bob,click,2026-05-23",
    "user_charlie,logout,2026-05-23"
])

# 2. Map to extract only the username (first field before comma)
usernames = raw_logs.map(lambda log: log.split(",")[0])

# 3. Fetch results
print("Usernames:", usernames.collect())
# Output: Usernames: ['user alice', 'user bob', 'user charlie']

Example C: Type Conversion (String to Length)

Let's map a list of strings to their respective lengths:

# 1. RDD of strings
words = sc.parallelize(["Spark", "RDD", "Map", "Transformation"])

# 2. Map strings to a tuple containing the word and its length
word_lengths = words.map(lambda word: (word, len(word)))

print("Word Lengths:", word_lengths.collect())
# Output: Word Lengths: [('Spark', 5), ('RDD', 3), ('Map', 3), ('Transformation', 14)]
Find this content helpful? ☕ Buy me a coffee

Entity Details

Create New Item

celebration
Enjoying the free content?

Create a free account to track your progress and save your place.

Create Free Account
help

Submit Technical Query

Have a question or run into an issue? Describe it below, upload an optional screenshot, and our engineering team will answer it!

image Attach image (optional)

Submit Feedback

build Free Developer Utility Free Tool
gavel

Privacy & Legal Disclaimer

1. Client-Side Browser Processing

All utility tools on DeepEngineerHub (including Image to PDF, Text Formatters, JSON Converters, and Encryptors) execute 100% locally within your client browser using WebAssembly and JavaScript. No uploaded images, text, or documents are transmitted, collected, or stored on remote servers.

2. Limitation of Liability ("As-Is" Provision)

Tools and services are provided free of charge for convenience and educational purposes "as-is" without warranties of any kind. DeepEngineerHub shall not be held liable for any data loss, formatting inconsistencies, or indirect damages resulting from tool usage.

3. Open Source & Third-Party Software

Certain utilities utilize open-source client libraries (such as jsPDF, Mermaid.js, Pyodide) licensed under MIT, Apache, or BSD open licenses. All intellectual property remains with their respective copyright holders.