{"id":150,"date":"2022-06-01T19:23:10","date_gmt":"2022-06-01T19:23:10","guid":{"rendered":"https:\/\/kindsonthegenius.com\/apache-spark\/?p=150"},"modified":"2022-06-01T19:23:10","modified_gmt":"2022-06-01T19:23:10","slug":"spark-setup-in-intellij-with-scala","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/apache-spark\/spark-setup-in-intellij-with-scala\/","title":{"rendered":"Spark &#8211; Setup in IntelliJ With Scala"},"content":{"rendered":"<p>In this tutorial, you will learn how to setup Spark to run in IntelliJ with Scala. Then we would create a simple HelloWorld application in IntelliJ.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong>Create a Spark Project and Add Dependencies<\/strong><\/h4>\n<p><strong>Step 1<\/strong> &#8211; First you need to setup Scala with IntelliJ. <a href=\"https:\/\/www.kindsonthegenius.com\/scala\/scala-ide-installation-and-setup\/\" target=\"_blank\" rel=\"noopener\">See the steps here<\/a>.<\/p>\n<p><strong>Step 2<\/strong> &#8211; Create a new Scala Project in IntelliJ<\/p>\n<p><strong>Step 3<\/strong> &#8211; Add Spark dependencies:<\/p>\n<p>Open the build.sbt file and add the Spark Core and Spark SQL and Streaming dependencies.<\/p>\n<p>the complete content of my build.sbt file is shown below<\/p>\n<pre>name := \"FirstProg\"\r\n\r\nversion := \"0.1\"\r\n\r\nscalaVersion := \"2.13.8\"\r\n\r\nlibraryDependencies ++= Seq(\r\n  \"org.apache.spark\" %% \"spark-core\" % \"3.2.1\",\r\n  \"org.apache.spark\" %% \"spark-sql\" % \"3.2.1\",\r\n  \"org.apache.spark\" %% \"spark-streaming\" % \"3.2.1\",\r\n)<\/pre>\n<p>You&#8217;ll need to refresh the sbt to download the dependencies.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong>Create an Application<\/strong><\/h4>\n<p>Create a new Scala Class in IntelliJ. Choose the type to be Object.<\/p>\n<p>The content of this file is as follows<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">package<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">org.kindsonthegenius<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">org.apache.spark.sql.SparkSession<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">object<\/span> <span style=\"color: #bb0066; font-weight: bold;\">SparkSessionTest<\/span> <span style=\"color: #008800; font-weight: bold;\">extends<\/span> <span style=\"color: #bb0066; font-weight: bold;\">App<\/span><span style=\"color: #333333;\">{<\/span>\r\n  <span style=\"color: #008800; font-weight: bold;\">val<\/span> spark <span style=\"color: #008800; font-weight: bold;\">=<\/span> <span style=\"color: #bb0066; font-weight: bold;\">SparkSession<\/span><span style=\"color: #333333;\">.<\/span>builder<span style=\"color: #333333;\">()<\/span>\r\n    <span style=\"color: #333333;\">.<\/span>master<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"local[1]\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n    <span style=\"color: #333333;\">.<\/span>appName<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Spark Tutorial by Kindson\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n    <span style=\"color: #333333;\">.<\/span>getOrCreate<span style=\"color: #333333;\">()<\/span>\r\n\r\n  println<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"************** Your First SparkContext ***************\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n  println<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"App Name - \"<\/span> <span style=\"color: #333333;\">+<\/span> spark<span style=\"color: #333333;\">.<\/span>sparkContext<span style=\"color: #333333;\">.<\/span>appName<span style=\"color: #333333;\">)<\/span>\r\n  println<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Deploy Mode - \"<\/span> <span style=\"color: #333333;\">+<\/span> spark<span style=\"color: #333333;\">.<\/span>sparkContext<span style=\"color: #333333;\">.<\/span>deployMode<span style=\"color: #333333;\">)<\/span>\r\n  println<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Master - \"<\/span> <span style=\"color: #333333;\">+<\/span>spark<span style=\"color: #333333;\">.<\/span>sparkContext<span style=\"color: #333333;\">.<\/span>master<span style=\"color: #333333;\">)<\/span>\r\n\r\n  <span style=\"color: #008800; font-weight: bold;\">val<\/span> sparkSession2 <span style=\"color: #008800; font-weight: bold;\">=<\/span> <span style=\"color: #bb0066; font-weight: bold;\">SparkSession<\/span>\r\n    <span style=\"color: #333333;\">.<\/span>builder<span style=\"color: #333333;\">.<\/span>master<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"local[1]\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n    <span style=\"color: #333333;\">.<\/span>appName<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Spark Tutorial by Kindson\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n    <span style=\"color: #333333;\">.<\/span>getOrCreate\r\n\r\n  println<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"************* Your Second Spark Context ***************\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n  println<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"APP Name - \"<\/span> <span style=\"color: #333333;\">+<\/span> sparkSession2<span style=\"color: #333333;\">.<\/span>sparkContext<span style=\"color: #333333;\">.<\/span>appName<span style=\"color: #333333;\">)<\/span>\r\n  println<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Deploy Mode - \"<\/span> <span style=\"color: #333333;\">+<\/span> sparkSession2<span style=\"color: #333333;\">.<\/span>sparkContext<span style=\"color: #333333;\">.<\/span>deployMode<span style=\"color: #333333;\">)<\/span>\r\n  println<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Master - \"<\/span> <span style=\"color: #333333;\">+<\/span> sparkSession2<span style=\"color: #333333;\">.<\/span>sparkContext<span style=\"color: #333333;\">.<\/span>master<span style=\"color: #333333;\">)<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>You can now run the program and you will have the output shown below<\/p>\n<figure id=\"attachment_157\" aria-describedby=\"caption-attachment-157\" style=\"width: 1024px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-large wp-image-157\" src=\"https:\/\/kindsonthegenius.com\/apache-spark\/wp-content\/uploads\/sites\/13\/2022\/06\/Screenshot-2022-06-01-at-21.21.48-1024x576.png\" alt=\"Output of Spark Program\" width=\"1024\" height=\"576\" srcset=\"https:\/\/www.kindsonthegenius.com\/apache-spark\/wp-content\/uploads\/sites\/13\/2022\/06\/Screenshot-2022-06-01-at-21.21.48-1024x576.png 1024w, https:\/\/www.kindsonthegenius.com\/apache-spark\/wp-content\/uploads\/sites\/13\/2022\/06\/Screenshot-2022-06-01-at-21.21.48-300x169.png 300w, https:\/\/www.kindsonthegenius.com\/apache-spark\/wp-content\/uploads\/sites\/13\/2022\/06\/Screenshot-2022-06-01-at-21.21.48-768x432.png 768w, https:\/\/www.kindsonthegenius.com\/apache-spark\/wp-content\/uploads\/sites\/13\/2022\/06\/Screenshot-2022-06-01-at-21.21.48-1536x864.png 1536w, https:\/\/www.kindsonthegenius.com\/apache-spark\/wp-content\/uploads\/sites\/13\/2022\/06\/Screenshot-2022-06-01-at-21.21.48.png 1920w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption id=\"caption-attachment-157\" class=\"wp-caption-text\">Output of Spark Program<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<p>I recommend you watch the video if you have any challenges. You can also leave me a comment below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to setup Spark to run in IntelliJ with Scala. Then we would create a simple HelloWorld application in &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,3],"tags":[36],"class_list":["post-150","post","type-post","status-publish","format-standard","hentry","category-pyspark","category-spark","tag-sparkcontext"],"_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/apache-spark\/wp-json\/wp\/v2\/posts\/150","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kindsonthegenius.com\/apache-spark\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kindsonthegenius.com\/apache-spark\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/apache-spark\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/apache-spark\/wp-json\/wp\/v2\/comments?post=150"}],"version-history":[{"count":2,"href":"https:\/\/www.kindsonthegenius.com\/apache-spark\/wp-json\/wp\/v2\/posts\/150\/revisions"}],"predecessor-version":[{"id":158,"href":"https:\/\/www.kindsonthegenius.com\/apache-spark\/wp-json\/wp\/v2\/posts\/150\/revisions\/158"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/apache-spark\/wp-json\/wp\/v2\/media?parent=150"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/apache-spark\/wp-json\/wp\/v2\/categories?post=150"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/apache-spark\/wp-json\/wp\/v2\/tags?post=150"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}