How Foreach Operation works in Apache Spark?

0 votes

Can anyone explain how foreach operation works in Apache Spark?

Aug 2, 2019 in Apache Spark by Nidhi
284 views

1 answer to this question.

0 votes

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

answered Aug 2, 2019 by Gitika
• 25,460 points

Related Questions In Apache Spark

0 votes
7 answers

How to print the contents of RDD in Apache Spark?

Save it to a text file: line.saveAsTextFile("alicia.txt") Print contains ...READ MORE

answered Dec 10, 2018 in Apache Spark by Akshay
16,393 views
0 votes
1 answer

How to check if a particular keyword exists in Apache Spark?

Hey, You can try this code to get ...READ MORE

answered Jul 22, 2019 in Apache Spark by Gitika
• 25,460 points
166 views
0 votes
1 answer

How to save RDD in Apache Spark?

Hey, There are few methods provided by the ...READ MORE

answered Jul 22, 2019 in Apache Spark by Gitika
• 25,460 points
240 views
0 votes
1 answer

How to work with Matrix Multiplication in Apache Spark?

Hey, You can follow this below solution for ...READ MORE

answered Jul 31, 2019 in Apache Spark by Gitika
• 25,460 points
1,359 views
+1 vote
1 answer
+1 vote
1 answer

Hadoop Mapreduce word count Program

Firstly you need to understand the concept ...READ MORE

answered Mar 16, 2018 in Data Analytics by nitinrawat895
• 10,840 points
3,923 views
0 votes
1 answer

hadoop.mapred vs hadoop.mapreduce?

org.apache.hadoop.mapred is the Old API  org.apache.hadoop.mapreduce is the ...READ MORE

answered Mar 16, 2018 in Data Analytics by nitinrawat895
• 10,840 points
542 views
+1 vote
11 answers

hadoop fs -put command?

put syntax: put <localSrc> <dest> copy syntax: copyFr ...READ MORE

answered Dec 7, 2018 in Big Data Hadoop by Aditya
20,915 views
0 votes
1 answer

How SortBykey() operation works in Spark?

Hey, sortByKey() is a transformation. It returns an RDD sorted ...READ MORE

answered Aug 2, 2019 in Apache Spark by Gitika
• 25,460 points
533 views
0 votes
1 answer

How fault tolerance is achieved in Apache Spark?

Hey, In Apache Spark, the data storage model is ...READ MORE

answered Jul 22, 2019 in Apache Spark by Gitika
• 25,460 points
180 views