最近由于要使用Spark做POC,在本地搭建了相应的开发环境,Spark本身是使用Scala语言编写的,当然也可以使用java来开发spark项目,但使用scala语言来开发更加简洁,本文在IDEA开发工具中使用Maven来创建Scala工程
1:安装Java SDK,Scala,及IDEA 集成开发环境
JDK下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
Scala下载地址:http://www.scala-lang.org/download/all.html
Intellij IDEA下载地址:https://www.jetbrains.com/idea/download/download-thanks.html
以下是我测试环境中 java 及 scala的版本:
IDEA使用的是社区版本:IDEA 2017.3.5 (Community Edition)
2:Spark的安装
下载spark:https://spark.apache.org/downloads.html,选择相应的版本,这里选择了2.2.1版本 spark-2.2.1-bin-hadoop2.7,并解压到指定的目录,添加环境变量:
SPARK_HOME:D:Applicationspark-2.2.1-bin-hadoop2.7
并添加以下路径到Path变量中:%SPARK_HOME%in
这样可以在命令行中启动命令:spark-shell
3:Scala插件的安装
在IDEA中选择 Configure -> Plugins,搜索scala,点击安装:
4:使用Maven来创建Scala 工程
点击 Next,设置好GroupId及ArtifactId,一路点击到结束,期间根据实际情况可以修改Project name及代码的路径,最后生成Scala架构的代码目录及默认的pom.xml文件,在这里需要注意一点,Scala的本机安装的版本和Scala插件的版本不一致的问题,查看生成的默认pom.xml文件如下,发现版本使用的是2.7.0,而前面本机安装的是2.11.8 ,
这样创建的项目运行时会失败,提示 "Error: Could not find or load main class ..."
查看前面安装的Scala版本,相应的修改pom文件中的版本号为2.11.8,这就出现了另外一个问题,从Scala2.9以后,已经废弃了Application类,而是使用了新的类App,于是修改代码
object App extends App {
println( "Hello World!" )
}
运行项目,单元测试报错,提示scala signature 版本错误,先删除MySpec.scala文件
修改pom文件,
5:添加对spark的支持,本地测试文件单词统计
修改pom.xml
添加属性 spark 版本,为本机安装的spark版本
<spark.version>2.2.1</spark.version>
添加依赖库
<dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-core_2.11</artifactId> <version>${spark.version}</version> </dependency>
完整的pom.xml如下所示:
<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>mytest</groupId> <artifactId>myproject</artifactId> <version>1.0-SNAPSHOT</version> <inceptionYear>2008</inceptionYear> <properties> <scala.version>2.11.8</scala.version> <spark.version>2.2.1</spark.version> </properties> <repositories> <repository> <id>scala-tools.org</id> <name>Scala-Tools Maven2 Repository</name> <url>http://scala-tools.org/repo-releases</url> </repository> </repositories> <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.apache.spark</groupId> <artifactId>spark-core_2.11</artifactId> <version>${spark.version}</version> </dependency> <dependency> <groupId>org.scala-lang</groupId> <artifactId>scala-library</artifactId> <version>${scala.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>
修改scala代码,引入spark库:
package mytest import org.apache.spark._ object wordcount { def main(args: Array[String]) { var masterUrl = "local[1]" var inputPath = "D:\temp\mytext.txt" var outputPath = "D:\temp\output" 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) } }
再次运行工程,可以在窗口中看到已经成功运行spark,对输入的文本进行词汇统计,此时,spark运行的是本地模式,使用的是本地文件作为输入
在输出目录,可以看到程序运行的结果