Support Questions
Find answers, ask questions, and share your expertise
Announcements
Alert: Welcome to the Unified Cloudera Community. Former HCC members be sure to read and learn how to activate your account here.

Explain foreach() operation in apache spark

Highlighted

Explain foreach() operation in apache spark

New Contributor
 
1 REPLY 1

Re: Explain foreach() operation in apache spark

New Contributor

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