Apache Spark™ is a general purpose compute engine for large scale data processing.
Spark offers
- Lazy Computations
- In Memory data caching
Two main abstraction of Spark
- RDD (Resilient Distributed Data Set)
- Collection of data items split into partitions and stored in memory on worker nodes of the cluster.
- Shared Variables : ships a copy of each variable used in the function to each task
- broadcast variables : to cache a value in memory on all nodes
- accumulators : variables that are only “added” to, such as counters and sums.
RDD
Driver Program:
At a high level, every Spark application consists of a driver program that runs the user’s
SparkContext
RDDs (Resilient Distributed Datasets)
There are two ways to create RDDs: parallelizing an existing collection in your driver program, or referencing a dataset in an external storage system, such as a shared filesystem, HDFS, HBase, or any data source offering a Hadoop InputFormat.
External Datasets
RDD Operations
RDDs support two types of operations: transformations, which create a new dataset from an existing one (example:
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
main
function and executes various parallel operations on a cluster.SparkContext
RDDs (Resilient Distributed Datasets)
There are two ways to create RDDs: parallelizing an existing collection in your driver program, or referencing a dataset in an external storage system, such as a shared filesystem, HDFS, HBase, or any data source offering a Hadoop InputFormat.
Parallelized Collections
Parallelized collections are created by calling JavaSparkContext
’s parallelize
method on an existing Collection
in your driver program.List<Integer> data = Arrays.asList(1, 2, 3, 4, 5);
JavaRDD<Integer> distData = sc.parallelize(data);
External Datasets
Spark can create distributed datasets from any storage source supported by Hadoop, including your local file system, HDFS, Cassandra, HBase, Amazon S3, etc. Spark supports text files, SequenceFiles, and any other Hadoop InputFormat.
Text file RDDs can be created using
SparkContext
’s textFile
method. RDD Operations
RDDs support two types of operations: transformations, which create a new dataset from an existing one (example:
map
), and actions, which return a value to the driver program after running a computation on the dataset (example: reduce
).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.
Basics
To illustrate RDD basics, consider the simple program below:
The first line defines a base RDD from an external file. This dataset is not loaded in memory or otherwise acted on:
lines
is merely a pointer to the file. The second line defines lineLengths
as the result of a map
transformation. Again, lineLengths
is not immediately computed, due to laziness. Finally, we run reduce
, which is an action. At this point Spark breaks the computation into tasks to run on separate machines, and each machine runs both its part of the map and a local reduction, returning only its answer to the driver program.Transformations
The following table lists some of the common transformations supported by Spark.
Actions
The following table lists some of the common actions supported by Spark.
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, etc). |
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 or interacting with external storage systems. Note: modifying variables other than Accumulators outside of the foreach() may result in undefined behavior. See Understanding closures for more details. |
RDD Persistence
One of the most important capabilities in Spark is persisting (or caching) a dataset in memory across operations. When you persist an RDD, each node stores any partitions of it that it computes in memory and reuses them in other actions on that dataset (or datasets derived from it). This allows future actions to be much faster (often by more than 10x). Caching is a key tool for iterative algorithms and fast interactive use.
No comments:
Post a Comment