Explain foreach() operation in apache spark

Viewing 2 reply threads
  • Author
    Posts
    • #6084
      DataFlair Team
      Moderator

      Explain foreach() operation in apache spark

    • #6089
      DataFlair Team
      Moderator

      > 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

    • #6090
      DataFlair Team
      Moderator

      Why foreach() is considered as an action ?

Viewing 2 reply threads
  • You must be logged in to reply to this topic.