Spark RDD -
Spark is build on the concept of RDD. RDD(Resilient Distributed Dataset) is Spark's primary data abstraction. Its a fault tolerant collection of elements that can be operated on parallel. They are immutable, so once it is created, it cannot be changed. They are the building blocks of spark. RDD are distributed across the worker nodes and operated in parallel..
There are three ways in which RDD are created
- Parallelizing an existing collection
- val data = 1 to 1000
- val dataRDD = sc.parallelize(data) // sc is spark context
- Referencing an existing dataset from HDFS, HBase etc.
- val testFile = sc.textFile("hdfs://testFile")
- Performing transformation to existing RDD.
- val filteredRDD = dataRDD.filter()
Types of files supported by Spark RDDs
- Text Files
- Sequence Files
- Hadoop Input Formats
There are two types of RDD operations
- transformations
- lazily evaluated.
- A transformed RDD is executed only when an action runs on it.
- can also persist, or cache RDDs in memory or on disk.
- actions
- cause Spark to execute the recipe to transform the source data.
- they kick off the job on the cluster
RDD are created when the action operations is performed on a direct acyclic graph(RDD) which is formed using parallelize method or sc.testFile().
when we applied transformation(operation) to the RDD only DAG is adjusted in memory and it returns a pointer to the updated RDD or RDD DAG to be precise. Action (operation) is operated on DAG to return a value (resultant RDD).
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 m |
No comments:
Post a Comment