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
6,399 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
• 65,770 points

Related Questions In Apache Spark

+1 vote
8 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
61,868 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 23, 2019 in Apache Spark by Gitika
• 65,770 points
4,929 views
0 votes
1 answer

How to save RDD in Apache Spark?

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

answered Jul 23, 2019 in Apache Spark by Gitika
• 65,770 points
3,640 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
• 65,770 points
7,816 views
+1 vote
2 answers
+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
• 11,380 points
11,077 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
• 11,380 points
2,575 views
+2 votes
11 answers

hadoop fs -put command?

Hi, You can create one directory in HDFS ...READ MORE

answered Mar 16, 2018 in Big Data Hadoop by nitinrawat895
• 11,380 points
109,076 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
• 65,770 points
6,134 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
• 65,770 points
8,681 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP