{"id":1802,"date":"2026-09-13T05:35:54","date_gmt":"2026-09-13T05:35:54","guid":{"rendered":"https:\/\/prwatech.in\/blog\/?p=1802"},"modified":"2026-09-14T15:31:26","modified_gmt":"2026-09-14T15:31:26","slug":"apache-spark-rdd-commands","status":"publish","type":"post","link":"https:\/\/prwatech.in\/blog\/apache-spark\/apache-spark-rdd\/apache-spark-rdd-commands\/","title":{"rendered":"Apache Spark RDD Commands"},"content":{"rendered":"<h1>Apache Spark RDD Commands<\/h1>\n<p>Welcome to the world of best RDD commands used in Apache Spark, In This tutorial, one can easily learn a List of all Top Rated Apache Spark basic RDD commands which are available and are used by most of the Spark developers. Are you also dreaming to become to certified Pro Developer, then stop just dreaming get your Apache Spark certification course from India\u2019s Leading Apache Spark Training institute.<\/p>\n<p>This is a working reference to the core commands for Apache Spark&#8217;s <strong><b>RDD<\/b><\/strong>, short for Resilient Distributed Dataset, Spark&#8217;s original data structure for distributed processing. An RDD is an immutable collection of elements, partitioned across the nodes of a cluster, that can be operated on in parallel.<\/p>\n<p>So follow the below mentioned used from Prwatech and learn <a href=\"https:\/\/prwatech.in\/apache-spark-training-institute-in-bangalore\/\" target=\"_blank\" rel=\"noopener noreferrer\">Apache Spark Course<\/a> like a pro from today itself under 15+ Years of Hands-on Experienced Professionals.<\/p>\n<h2>Basic used in Apache Spark<\/h2>\n<h3><strong>\u00a0Creating a new list with a value<\/strong><\/h3>\n<p>The starting point for most RDD work is turning a plain Scala list into an RDD, which Spark can then spread across a cluster.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1803\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/1-8.png\" alt=\"Apache Spark RDD Commands\" width=\"850\" height=\"62\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/1-8.png 621w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/1-8-300x22.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<p>Parallelizing that list distributes it across partitions, by default 2, so Spark can process it in parallel.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1804\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/2-8.png\" alt=\"Parallelize the data set\" width=\"850\" height=\"81\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/2-8.png 621w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/2-8-300x29.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<h3><strong>Creating new RDD for the list<\/strong><\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1805\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/3-8.png\" alt=\"Apache Spark RDD Commands\" width=\"850\" height=\"82\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/3-8.png 624w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/3-8-300x29.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<h2>RDD Functions<\/h2>\n<p>An RDD supports a wide set of built in functions for transforming and inspecting data, several of which are shown across the screenshots below.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1806\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/4-8.png\" alt=\"\u00a0RDD functions\" width=\"850\" height=\"574\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/4-8.png 622w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/4-8-300x203.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1807\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/5-8.png\" alt=\"Apache Spark RDD Commands\" width=\"850\" height=\"305\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/5-8.png 621w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/5-8-300x108.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1808\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/6-8.png\" alt=\"Apache Spark RDD \" width=\"850\" height=\"62\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/6-8.png 619w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/6-8-300x22.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<h2>Checking RDD Details<\/h2>\n<p>You can check how many partitions or elements an RDD has, which is a useful sanity check before running a larger job.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1809\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/7-8.png\" alt=\"To check total RDD\" width=\"850\" height=\"62\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/7-8.png 620w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/7-8-300x22.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<h3><strong>Creating new RDD with output<\/strong><\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1810\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/8-8.png\" alt=\"Creating new RDD with output\" width=\"850\" height=\"77\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/8-8.png 618w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/8-8-300x27.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<h3><strong>Creating new RDD using map RDD<\/strong><\/h3>\n<p>The <strong><b>map<\/b><\/strong>\u00a0function applies a given operation to every element in an RDD, returning a new RDD with the transformed values.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1811\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/9-7.png\" alt=\"Creating new RDD using map RDD\" width=\"850\" height=\"195\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/9-7.png 623w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/9-7-300x69.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<h3><strong>To check the function of RDD<\/strong><\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1812\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/10-7.png\" alt=\"To check the function of RDD\" width=\"850\" height=\"148\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/10-7.png 625w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/10-7-300x52.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<h2>Transformations and Actions<\/h2>\n<p>Every RDD operation falls into one of two categories, and understanding the difference is the single most important concept for working with RDDs efficiently.<\/p>\n<ul>\n<li><b><\/b><strong><b>Transformations, <\/b><\/strong>such as map, filter, and union, build a new RDD from an existing one, but they are lazy. Spark does not actually run them the moment you write them, it just records what you have asked for.<\/li>\n<li><b><\/b><strong><b>Actions, <\/b><\/strong>such as count, collect, and saveAsTextFile, are what actually trigger execution. Only once an action is called does Spark run through the full chain of transformations that led up to it.<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1813\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/11-7.png\" alt=\"Transformation\" width=\"850\" height=\"60\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/11-7.png 620w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/11-7-300x21.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<h2>Count<\/h2>\n<p>The <strong><b>count<\/b><\/strong>\u00a0action returns the total number of elements in an RDD, such as the total entries in an array.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1814\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/12-5.png\" alt=\"How to setup Apache Spark\" width=\"850\" height=\"61\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/12-5.png 624w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/12-5-300x22.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<h2>Reading and Displaying a File<\/h2>\n<p>An RDD can be built directly from a file, reading its contents in as a collection of lines.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1815\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/13-5.png\" alt=\"Read the file from the source\" width=\"850\" height=\"200\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/13-5.png 624w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/13-5-300x71.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<h3><strong>Display file<\/strong><\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1816\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/14-4.png\" alt=\"Display file\" width=\"850\" height=\"105\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/14-4.png 624w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/14-4-300x37.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<h2>Splitting and Measuring Data<\/h2>\n<p>Once data is loaded, you can split it apart using a keyword or delimiter, then check the length of the resulting array.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1817\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/15-4.png\" alt=\"Using keyword to separate data\" width=\"850\" height=\"92\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/15-4.png 622w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/15-4-300x32.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<h3><strong>To find the length of the array<\/strong><\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1818\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/16-4.png\" alt=\"To find the length of the array\" width=\"850\" height=\"68\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/16-4.png 621w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/16-4-300x24.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<h3><strong>To check value using index<\/strong><\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1819\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/17-4.png\" alt=\"Resilient Distributed\u00a0Dataset(RDD):\" width=\"850\" height=\"156\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/17-4.png 603w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/17-4-300x55.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<h2>Line Level Operations<\/h2>\n<p>Some operations work at the level of individual lines within an RDD, such as filtering lines that start with a specific keyword.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1820\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/18-3.png\" alt=\"Line operations\" width=\"850\" height=\"739\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/18-3.png 572w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/18-3-300x261.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<h3><strong>Starting keyword<\/strong><\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1821\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/19-3.png\" alt=\"Starting keyword\" width=\"850\" height=\"62\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/19-3.png 613w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/19-3-300x22.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<h2>Combining RDDs: Union and Intersection<\/h2>\n<h3>Union<\/h3>\n<p>The <strong><b>union<\/b><\/strong>\u00a0function combines two RDDs into one, keeping every element from both, including duplicates.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1822\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/20-3.png\" alt=\"Creating two arrays for the union\" width=\"850\" height=\"197\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/20-3.png 622w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/20-3-300x69.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1823\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/21-3.png\" alt=\"Creating two arrays for the union &lt;\" width=\"850\" height=\"202\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/21-3.png 620w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/21-3-300x71.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<h3><strong>Intersection<\/strong><\/h3>\n<p>The <strong><b>intersection<\/b><\/strong>\u00a0function instead returns only the elements that appear in both RDDs.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1824\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/22-2.png\" alt=\"Intersection\" width=\"850\" height=\"410\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/22-2.png 564w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/22-2-300x145.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<h3>Join<\/h3>\n<p>The <strong><b>join<\/b><\/strong>\u00a0function combines two key value RDDs based on matching keys, similar to a join between two tables in SQL.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1825\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/23-2.png\" alt=\"Join function\" width=\"850\" height=\"503\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/23-2.png 623w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/23-2-300x178.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<h3><strong>Using the index to get value<\/strong><\/h3>\n<p>You can retrieve values from an RDD by index and format them for cleaner, more readable output.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1826\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/24-2.png\" alt=\"Using the index to get value\" width=\"850\" height=\"61\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/24-2.png 622w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/24-2-300x22.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/> <img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1827\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/25-2.png\" alt=\"Using the index to get value&lt;\" width=\"850\" height=\"206\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/25-2.png 627w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/25-2-300x73.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1828\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/26-2.png\" alt=\"Using the index to get value&gt;\" width=\"850\" height=\"67\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/26-2.png 623w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/26-2-300x24.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<h3><strong>Using values to print in a proper format<\/strong><\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1829\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/27-2.png\" alt=\"Using values to print in a proper format\" width=\"850\" height=\"87\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/27-2.png 623w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/27-2-300x31.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<h3><strong>Print multiple values using for loop<\/strong><\/h3>\n<p>A for loop can print multiple values from an RDD in sequence.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1830\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/28-2.png\" alt=\"Print multiple values using for loop\" width=\"850\" height=\"175\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/28-2.png 622w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/28-2-300x62.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<h2>Word Count With MapReduce<\/h2>\n<p>The classic word count example uses map to split text into individual words paired with a count of one, then <strong><b>reduceByKey<\/b><\/strong>\u00a0to sum those counts together for each unique word.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1831\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/29-2.png\" alt=\"Using MapReduce in RDD\" width=\"850\" height=\"121\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/29-2.png 617w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/29-2-300x43.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1832\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/30-2.png\" alt=\"word count\" width=\"850\" height=\"98\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/30-2.png 623w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/30-2-300x35.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<h2>Group By Key and Map Values<\/h2>\n<p>The <strong><b>groupByKey<\/b><\/strong>\u00a0function groups all values sharing the same key together into a single collection per key.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1833\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/31-2.png\" alt=\"Group By Key\" width=\"850\" height=\"366\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/31-2.png 627w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/31-2-300x129.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<h3><strong>Map value\u00a0<\/strong><\/h3>\n<p>The <strong><b>mapValues<\/b><\/strong>\u00a0function applies a transformation to only the values in a key value RDD, leaving the keys untouched.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1834\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/32-2.png\" alt=\"Map value\u00a0\" width=\"850\" height=\"96\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/32-2.png 623w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/32-2-300x34.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<h3>Aggregating Values<\/h3>\n<p><strong>Add :<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1835\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/33-2.png\" alt=\"Aggregation of integer value\" width=\"850\" height=\"80\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/33-2.png 627w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/33-2-300x28.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<p><strong>Subtract :<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1836\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/34-2.png\" alt=\"Subtract\" width=\"850\" height=\"80\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/34-2.png 625w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/34-2-300x28.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<p><strong>Changing the Parallelism:<\/strong><br \/>\nThe number of partitions an RDD is split across, its parallelism, can be adjusted directly, which affects how the work is distributed across the cluster.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1837\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/35-2.png\" alt=\"Changing the parallelism\" width=\"850\" height=\"85\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/35-2.png 619w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/35-2-300x30.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<h2>Ordering Results<\/h2>\n<p>You can select a specific number of elements from an RDD based on a given ordering, such as the top or bottom values in an array.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1838\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/36-2.png\" alt=\"Take order\" width=\"850\" height=\"89\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/36-2.png 622w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/36-2-300x31.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<h2>Saving RDDs to a File<\/h2>\n<p>The <strong><b>saveAsTextFile<\/b><\/strong>\u00a0action writes an RDD&#8217;s contents out to a specified location, ready to be read back in or handed off to another process.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1839\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/37-2.png\" alt=\"Save file command\" width=\"850\" height=\"198\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/37-2.png 623w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/37-2-300x70.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1840\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/38-2.png\" alt=\"Save file\" width=\"850\" height=\"562\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/38-2.png 534w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/38-2-300x198.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<h3><strong>Saving the file in map format<\/strong><\/h3>\n<p>An RDD of key value pairs can also be saved specifically in map format, preserving that key value structure.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1841\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/39-2.png\" alt=\"Saving the file in map format\" width=\"850\" height=\"65\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/39-2.png 625w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/39-2-300x23.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1842\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/40-2.png\" alt=\"Saving the file in map format command\" width=\"850\" height=\"385\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/40-2.png 629w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/40-2-300x136.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<h2>RDD Compared to DataFrame<\/h2>\n<p>RDD was Spark&#8217;s original API, and it remains fully supported, but most day to day work today happens through DataFrames instead, covered in the Spark SQL Commands guide. The difference comes down to structure and optimization. A DataFrame organizes data into named columns and benefits from Spark&#8217;s Catalyst optimizer, which can significantly speed up common operations such as filtering and joining. An RDD has no built in structure and no automatic optimization, but it gives you full, low level control over exactly how your data is transformed and partitioned.<\/p>\n<ul>\n<li>Reach for a DataFrame when your data is structured or semi structured and you want Spark to optimize the query plan for you, which covers most everyday analysis.<\/li>\n<li>Reach for an RDD when you need fine grained control over partitioning and transformations, when your data is unstructured, or when your logic does not map cleanly onto columns and rows.<\/li>\n<\/ul>\n<h2>Common Mistakes to Avoid<\/h2>\n<ul>\n<li>Calling multiple actions on the same RDD without caching it. Since transformations are lazy, calling count and then collect on the same uncached RDD recomputes the entire chain of transformations twice. Use cache or persist if you plan to reuse an RDD more than once.<\/li>\n<li>Confusing map with flatMap. map returns exactly one output element per input element, while flatMap can return zero, one, or many, which matters for operations like splitting a line of text into multiple words.<\/li>\n<li>Reaching for an RDD by default. If your data is structured and a DataFrame would work just as well, the DataFrame version is usually faster, thanks to Catalyst, and easier for someone else to read later.<\/li>\n<\/ul>\n<h2>Watch It in Action<\/h2>\n<p>The two videos below, embedded on the original page, walk through these RDD commands step by step.<\/p>\n<p><iframe loading=\"lazy\" src=\"https:\/\/www.youtube.com\/embed\/9VTX8YME9kk\" width=\"650\" height=\"315\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<p>That covers the core Apache Spark RDD commands, from creating and transforming RDDs through joins, aggregation, and saving output. To go further, explore <a href=\"https:\/\/prwatech.in\/apache-spark-training-institute-in-pune\/\"><strong><b>Prwatech&#8217;s Apache Spark training<\/b><\/strong><\/a>\u00a0program, which includes placement assistance.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Apache Spark RDD Commands Welcome to the world of best RDD commands used in Apache Spark, In This tutorial, one can easily learn a List of all Top Rated Apache Spark basic RDD commands which are available and are used by most of the Spark developers. Are you also dreaming to become to certified Pro [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21,1690],"tags":[891,294,892,890,889,893,894,895,896,888],"class_list":["post-1802","post","type-post","status-publish","format-standard","hentry","category-apache-spark","category-apache-spark-rdd","tag-apache-spark-rdd","tag-apache-spark-rdd-commands","tag-creating-rdd-list-in-spark","tag-leran-rdd-commands","tag-rdd-commands-in-spark-scala","tag-rdd-functions","tag-rdd-functions-in-spark","tag-rdd-intersection","tag-rdd-join-function","tag-spark-rdd-commands"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Apache Spark RDD Commands: A Practical Guide | PrwaTech<\/title>\n<meta name=\"description\" content=\"Learn essential Apache Spark RDD commands, from creating and transforming RDDs to joins, aggregation, and saving output, with clear examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prwatech.in\/blog\/apache-spark\/apache-spark-rdd\/apache-spark-rdd-commands\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Apache Spark RDD Commands: A Practical Guide | PrwaTech\" \/>\n<meta property=\"og:description\" content=\"Learn essential Apache Spark RDD commands, from creating and transforming RDDs to joins, aggregation, and saving output, with clear examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prwatech.in\/blog\/apache-spark\/apache-spark-rdd\/apache-spark-rdd-commands\/\" \/>\n<meta property=\"og:site_name\" content=\"Prwatech\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prwatech.in\/\" \/>\n<meta property=\"article:published_time\" content=\"2026-09-13T05:35:54+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-09-14T15:31:26+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/1-8.png\" \/>\n\t<meta property=\"og:image:width\" content=\"621\" \/>\n\t<meta property=\"og:image:height\" content=\"45\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Prwatech\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@Eduprwatech\" \/>\n<meta name=\"twitter:site\" content=\"@Eduprwatech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prwatech\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"13 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prwatech.in\/blog\/apache-spark\/apache-spark-rdd\/apache-spark-rdd-commands\/\",\"url\":\"https:\/\/prwatech.in\/blog\/apache-spark\/apache-spark-rdd\/apache-spark-rdd-commands\/\",\"name\":\"Apache Spark RDD Commands: A Practical Guide | PrwaTech\",\"isPartOf\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prwatech.in\/blog\/apache-spark\/apache-spark-rdd\/apache-spark-rdd-commands\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prwatech.in\/blog\/apache-spark\/apache-spark-rdd\/apache-spark-rdd-commands\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/1-8.png\",\"datePublished\":\"2026-09-13T05:35:54+00:00\",\"dateModified\":\"2026-09-14T15:31:26+00:00\",\"author\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\"},\"description\":\"Learn essential Apache Spark RDD commands, from creating and transforming RDDs to joins, aggregation, and saving output, with clear examples.\",\"breadcrumb\":{\"@id\":\"https:\/\/prwatech.in\/blog\/apache-spark\/apache-spark-rdd\/apache-spark-rdd-commands\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prwatech.in\/blog\/apache-spark\/apache-spark-rdd\/apache-spark-rdd-commands\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prwatech.in\/blog\/apache-spark\/apache-spark-rdd\/apache-spark-rdd-commands\/#primaryimage\",\"url\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/1-8.png\",\"contentUrl\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/1-8.png\",\"width\":621,\"height\":45},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prwatech.in\/blog\/apache-spark\/apache-spark-rdd\/apache-spark-rdd-commands\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/prwatech.in\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Apache Spark RDD Commands\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/prwatech.in\/blog\/#website\",\"url\":\"https:\/\/prwatech.in\/blog\/\",\"name\":\"Prwatech\",\"description\":\"Share Ideas, Start Something Good.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/prwatech.in\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\",\"name\":\"Prwatech\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g\",\"caption\":\"Prwatech\"},\"url\":\"https:\/\/prwatech.in\/blog\/author\/prwatech123\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Apache Spark RDD Commands: A Practical Guide | PrwaTech","description":"Learn essential Apache Spark RDD commands, from creating and transforming RDDs to joins, aggregation, and saving output, with clear examples.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/prwatech.in\/blog\/apache-spark\/apache-spark-rdd\/apache-spark-rdd-commands\/","og_locale":"en_US","og_type":"article","og_title":"Apache Spark RDD Commands: A Practical Guide | PrwaTech","og_description":"Learn essential Apache Spark RDD commands, from creating and transforming RDDs to joins, aggregation, and saving output, with clear examples.","og_url":"https:\/\/prwatech.in\/blog\/apache-spark\/apache-spark-rdd\/apache-spark-rdd-commands\/","og_site_name":"Prwatech","article_publisher":"https:\/\/www.facebook.com\/prwatech.in\/","article_published_time":"2026-09-13T05:35:54+00:00","article_modified_time":"2026-09-14T15:31:26+00:00","og_image":[{"width":621,"height":45,"url":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/1-8.png","type":"image\/png"}],"author":"Prwatech","twitter_card":"summary_large_image","twitter_creator":"@Eduprwatech","twitter_site":"@Eduprwatech","twitter_misc":{"Written by":"Prwatech","Est. reading time":"13 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/prwatech.in\/blog\/apache-spark\/apache-spark-rdd\/apache-spark-rdd-commands\/","url":"https:\/\/prwatech.in\/blog\/apache-spark\/apache-spark-rdd\/apache-spark-rdd-commands\/","name":"Apache Spark RDD Commands: A Practical Guide | PrwaTech","isPartOf":{"@id":"https:\/\/prwatech.in\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prwatech.in\/blog\/apache-spark\/apache-spark-rdd\/apache-spark-rdd-commands\/#primaryimage"},"image":{"@id":"https:\/\/prwatech.in\/blog\/apache-spark\/apache-spark-rdd\/apache-spark-rdd-commands\/#primaryimage"},"thumbnailUrl":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/1-8.png","datePublished":"2026-09-13T05:35:54+00:00","dateModified":"2026-09-14T15:31:26+00:00","author":{"@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3"},"description":"Learn essential Apache Spark RDD commands, from creating and transforming RDDs to joins, aggregation, and saving output, with clear examples.","breadcrumb":{"@id":"https:\/\/prwatech.in\/blog\/apache-spark\/apache-spark-rdd\/apache-spark-rdd-commands\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prwatech.in\/blog\/apache-spark\/apache-spark-rdd\/apache-spark-rdd-commands\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prwatech.in\/blog\/apache-spark\/apache-spark-rdd\/apache-spark-rdd-commands\/#primaryimage","url":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/1-8.png","contentUrl":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/05\/1-8.png","width":621,"height":45},{"@type":"BreadcrumbList","@id":"https:\/\/prwatech.in\/blog\/apache-spark\/apache-spark-rdd\/apache-spark-rdd-commands\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/prwatech.in\/blog\/"},{"@type":"ListItem","position":2,"name":"Apache Spark RDD Commands"}]},{"@type":"WebSite","@id":"https:\/\/prwatech.in\/blog\/#website","url":"https:\/\/prwatech.in\/blog\/","name":"Prwatech","description":"Share Ideas, Start Something Good.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/prwatech.in\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3","name":"Prwatech","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g","caption":"Prwatech"},"url":"https:\/\/prwatech.in\/blog\/author\/prwatech123\/"}]}},"_links":{"self":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/1802","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/comments?post=1802"}],"version-history":[{"count":19,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/1802\/revisions"}],"predecessor-version":[{"id":11751,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/1802\/revisions\/11751"}],"wp:attachment":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/media?parent=1802"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/categories?post=1802"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/tags?post=1802"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}