Actions

Action triggers the entire DAG (Directed Acyclic Graph) of transformations built so far to be materialized by running the code blocks and functions. All operations are now executed as the DAG specifies.

There are two kinds of action operations:

  • Driver: One kind of action is the driver action such as collect count, count by key, and so on. Each such action performs some calculations on the remote executor and pulls the data back into the driver.
Driver-based action has the problem that actions on large datasets can easily overwhelm the memory available on the driver taking down the application, so you should use the driver involved actions judiciously
  • Distributed: Another kind of action is a distributed action, which is executed on the nodes in the cluster. An example of such a distributed action is saveAsTextfile. This is the most common action operation due to the desirable distributed nature of the operation.

The following is the list of action functions as available in the latest Spark 2.1.1:

Action Meaning
reduce(func) Aggregate the elements of the dataset using a function func (which takes two arguments and returns one). The function should be commutative and associative so that it can be computed correctly in parallel.
collect() Return all the elements of the dataset as an array at the driver program. This is usually useful after a filter or other operation that returns a sufficiently small subset of the data.
count() Return the number of elements in the dataset.
first() Return the first element of the dataset (similar to take(1)).
take(n) Return an array with the first n elements of the dataset.
takeSample(withReplacement, num, [seed]) Return an array with a random sample of num elements of the dataset, with or without replacement, optionally pre-specifying a random number generator seed.
takeOrdered(n, [ordering]) Return the first n elements of the RDD using either their natural order or a custom comparator.
saveAsTextFile(path) Write the elements of the dataset as a text file (or set of text files) in a given directory in the local filesystem, HDFS or any other Hadoop-supported file system. Spark will call toString on each element to convert it to a line of text in the file.
saveAsSequenceFile(path)
(Java and Scala)
Write the elements of the dataset as a Hadoop SequenceFile in a given path in the local filesystem, HDFS, or any other Hadoop-supported file system. This is available on RDDs of key-value pairs that implement Hadoop's Writable interface. In Scala, it is also available on types that are implicitly convertible to Writable (Spark includes conversions for basic types like Int, Double, String, and so on).
saveAsObjectFile(path)
(Java and Scala)
Write the elements of the dataset in a simple format using Java serialization, which can then be loaded using SparkContext.objectFile().
countByKey() Only available on RDDs of type (K, V). Returns a hashmap of (K, Int) pairs with the count of each key.
foreach(func) Run a function func on each element of the dataset. This is usually done for side effects such as updating an accumulator (http://spark.apache.org/docs/latest/programming-guide.html#accumulators) or interacting with external storage systems.
Note: modifying variables other than accumulators outside of the foreach() may result in undefined behavior. See understanding closures (http://spark.apache.org/docs/latest/programming-guide.html#understanding-closures-a-nameclosureslinka) for more details.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.148.113.155