Support Questions

Find answers, ask questions, and share your expertise
Announcements
Celebrating as our community reaches 100,000 members! Thank you!

Explain foreach() operation in apache spark

avatar
Explorer
 
1 REPLY 1

avatar

foreach() operation is an action.

> It do not return any value.
> It executes input function on each element of an RDD.

From :
http://data-flair.training/blogs/rdd-transformations-actions-apis-apache-spark/#39_Foreach

It executes the function on each item in RDD. It is good for writing database or publishing to web services. It executes parameter less function for each data items.

Example:

val mydata = Array(1,2,3,4,5,6,7,8,9,10)
val rdd1 = sc.parallelize(mydata)
rdd1.foreach{x=>println(x)}

OR

rdd1.foreach{println}


Output:

1
2
3
4
5
6
7
8
9
10