

apply a function for each element of RDD List data = Arrays.asList("Learn","Apache","Spark","with","Tutorial Kart") JavaSparkContext sc = new JavaSparkContext(sparkConf) SparkConf sparkConf = new SparkConf().setAppName("Spark RDD foreach Example") We shall use RDD.foreach() on this RDD, and for each item in the RDD, we shall print the item. The difference between mutable and immutable objects is that when an object is immutable. There are two kinds of Maps, the immutable and the mutable.

Keys are unique in the Map, but values need not be unique. Any value can be retrieved based on its key. Here’s a description of Future from its Scaladoc: A Future represents a value which may or may not currently be. In this example, we will take an RDD with strings as elements. Scala map is a collection of key/value pairs. When you want to write parallel and concurrent applications in Scala, you could still use the native Java Thread but the Scala Future makes parallel/concurrent programming much simpler, and it’s preferred. Now, it’s time to try to give to the yield body some dignity.Syntax of RDD foreach public void foreach(scala.Function1 f)Īrgument could be a lambda function or use .java.function VoidFunction functional interface as the assignment target for a lambda expression or method reference.įoreach method does not modify the contents of RDD. Let’s add it to the Result type:ĭef foreach(f: A => Unit): Unit = f(result) So, the Scala compiler desugars the above construct to a call to the foreach method. The compiler warns us that we cannot use the variable res in the for-comprehension: Value foreach is not a member of .ForComprehension.Result. Let’s create a Result class that is a wrapper around an expression result: case class Result(result: A)įirst things first, we’ll try to print to standard output the value of a Result using a for-comprehension: val result: Result = Result(42) Let’s see an example.įirst of all, let’s define a class to work on. We can use for-comprehension syntax on every type that defines such methods. In Scala, the for-comprehension is nothing more than syntactic sugar to a sequence of calls to one or more of the methods: In the previous example, we saw how the semantics of a for -comprehension are equal to that of a sequence of operations on streams or sequences.
#SCALA FOREACH GENERATOR#
So, we can mix a generator of type List with a generator of type List. As an example, you can use foreach method to loop through all elements in a collection. In our previous example, both were instances of List. The foreach method takes a function as parameter and applies it to every element in the collection.

The values contained in the numberOfAssertsWithExecutionTime list are: ListĪll the generators inside a for-comprehension must share the same type they loop over. } yield ((id, result.totalAsserts, time)) Val numberOfAssertsWithExecutionTime: List = Then, we merge the two elements, listing the total number of asserts executed for each test result, along with the execution time: val executionTimeList = List(("test 1", 100), ("test 2", 230)) In the example, we loop over the list of results and the list of execution times. They loop independently from each other, producing all the possible combinations of their variables. We can have as many generators as we want. It introduces a new variable, result, that loops over each value of the variable results. The foreach method takes a function as parameter and applies it to every element in the collection. The statement result <- results represent a generator. The foreach function is applicable to both Scala's Mutable and Immutable collection data structures.
