Applies a function to all elements of this RDD. 1

Applies a function to all elements of this RDD.

def f(x): print(x)
sc.parallelize([1, 2, 3, 4, 5]).foreach(f)

Here is what the above code is Doing:
1. Create a new RDD that has 5 partitions, each with a single element in it.
2. Pass a function to foreach() to be run on each element in the RDD.
3. Because the elements of the RDD are processed in parallel, the order in which they are printed is not deterministic.

Similar Posts