Hi,
foreach() operation is an action. It does not return any value. It executes input function on each element of an RDD. It executes the function on each item in RDD. It is good for writing database or publishing to web services.
It executes the parameter less function for each data items.
Here is an example below:
val mydata = Array(1,2,3,4,5,6,7,8,9,10)
val rdd1 = sc.parallelize(mydata)
rdd1.foreach{x=>println(x)}
Output:
1
2
3
4
5
6
7
8
9
10