转载请注明作者,知识分享,开心你我!
现在网上很多spark开发环境的搭建都是基于idea的,个人用习惯了eclipse,还是用eclipse来搭建开发环境把。准备工作,下载Scala IDE for Eclipse 版本:http://scala-ide.org/
该方法和Java 工程类似。
右击 --> Scala --> set the Scala Installation
也可以
右击工程--> Properties --> Scala Compiler --> Use project Setting 中选择spark对应的scala版本,此处选择Lastest2.10 bundle
目前社区的项目基本都采用了Maven方式管理,所以Maven开发方式学习还是很有必要的。
要创建spark工程,我们先学习下maven创建scala工程吧!
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.wankun.scala</groupId> <artifactId>test3</artifactId> <version>1.0</version> <packaging>jar</packaging> <name>test3</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven-compiler-plugin.version>3.1</maven-compiler-plugin.version> <org.scala-tools.version>2.3.2</org.scala-tools.version> <scala.library.version>2.10.4</scala.library.version> </properties> <dependencies> <dependency> <groupId>org.scala-lang</groupId> <artifactId>scala-library</artifactId> <version>${scala.library.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> </dependencies> <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> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>${maven-compiler-plugin.version}</version> <configuration> <source>1.6</source> <target>1.6</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.scala-tools</groupId> <artifactId>maven-scala-plugin</artifactId> <executions> <execution> <goals> <goal>compile</goal> <goal>testCompile</goal> </goals> </execution> </executions> <configuration> <sourceDir>src/main/scala</sourceDir> <jvmArgs> <jvmArg>-Xms64m</jvmArg> <jvmArg>-Xmx1024m</jvmArg> </jvmArgs> </configuration> </plugin> </plugins> </build> </project>
pom主要修改的几个地方:
其他的maven插件可以在需要的时候再添加。
必要的时候可以再增加引入scala的一些其他类库
<dependency> <groupId>org.scala-lang</groupId> <artifactId>scala-compiler</artifactId> <version>${scala.version}</version> </dependency> <dependency> <groupId>org.scala-lang</groupId> <artifactId>scala-reflect</artifactId> <version>${scala.version}</version> </dependency> <dependency> <groupId>org.scala-lang</groupId> <artifactId>jline</artifactId> <version>${scala.version}</version> </dependency> <dependency> <groupId>org.scala-lang</groupId> <artifactId>scala-library</artifactId> <version>${scala.version}</version> </dependency> <dependency> <groupId>org.scala-lang</groupId> <artifactId>scala-actors</artifactId> <version>${scala.version}</version> </dependency> <dependency> <groupId>org.scala-lang</groupId> <artifactId>scalap</artifactId> <version>${scala.version}</version> </dependency>
<repository> <id>cloudera</id> <url>https://repository.cloudera.com/cloudera/cloudera-repos</url> </repository>
<dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-core_2.10</artifactId> <version>${spark.version}</version> </dependency>
注意:测试时发现 scala.math.random 无法正常引入,因为我写入了自己packagecom.wankun,这样导致在import时,提示无法importcom.wankun.scala.math,应该是scala中的包机制和Java中的包机制不同导致的把。