Resilient Distributed Datasets (RDDs)
RDDs are the main logical data units in Spark. They are a distributed collection of objects, which are stored in memory or on disks of different machines of a cluster. A single RDD can be divided into multiple logical partitions so that these partitions can be stored and processed on different machines of a cluster.
Check out this insightful video on Spark Tutorial for Beginners:
RDDs are immutable (read-only) in nature. You cannot change an original RDD, but you can create new RDDs by performing coarse-grain operations, like transformations, on an existing RDD.
Enhance your skills in Apache Spark by grabbing this Big Data and Spark Training Course!
An RDD in Spark can be cached and used again for future transformations, which is a huge benefit for users. RDDs are said to be lazily evaluated, i.e., they delay the evaluation until it is really needed. This saves a lot of time and improves efficiency.
Features of an RDD in Spark
- Resilience: RDDs track data lineage information to recover lost data, automatically on failure. It is also called fault tolerance.
- Distributed: Data present in an RDD resides on multiple nodes. It is distributed across different nodes of a cluster.
- Lazy evaluation: Data does not get loaded in an RDD even if you define it. Transformations are actually computed when you call an action, such as count or collect, or save the output to a file system.
- Immutability: Data stored in an RDD is in the read-only mode━you cannot edit the data which is present in the RDD. But, you can create new RDDs by performing transformations on the existing RDDs.
- In-memory computation: An RDD stores any immediate data that is generated in the memory (RAM) than on the disk so that it provides faster access.
- Partitioning: Partitions can be done on any existing RDD to create logical parts that are mutable. You can achieve this by applying transformations on the existing partitions.
Operations on RDDs
There are two basic operations that can be done on RDDs. They are transformations and actions.
Want to grasp a detailed knowledge of Hadoop? Read this extensive Spark Tutorial!
These are functions that accept the existing RDDs as input and outputs one or more RDDs. However, the data in the existing RDD in Spark does not change as it is immutable. Some of the transformation operations are provided in the table below:
||Returns a new RDD by applying the function on each data element
||Returns a new RDD formed by selecting those elements of the source on which the function returns true
||Aggregates the values of a key using a function
||Converts a (key, value) pair into a (key, <iterable value>) pair
||Returns a new RDD that contains all elements and arguments from the source RDD
||Returns a new RDD that contains an intersection of the elements in the datasets
These transformations are executed when they are invoked or called. Every time transformations are applied, a new RDD is created.
If you have any more queries related to Spark and Hadoop, come to our Big Data Hadoop and Spark Community!
Actions in Spark are functions that return the end result of RDD computations. It uses a lineage graph to load data onto the RDD in a particular order. After all of the transformations are done, actions return the final result to the Spark Driver. Actions are operations that provide non-RDD values. Some of the common actions used in Spark are given below:
||Gets the number of data elements in an RDD
||Gets all the data elements in an RDD as an array
||Aggregates data elements into an RDD by taking two arguments and returning one
||Fetches the first n elements of an RDD
||Executes the operation for each data element in an RDD
||Retrieves the first data element of an RDD
Creating an RDD
An RDD can be created in three ways. Let’s discuss them one by one.
By Loading an External Dataset
You can load an external file onto an RDD. The types of files you can load are csv, txt, JSON, etc. Here is the example of loading a text file onto an RDD:
By Parallelizing the Collection of Objects
When Spark’s parallelize method is applied to a group of elements, a new distributed dataset is created. This dataset is an RDD.
Make yourself job-ready with these top Spark Interview Questions and Answers today!
Below, you can see how to create an RDD by applying the parallelize method to a collection that consists of six elements:
By Performing Transformations on the Existing RDDs
One or more RDDs can be created by performing transformations on the existing RDDs as mentioned earlier in this tutorial page. The below figure shows how a map() function can be used to create an RDD:
However, the data inside RDDs are not always organized or structured since the data is stored from different sources.
In a further section of this Apache Spark tutorial, you will learn about Spark SQL that organizes data into rows and columns. Besides, you will come to know about Spark SQL libraries that provide APIs to connect to Spark SQL through JDBC/ODBC connections and perform queries (table operations) on structured data, which is not possible in an RDD in Spark. Stay tuned!
Want to become a master in Apache Spark? Check out this Big Data and Spark Course in London!