We create RDDs to work with data in Spark. RDDs let us process data in parallel easily.
Creating RDDs from collections and files in Apache Spark
rdd = spark.sparkContext.parallelize(collection) rdd = spark.sparkContext.textFile(file_path)
parallelize() creates an RDD from a local collection like a list.
textFile() reads a file and creates an RDD where each item is a line.
numbers = [1, 2, 3, 4] rdd_numbers = spark.sparkContext.parallelize(numbers)
lines_rdd = spark.sparkContext.textFile("data/sample.txt")This program shows how to create RDDs from a list and from a text file. It prints the contents of both RDDs.
from pyspark.sql import SparkSession spark = SparkSession.builder.appName("CreateRDDs").getOrCreate() # Create RDD from a collection fruits = ["apple", "banana", "cherry"] rdd_fruits = spark.sparkContext.parallelize(fruits) print("RDD from collection:") print(rdd_fruits.collect()) # Create RDD from a text file # For demonstration, create a small text file first file_path = "/tmp/sample.txt" with open(file_path, "w") as f: f.write("hello\nworld\nspark") rdd_file = spark.sparkContext.textFile(file_path) print("RDD from file lines:") print(rdd_file.collect()) spark.stop()
Remember to call collect() to get all RDD items back to the driver for printing or inspection.
Creating RDDs from files is useful for big data, but for small data, collections are easier to use.
Always stop the Spark session when done to free resources.
Use parallelize() to create RDDs from local collections like lists.
Use textFile() to create RDDs from text files, where each line is an item.
RDDs let you process data in parallel with Spark easily.
Practice
parallelize() method do in Apache Spark?Solution
Step 1: Understand the purpose of
This method takes a local collection such as a list and creates an RDD from it, enabling parallel processing.parallelize()Step 2: Differentiate from other methods
textFile()reads from files,saveAsTextFile()saves data, andfilter()filters data. These are different fromparallelize().Final Answer:
Creates an RDD from a local collection like a list -> Option AQuick Check:
parallelize()= create RDD from list [OK]
- Confusing parallelize() with textFile()
- Thinking parallelize() reads files
- Mixing up saving and creating RDDs
[1, 2, 3] using SparkContext sc?Solution
Step 1: Recall the method to create RDD from a list
The correct method isparallelize()called on SparkContext with the list as argument.Step 2: Check syntax correctness
sc.parallelize([1, 2, 3])is valid syntax. Other options are invalid or do not exist.Final Answer:
sc.parallelize([1, 2, 3]) -> Option BQuick Check:
Use parallelize() with list [OK]
- Using textFile() with a list argument
- Trying to use non-existent methods like createRDD()
- Passing list to load() which is for files
rdd = sc.parallelize(['apple', 'banana', 'cherry']) result = rdd.collect()
What will be the value of
result?Solution
Step 1: Understand what
It creates an RDD from the list ['apple', 'banana', 'cherry'].parallelize()doesStep 2: Understand what
collect()doescollect()returns all elements of the RDD as a list to the driver program.Final Answer:
['apple', 'banana', 'cherry'] -> Option CQuick Check:
collect() returns list of RDD elements [OK]
- Thinking collect() returns an RDD
- Confusing collect() with saving to file
- Expecting line numbers instead of data
rdd = sc.textFile([1, 2, 3]) print(rdd.collect())
Solution
Step 1: Check the argument type for
textFile()textFile()expects a string path to a file, not a list of strings.Step 2: Understand the error cause
Passing a list causes a type error because Spark cannot interpret it as a file path.Final Answer:
textFile() expects a string path, not a list -> Option AQuick Check:
textFile() needs string path [OK]
- Passing list instead of string to textFile()
- Thinking collect() is invalid on textFile RDD
- Assuming textFile needs multiple arguments
data.txt and then filter lines containing the word "error". Which code snippet correctly creates the RDD and applies the filter?Solution
Step 1: Choose correct method to create RDD from file
sc.textFile('data.txt')reads the file line by line into an RDD. Passing a string path is required.Step 2: Apply filter to select lines containing 'error'
Thefilter()method with a lambda checks each line for the substring 'error'. This is the correct way to filter.Final Answer:
rdd = sc.textFile('data.txt') filtered = rdd.filter(lambda line: 'error' in line) -> Option DQuick Check:
Use textFile() with string path + filter() [OK]
- Using parallelize() to read files
- Passing list instead of string to textFile()
- Using non-existent sc.load() method
Ready to prove it?
Pass the test and this pattern moves unknown
