Introduction to Spark flatMap
In Apache spark, Spark flatMap is one of the transformation operations. Tr operation of Map function is applied to all the elements of RDD which means Resilient Distributed Data sets. These are immutable and collection of records which are partitioned and these can only be created by operations (operations that are applied throughout all the elements of the dataset) like filter and map. The operation developer in Map has the facility to create his own custom logic business. Map() is mostly similar to flatMap() and can return only 0 or 1 and or more elements from the function map().
Syntax for flatMap in Spark:
RDD.flatMap(<transformation function>)
The transformation function from the above syntax code, for each element source of the RDD, can return multiple elements of RDD.
How Spark flatMap work?
A flatMap is an operation of transformation. A new RDD is returned with its application on each element of RDD as a result. This gives many results out of it which means that we can get one, two, zero and other many elements from the flatMap operation applications. Map operation is one step behind flatMap operation technique and is mostly similar.
Example:
Spark
Scala
Java helps
Hello world
How are you doing
Debugging is fun
Code:
flatMap(a => a.split(' '))
Output:
flatMap operation of transformation is done from one to many.
Let us consider an example which calls lines.flatMap(a => a.split(‘ ‘)), is a flatMap which will create new files off RDD with records of 6 number as shown in the below picture as it splits the records into separate words with spaces in between them.
Splitting an RDD key value can also be done using flatMap operation transformation.
Like for the above example, if we consider mapping them with the key values, they are given with the same number key for identification of each key value pair.
1. Spark
2. Scala
3. Java helps
4. Hello world
5. How are you doing
6. Debugging is fun
Code:
flatMap(a => a.split(' '))
Output:
One to one can also be used in flatMap also, one to zero mapping. lines.flatMap(a => None) is used in returning an empty RDD as flatMap does not help in creating a record for none values in a resulting RDD.flatMap(a => a.split(‘ ‘))
Spark
Scala
Java helps
Hello world
How are you doing
Debugging is fun
Code:
flatMap(a => None)
Output:
Examples of Spark flatMap
Given below are the examples mentioned:
Example #1
String to words – An example for Spark flatMap in RDD using Java.
Code:
import java.util.Arrays;
import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
public class flatMapEx{
public static void main(String[] args) {
// Spark configuration is done according to below code
SparkConf sparkConf = new SparkConf().setAppName("Text Reading")
.setMaster("local[2]").set("spark.executor.memory","2g");
// A context of Spark being started
JavaSparkContext sc = new JavaSparkContext(sparkConf);
// A path for input text file is being provided
String path = "data/stringToWords/input_rdd/sample1.txt";
// A text file is read into RDD
JavaRDD<String> lines = sc.textFile(path);
JavaRDD<String> words = lines.flatMap(s -> Arrays.asList(s.split(" ")).iterator());
// RDD being collected for printing
for(String word:words.collect()){
System.out.println(word);
}
}
}
sample1 – sample1.txt:
Welcome to TutorialKart
Learn Apache Spark
Learn to work with RDD
Output:
Example #2
String to words – An example for Spark flatMap in RDD using pyp – Python.
Code:
import sys
from pyspark import SparkContext, SparkConf
if __name__ == "__main__":
#Using Spark configuration, creating a Spark context
conf = SparkConf().setAppName("Read Text to RDD - Python")
sc = SparkContext(conf=conf)
#Input text file is being read to the RDD
lines = sc.textFile("https://cdn.educba.com/home/tutorialeducba/heythere/spark_rdd/sample1.txt")
#Each line to words conversion using flatMap
words = lines.flatMap(lambda line: line.split(" "))
#A list is made from the collection of RDD
list1 = words.collect()
#Printing of the above list1
for line in list1:
print line
spark-submit is given below which is to be run for the above Python code.
Code:
~$ spark-submit flatmap-spark-rdd-exp.py
sample1 – sample1.txt:
Welcome to TutorialKart
Learn Apache Spark
Learn to work with RDD
Output:
Important points to be noted about transformation in flatMap Spark:
- Spark flatMap transformation provides flattened output.
- Lazy evaluation is done in this transformation due to operation of Spark transformation.
- A list, a sequence or an array is returned by this parameter.
- Shuffling of the data is not done from one partition to another partition because of it being a narrow operation.
Conclusion
We have seen the concept of Spark flatMap operation. Spark flatMap transformation operation expresses one to many operation transformation. Which is a transformation of each element from zero to one, two, three or more than those valued elements. In the operation of a flatMap a developer can design his own business of logic custom. And the similar logic is applied to elements throughout the RDD.
Recommended Articles
This is a guide to Spark flatMap. Here we discuss how spark flatMap work? along with examples respectively. You may also have a look at the following articles to learn more –
3 Online Courses | 13+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5
View Course
Related Courses