{"id":1315,"date":"2026-09-12T12:11:18","date_gmt":"2026-09-12T12:11:18","guid":{"rendered":"https:\/\/prwatech.in\/blog\/?p=1315"},"modified":"2026-09-13T13:55:02","modified_gmt":"2026-09-13T13:55:02","slug":"scala-commands-for-spark","status":"publish","type":"post","link":"https:\/\/prwatech.in\/blog\/apache-spark\/apache-spark-scala\/scala-commands-for-spark\/","title":{"rendered":"Scala commands for Spark"},"content":{"rendered":"<h1>Scala Commands for Spark<\/h1>\n<p>This is a working reference to the core <a href=\"https:\/\/prwatech.in\/apache-spark-training-institute-in-pune\/\">Scala language commands<\/a> every Spark developer runs into early on, expressions, values, functions, classes, loops, and collections, each with a short explanation and example.<\/p>\n<h2>Expressions<\/h2>\n<p>An expression is simply a computable statement. Typed into the Scala shell, it evaluates immediately and returns a result.<\/p>\n<p style=\"text-align: left;\"><em>scala&gt; 12 * 10<\/em><br \/>\n<em>res0: Int = 120<\/em><\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1316\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/1.png\" alt=\"Scala commands for Spark\" width=\"850\" height=\"128\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/1.png 666w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/1-300x45.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/strong><\/p>\n<h2>Printing Output With println<\/h2>\n<p>You can print the result of an expression directly using <strong><b>println<\/b><\/strong>.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1317\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/2.png\" alt=\"scala print\" width=\"850\" height=\"119\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/2.png 664w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/2-300x42.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/strong><\/p>\n<p>scala&gt; println(&#8220;Hello, &#8221; + &#8220;Prwatech&#8221;)<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1318\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/3.png\" alt=\"scala print in\" width=\"850\" height=\"158\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/3.png 674w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/3-300x56.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/strong><\/p>\n<h3><strong>Values<\/strong><\/h3>\n<p>The <strong><b>val<\/b><\/strong>\u00a0keyword names the result of an expression. Referencing a value later does not recompute it, it stays fixed.<\/p>\n<p>Here such as\u00a0 \u2018x\u2019 is value \u201cReferencing a value doesn\u2019t re-compute it\u201d<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1319\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/4.png\" alt=\"values\" width=\"850\" height=\"167\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/4.png 672w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/4-300x59.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/strong><\/p>\n<h2>Variables<\/h2>\n<p>Variables behave like values, except you can reassign them. Define one with the <strong><b>var<\/b><\/strong>\u00a0keyword.<\/p>\n<p>var x = 6 + 4<br \/>\nx = 10<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1320\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/5.png\" alt=\"Variable\" width=\"850\" height=\"111\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/5.png 674w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/5-300x39.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/strong><\/p>\n<h2>Blocks<\/h2>\n<p>Surrounding a group of expressions with curly braces combines them into a block. The result of the last expression inside the block becomes the result of the block as a whole.<\/p>\n<p><strong> <img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1321\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/6.png\" alt=\"spark tutorial\" width=\"850\" height=\"134\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/6.png 671w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/6-300x47.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/strong><\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1322\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/7-1.png\" alt=\"apache spark\" width=\"850\" height=\"139\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/7-1.png 666w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/7-1-300x49.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/strong><\/p>\n<h3><strong>Functions<\/strong><\/h3>\n<h2>Functions<\/h2>\n<p>Functions are expressions that take parameters. You can define an anonymous function, one with no name, using arrow syntax, where the parameter list sits to the left of the arrow and the expression using it sits to the right.<\/p>\n<p>On the left of\u00a0=,&gt;\u00a0is a list of parameters. On the right is an expression involving the parameters.<\/p>\n<p>You can also name functions.<\/p>\n<p><strong> <img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1323\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/8.png\" alt=\"function of command\" width=\"850\" height=\"234\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/8.png 673w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/8-300x82.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/strong><\/p>\n<p>Functions can also be given a name.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1324\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/9.png\" alt=\"spark tutorial \" width=\"850\" height=\"271\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/9.png 667w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/9-300x96.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/strong><\/p>\n<p>A function may take multiple parameters.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1325\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/10.png\" alt=\"spark sql\" width=\"850\" height=\"125\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/10.png 665w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/10-300x44.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/strong><\/p>\n<p>Or it can take no parameters at all.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1326\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/11.png\" alt=\"spark dataframe\" width=\"850\" height=\"141\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/11.png 644w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/11-300x50.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/strong><\/p>\n<h2>Methods<\/h2>\n<p>Methods look and behave much like functions, with a few key differences. A method is defined with the <strong><b>def<\/b><\/strong>\u00a0keyword, and its return type is declared after the parameter list, following a colon.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1327\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/12.png\" alt=\"Methods are defined with the\u00a0def\u00a0keyword\" width=\"850\" height=\"144\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/12.png 661w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/12-300x51.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/strong><\/p>\n<p>A method can also take multiple parameter lists, one set of parentheses after another.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1328\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/13.png\" alt=\"Methods can take multiple parameter lists\" width=\"850\" height=\"158\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/13.png 672w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/13-300x56.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/strong><\/p>\n<h3><strong>Classes<\/strong><\/h3>\n<p>You can define classes with the class keyword followed by its name and constructor parameters.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1329\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/14.png\" alt=\"class prwatech\" width=\"850\" height=\"172\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/14.png 658w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/14-300x61.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/strong><\/p>\n<h3><strong>Objects<\/strong><\/h3>\n<p>Objects are single instances of their own definitions. You can think of them as singletons of their own classes.<\/p>\n<p>You can define objects with the\u00a0object\u00a0keyword.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1330\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/15.png\" alt=\"You can define objects with the\u00a0object\u00a0keyword.\" width=\"850\" height=\"482\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/15.png 663w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/15-300x170.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/strong><\/p>\n<h2>Singleton Objects<\/h2>\n<p>Scala classes do not have a static keyword, so a singleton object fills that role instead. Its constructor runs the first time the object is used, it carries all the features of a class, and its constructor cannot take parameters. A singleton object can be imported from anywhere in the program.<\/p>\n<p>Singleton objects are commonly used in a few situations:<\/p>\n<ul>\n<li>When a single instance is needed to coordinate a service.<\/li>\n<li>When one immutable instance can be shared across the program for efficiency.<\/li>\n<li>When an immutable instance is needed for utility functions or constants.<\/li>\n<\/ul>\n<p><strong><u><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1331\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/16.png\" alt=\"utility functions or constants.\" width=\"850\" height=\"408\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/16.png 618w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/16-300x144.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/u><\/strong><\/p>\n<h2>If Else Conditions<\/h2>\n<p>var a = 8<br \/>\nval s = if (a &gt; 1 &amp;&amp; a &lt; 9) 1 else 0<\/p>\n<p>var a = 9<br \/>\nval s = if (a &gt; 1 &amp;&amp; a &lt; 9) 1 else 0<\/p>\n<p><strong><em>\u00a0<img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1363\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/03\/1-24.png\" alt=\"commands var a\" width=\"850\" height=\"384\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/03\/1-24.png 677w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/03\/1-24-300x136.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/em><\/strong><\/p>\n<p><strong><em>\u00a0<\/em><\/strong><\/p>\n<h2>While Loop<\/h2>\n<p>var args = &#8220;PRWATECH&#8221;<br \/>\nvar i = 0<br \/>\nwhile (i &lt; args.length) {<br \/>\nprintln(args(i))<br \/>\ni += 1<br \/>\n}<\/p>\n<p><strong><em>\u00a0<img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1364\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/03\/2-23.png\" alt=\"Command:-var args\" width=\"850\" height=\"473\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/03\/2-23.png 677w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/03\/2-23-300x167.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/em><\/strong><\/p>\n<p><strong><em>\u00a0<\/em><\/strong><\/p>\n<h2>Foreach Loop<\/h2>\n<p>var args = &#8220;SCALA&#8221;<br \/>\nargs.foreach(println)<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1365\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/03\/3-24.png\" alt=\"Command:-var args scala\" width=\"850\" height=\"281\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/03\/3-24.png 680w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/03\/3-24-300x99.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<h2>For Loop<\/h2>\n<p>for (i &lt;- 1 to 5) print(i)<\/p>\n<p>for (i &lt;- 11 to 25) print(i)<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1366\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/03\/4-19.png\" alt=\"commands for scala\" width=\"850\" height=\"251\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/03\/4-19.png 677w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/03\/4-19-300x89.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<h2>Nested For Loop<\/h2>\n<p>for (i &lt;- 1 to 5; j = i) println(20 * i + j)<\/p>\n<p><u><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1367\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/03\/5-19.png\" alt=\"Scala commands \" width=\"850\" height=\"209\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/03\/5-19.png 675w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/03\/5-19-300x74.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/u><\/p>\n<h2><strong>How to write a function<\/strong><\/h2>\n<p>Define the function first, then call it with a value.<\/p>\n<p>def area(radius: Int): Double = { 7.20 * radius * radius }<\/p>\n<p><strong><u><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1368\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/03\/6-20.png\" alt=\"Command:-def area\" width=\"850\" height=\"132\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/03\/6-20.png 675w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/03\/6-20-300x47.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/u><\/strong><\/p>\n<h3><strong>Argument to function<\/strong><\/h3>\n<p>def concatstr(arg1: String, arg2: String = &#8220;Institute&#8221;, arg3: String = &#8220;Prwatech&#8221;) = println(arg1 + arg2 + arg3)<\/p>\n<p>concatstr(&#8220;BigData&#8221;)<br \/>\nconcatstr(&#8220;BigData&#8221;, arg3 = &#8220;spark&#8221;)<br \/>\nconcatstr(&#8220;BigData&#8221;)<\/p>\n<p>Parameters can be given default values, so a caller only needs to supply the ones they want to override, such as arg3 in the second call above.<\/p>\n<p><em>\u00a0<img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1369\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/03\/7-18.png\" alt=\"Command:-def concatstr\" width=\"850\" height=\"310\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/03\/7-18.png 682w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/03\/7-18-300x110.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/em><em>\u00a0<\/em><em>\u00a0<\/em><\/p>\n<h2>Procedures: Functions Without a Return Value<\/h2>\n<p>Scala has a special kind of function that returns no value. If a function is written without a preceding equals sign, its return type becomes <strong><b>Unit<\/b><\/strong>, and a function like this is called a procedure. Procedures never return a value in Scala.<\/p>\n<p>def rect_area(length: Float, breadth: Float) { val area = length * breadth; println(area) }<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1370\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/03\/8-14.png\" alt=\"Scala Procedure\" width=\"850\" height=\"223\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/03\/8-14.png 680w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/03\/8-14-300x79.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/p>\n<h2>Scala Collections<\/h2>\n<p>Scala has a rich collections library. The most commonly used are Array, ArrayBuffer, Map, Tuple, and List.<\/p>\n<h3>Array<\/h3>\n<p>An array is a collection of elements or records of the same type.<\/p>\n<p>var i = Array(&#8220;Hello&#8221;, &#8220;Prwatech&#8221;)<br \/>\nprint(i(0))<br \/>\nprint(i(1))<\/p>\n<p><em>\u00a0<img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1371\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/03\/9-12.png\" alt=\"scala var i\" width=\"850\" height=\"195\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/03\/9-12.png 679w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/03\/9-12-300x69.png 300w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/em><\/p>\n<h3>Array Buffers<\/h3>\n<p>An ArrayBuffer is like an array, but it can grow or shrink in size, unlike a fixed size Array. You bring it in with an import statement before using it.<\/p>\n<p>import scala.collection.mutable.ArrayBuffer<\/p>\n<p>var buffer = ArrayBuffer(&#8220;Hello&#8221;, &#8220;Prwatech&#8221;)<br \/>\nbuffer += &#8220;Spark&#8221;<br \/>\nprint(buffer)<\/p>\n<h3>Maps<\/h3>\n<p>A <strong><b>Map<\/b><\/strong>\u00a0is a collection of key and value pairs, similar to a Java HashMap or a Python dictionary. You look up a value by its key rather than by position.<\/p>\n<p>var colors = Map(&#8220;red&#8221; -&gt; &#8220;#FF0000&#8221;, &#8220;yellow&#8221; -&gt; &#8220;#FFFF00&#8221;)<br \/>\nprint(colors(&#8220;yellow&#8221;))<\/p>\n<h3>Tuples<\/h3>\n<p>A <strong><b>Tuple<\/b><\/strong>\u00a0holds a fixed group of values that can be of different types in the same tuple, unlike an Array, and it is immutable. You access each element with an underscore followed by its position, starting from 1.<\/p>\n<p>var t = (15, &#8220;Prwatech&#8221;, true)<br \/>\nprint(t._1)<br \/>\nprint(t._2)<\/p>\n<h3>Lists<\/h3>\n<p>A <strong><b>List<\/b><\/strong>\u00a0is an immutable, ordered collection of elements of the same type, similar to an Array, but a List cannot be resized once created.<\/p>\n<p>var l = List(&#8220;Hello&#8221;, &#8220;Prwatech&#8221;, &#8220;Spark&#8221;)<br \/>\nprint(l(0))<\/p>\n<h2>Common Mistakes to Avoid<\/h2>\n<ul>\n<li>Forgetting that a procedure has no equals sign. A method written without an equals sign before its braces returns Unit, meaning no value, even if the body computes something. Add the equals sign back if you actually need the result returned.<\/li>\n<li>Reassigning a val. A val is meant to stay fixed. If your logic genuinely needs to change a value later, use var instead of working around an immutable val.<\/li>\n<li>Confusing a function with a method. They look similar, but a method is defined with def and can carry multiple parameter lists, while a function is more often used as a value you can pass around, such as an anonymous function.<\/li>\n<li>Reaching for a mutable ArrayBuffer out of habit. If the size of your collection will not change, a plain List or Array is simpler and safer than an ArrayBuffer.<\/li>\n<\/ul>\n<p>That covers the core Scala commands used in Spark development, from expressions and functions through classes, loops, and collections. To go further, explore <a href=\"https:\/\/prwatech.in\/apache-spark-training-institute-in-bangalore\/\"><strong><b>Prwatech&#8217;s Apache Spark training<\/b><\/strong><\/a> program, which includes placement assistance.<\/p>\n<p><em>\u00a0<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Scala Commands for Spark This is a working reference to the core Scala language commands every Spark developer runs into early on, expressions, values, functions, classes, loops, and collections, each with a short explanation and example. Expressions An expression is simply a computable statement. Typed into the Scala shell, it evaluates immediately and returns a [&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,1693],"tags":[299,868,869,865,866,864,298,867],"class_list":["post-1315","post","type-post","status-publish","format-standard","hentry","category-apache-spark","category-apache-spark-scala","tag-apache-spark-basic-scala-commands","tag-apache-spark-scala","tag-basic-scala-commands","tag-commands-scala","tag-scala-command-in-spark","tag-scala-commands","tag-scala-commands-for-spark","tag-spark-scala-commands"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Scala Commands for Spark: A Beginner Reference Guide<\/title>\n<meta name=\"description\" content=\"A practical reference to core Scala commands for Spark, covering values, functions, loops, classes, and collections, with clear examples. A step by step process\" \/>\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-scala\/scala-commands-for-spark\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Scala Commands for Spark: A Beginner Reference Guide\" \/>\n<meta property=\"og:description\" content=\"A practical reference to core Scala commands for Spark, covering values, functions, loops, classes, and collections, with clear examples. A step by step process\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prwatech.in\/blog\/apache-spark\/apache-spark-scala\/scala-commands-for-spark\/\" \/>\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-12T12:11:18+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-09-13T13:55:02+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/1.png\" \/>\n\t<meta property=\"og:image:width\" content=\"666\" \/>\n\t<meta property=\"og:image:height\" content=\"100\" \/>\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=\"10 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-scala\/scala-commands-for-spark\/\",\"url\":\"https:\/\/prwatech.in\/blog\/apache-spark\/apache-spark-scala\/scala-commands-for-spark\/\",\"name\":\"Scala Commands for Spark: A Beginner Reference Guide\",\"isPartOf\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prwatech.in\/blog\/apache-spark\/apache-spark-scala\/scala-commands-for-spark\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prwatech.in\/blog\/apache-spark\/apache-spark-scala\/scala-commands-for-spark\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/1.png\",\"datePublished\":\"2026-09-12T12:11:18+00:00\",\"dateModified\":\"2026-09-13T13:55:02+00:00\",\"author\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\"},\"description\":\"A practical reference to core Scala commands for Spark, covering values, functions, loops, classes, and collections, with clear examples. A step by step process\",\"breadcrumb\":{\"@id\":\"https:\/\/prwatech.in\/blog\/apache-spark\/apache-spark-scala\/scala-commands-for-spark\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prwatech.in\/blog\/apache-spark\/apache-spark-scala\/scala-commands-for-spark\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prwatech.in\/blog\/apache-spark\/apache-spark-scala\/scala-commands-for-spark\/#primaryimage\",\"url\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/1.png\",\"contentUrl\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/1.png\",\"width\":666,\"height\":100},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prwatech.in\/blog\/apache-spark\/apache-spark-scala\/scala-commands-for-spark\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/prwatech.in\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Scala commands for Spark\"}]},{\"@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":"Scala Commands for Spark: A Beginner Reference Guide","description":"A practical reference to core Scala commands for Spark, covering values, functions, loops, classes, and collections, with clear examples. A step by step process","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-scala\/scala-commands-for-spark\/","og_locale":"en_US","og_type":"article","og_title":"Scala Commands for Spark: A Beginner Reference Guide","og_description":"A practical reference to core Scala commands for Spark, covering values, functions, loops, classes, and collections, with clear examples. A step by step process","og_url":"https:\/\/prwatech.in\/blog\/apache-spark\/apache-spark-scala\/scala-commands-for-spark\/","og_site_name":"Prwatech","article_publisher":"https:\/\/www.facebook.com\/prwatech.in\/","article_published_time":"2026-09-12T12:11:18+00:00","article_modified_time":"2026-09-13T13:55:02+00:00","og_image":[{"width":666,"height":100,"url":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/1.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":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/prwatech.in\/blog\/apache-spark\/apache-spark-scala\/scala-commands-for-spark\/","url":"https:\/\/prwatech.in\/blog\/apache-spark\/apache-spark-scala\/scala-commands-for-spark\/","name":"Scala Commands for Spark: A Beginner Reference Guide","isPartOf":{"@id":"https:\/\/prwatech.in\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prwatech.in\/blog\/apache-spark\/apache-spark-scala\/scala-commands-for-spark\/#primaryimage"},"image":{"@id":"https:\/\/prwatech.in\/blog\/apache-spark\/apache-spark-scala\/scala-commands-for-spark\/#primaryimage"},"thumbnailUrl":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/1.png","datePublished":"2026-09-12T12:11:18+00:00","dateModified":"2026-09-13T13:55:02+00:00","author":{"@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3"},"description":"A practical reference to core Scala commands for Spark, covering values, functions, loops, classes, and collections, with clear examples. A step by step process","breadcrumb":{"@id":"https:\/\/prwatech.in\/blog\/apache-spark\/apache-spark-scala\/scala-commands-for-spark\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prwatech.in\/blog\/apache-spark\/apache-spark-scala\/scala-commands-for-spark\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prwatech.in\/blog\/apache-spark\/apache-spark-scala\/scala-commands-for-spark\/#primaryimage","url":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/1.png","contentUrl":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2019\/04\/1.png","width":666,"height":100},{"@type":"BreadcrumbList","@id":"https:\/\/prwatech.in\/blog\/apache-spark\/apache-spark-scala\/scala-commands-for-spark\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/prwatech.in\/blog\/"},{"@type":"ListItem","position":2,"name":"Scala commands for Spark"}]},{"@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\/1315","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=1315"}],"version-history":[{"count":15,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/1315\/revisions"}],"predecessor-version":[{"id":11747,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/1315\/revisions\/11747"}],"wp:attachment":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/media?parent=1315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/categories?post=1315"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/tags?post=1315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}