Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I force Spark to execute code?

How can I force Spark to execute a call to map, even if it thinks it does not need to be executed due to its lazy evaluation?

I have tried to put cache() with the map call but that still doesn't do the trick. My map method actually uploads results to HDFS. So, its not useless, but Spark thinks it is.

like image 661
MetallicPriest Avatar asked Jul 13 '15 12:07

MetallicPriest


People also ask

How do you force a Spark to calculate?

Forcing computation on RDDs is relatively simple, all you need to do is call count() and Spark will evaluate the RDD.

What is lazy execution in Spark?

In Spark, Lazy Evaluation means that You can apply as many TRANSFORMATIONs as you want, but Spark will not start the execution of the process until an ACTION is called. 💡 So transformations are lazy but actions are eager.

How can you run a Spark program from the command line?

Go to the Apache Spark Installation directory from the command line and type bin/spark-shell and press enter, this launches Spark shell and gives you a scala prompt to interact with Spark in scala language. If you have set the Spark in a PATH then just enter spark-shell in command line or terminal (mac users).


2 Answers

Short answer:

To force Spark to execute a transformation, you'll need to require a result. Sometimes a simple count action is sufficient.

TL;DR:

Ok, let's review the RDD operations.

RDDs support two types of operations:

  • transformations - which create a new dataset from an existing one.
  • actions - which return a value to the driver program after running a computation on the dataset.

For example, map is a transformation that passes each dataset element through a function and returns a new RDD representing the results. On the other hand, reduce is an action that aggregates all the elements of the RDD using some function and returns the final result to the driver program (although there is also a parallel reduceByKey that returns a distributed dataset).

All transformations in Spark are lazy, in that they do not compute their results right away.

Instead, they just remember the transformations applied to some base dataset (e.g. a file). The transformations are only computed when an action requires a result to be returned to the driver program. This design enables Spark to run more efficiently – for example, we can realize that a dataset created through map will be used in a reduce and return only the result of the reduce to the driver, rather than the larger mapped dataset.

By default, each transformed RDD may be recomputed each time you run an action on it. However, you may also persist an RDD in memory using the persist (or cache) method, in which case Spark will keep the elements around on the cluster for much faster access the next time you query it. There is also support for persisting RDDs on disk, or replicated across multiple nodes.

Conclusion

To force Spark to execute a call to map, you'll need to require a result. Sometimes a count action is sufficient.

Reference

  • Spark Programming Guide.
like image 151
eliasah Avatar answered Sep 23 '22 03:09

eliasah


Spark transformations only describe what has to be done. To trigger an execution you need an action.

In your case there is a deeper problem. If goal is to create some kind of side effect, like storing data on HDFS, the right method to use is foreach. It is both an action and has a clean semantics. What is also important, unlike map, it doesn't imply referential transparency.

like image 23
zero323 Avatar answered Sep 22 '22 03:09

zero323