您好,登錄后才能下訂單哦!
怎么在IDEA中使用Maven創(chuàng)建一個Scala項(xiàng)目?相信很多沒有經(jīng)驗(yàn)的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
第一步:IntelliJ IDEA下安裝 Scala 插件
安裝完 Scala 插件完成
第二步:Maven 下 Scala 下的項(xiàng)目創(chuàng)建
正常創(chuàng)建 Maven 項(xiàng)目(不會的看另一篇 Maven 配置)
第三步:Scala 版本的下載及配置
通過Spark官網(wǎng)下載頁面http://spark.apache.org/downloads.html 可知“Note: Starting version 2.0, Spark is built with Scala 2.11 by default.”,建議下載Spark2.2對應(yīng)的 Scala 2.11。
登錄Scala官網(wǎng)http://www.scala-lang.org/,單擊download按鈕,然后再“Other Releases”標(biāo)題下找到“下載2.11.0
根據(jù)自己的系統(tǒng)下載相應(yīng)的版本
接下來就是配置Scala 的環(huán)境變量(跟 jdk 的配置方法一樣)
輸入 Scala -version 查看是否配置成功 會顯示 Scala code runner version 2.11.0 – Copyright 2002-2013, LAMP/EPFL
選擇自己安裝 Scala 的路徑
第四步:編寫 Scala 程序
將其他的代碼刪除,不然在編輯的時(shí)候會報(bào)錯
配置 pom.xml文件
在里面添加一個 Spark
<properties> <scala.version>2.11.0</scala.version> <spark.version>2.2.1</spark.version> </properties> <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-core_2.11</artifactId> <version>${spark.version}</version> </dependency>
具體的 pom.xml 內(nèi)容
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.spark</groupId> <artifactId>Spark</artifactId> <version>1.0-SNAPSHOT</version> <inceptionYear>2008</inceptionYear> <properties> <scala.version>2.11.0</scala.version> <spark.version>2.2.1</spark.version> </properties> <pluginRepositories> <pluginRepository> <id>scala-tools.org</id> <name>Scala-Tools Maven2 Repository</name> <url>http://scala-tools.org/repo-releases</url> </pluginRepository> </pluginRepositories> <dependencies> <dependency> <groupId>org.scala-lang</groupId> <artifactId>scala-library</artifactId> <version>${scala.version}</version> </dependency> <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-core_2.11</artifactId> <version>${spark.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.4</version> <scope>test</scope> </dependency> <dependency> <groupId>org.specs</groupId> <artifactId>specs</artifactId> <version>1.2.5</version> <scope>test</scope> </dependency> </dependencies> <build> <sourceDirectory>src/main/scala</sourceDirectory> <testSourceDirectory>src/test/scala</testSourceDirectory> <plugins> <plugin> <groupId>org.scala-tools</groupId> <artifactId>maven-scala-plugin</artifactId> <executions> <execution> <goals> <goal>compile</goal> <goal>testCompile</goal> </goals> </execution> </executions> <configuration> <scalaVersion>${scala.version}</scalaVersion> <args> <arg>-target:jvm-1.5</arg> </args> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-eclipse-plugin</artifactId> <configuration> <downloadSources>true</downloadSources> <buildcommands> <buildcommand>ch.epfl.lamp.sdt.core.scalabuilder</buildcommand> </buildcommands> <additionalProjectnatures> <projectnature>ch.epfl.lamp.sdt.core.scalanature</projectnature> </additionalProjectnatures> <classpathContainers> <classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER</classpathContainer> <classpathContainer>ch.epfl.lamp.sdt.launching.SCALA_CONTAINER</classpathContainer> </classpathContainers> </configuration> </plugin> </plugins> </build> <reporting> <plugins> <plugin> <groupId>org.scala-tools</groupId> <artifactId>maven-scala-plugin</artifactId> <configuration> <scalaVersion>${scala.version}</scalaVersion> </configuration> </plugin> </plugins> </reporting> </project>
編寫 WordCount 文件
package cn.spark import org.apache.spark.{SparkConf, SparkContext} /** * Created by hubo on 2018/1/13 */ object WordCount { def main(args: Array[String]) { var masterUrl = "local" var inputPath = "/Users/huwenbo/Desktop/a.txt" var outputPath = "/Users/huwenbo/Desktop/out" if (args.length == 1) { masterUrl = args(0) } else if (args.length == 3) { masterUrl = args(0) inputPath = args(1) outputPath = args(2) } println(s"masterUrl:$masterUrl, inputPath: $inputPath, outputPath: $outputPath") val sparkConf = new SparkConf().setMaster(masterUrl).setAppName("WordCount") val sc = new SparkContext(sparkConf) val rowRdd = sc.textFile(inputPath) val resultRdd = rowRdd.flatMap(line => line.split("\\s+")) .map(word => (word, 1)).reduceByKey(_ + _) resultRdd.saveAsTextFile(outputPath) } }
看完上述內(nèi)容,你們掌握怎么在IDEA中使用Maven創(chuàng)建一個Scala項(xiàng)目的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。