重启后,点击Create New Project:

Project SDK选择jdk安装目录,建议开发环境中的jdk版本与Spark集群上的jdk版本保持一致。点击左侧的Maven,勾选Create from archetype,选择org.scala-tools.archetypes:scala-archetype-simple:

点击Next后,可根据需求自行填写GroupId,ArtifactId和Version:

点击Next后,如果本机没有安装maven会报错,请保证之前已经安装maven:

点击Next后,输入文件名,完成New Project的最后一步:

点击Finish后,maven会自动生成pom.xml和下载依赖包。我们需要修改pom.xml中scala的版本:
1 <properties> 2 <scala.version>2.10.4</scala.version> 3 </properties>
在<dependencies></dependencies>之间添加配置:
1 <!-- Spark --> 2 <dependency> 3 <groupId>org.apache.spark</groupId> 4 <artifactId>spark-core_2.10</artifactId> 5 <version>1.1.0</version> 6 </dependency> 7 8 <!-- HDFS --> 9 <dependency> 10 <groupId>org.apache.Hadoop</groupId> 11 <artifactId>hadoop-client</artifactId> 12 <version>2.4.0</version> 13 </dependency>
在<build><plugins></plugins></build>之间添加配置:
1 <plugin> 2 <groupId>org.apache.maven.plugins</groupId> 3 <artifactId>maven-shade-plugin</artifactId> 4 <version>2.2</version> 5 <executions> 6 <execution> 7 <phase>package</phase> 8 <goals> 9 <goal>shade</goal> 10 </goals> 11 <configuration> 12 <filters> 13 <filter> 14 <artifact>*:*</artifact> 15 <excludes> 16 <exclude>META-INF/*SF</exclude> 17 <exclude>META-INF/*.DSA</exclude> 18 <exclude>META-INF/*.RSA</exclude> 19 </excludes> 20 </filter> 21 </filters> 22 <transformers> 23 <transformer 24 implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> 25 <mainClass>mark.lin.App</mainClass> // 记得修改成你的mainClass 26 </transformer> 27 <transformer 28 implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> 29 <resource>reference.conf</resource> 30 </transformer> 31 </transformers> 32 <shadedArtifactAttached>true</shadedArtifactAttached> 33 <shadedClassifierName>executable</shadedClassifierName> 34 </configuration> 35 </execution> 36 </executions> 37 </plugin>
Spark的开发环境至此搭建完成。One more thing,wordcount的示例代码:
1 package mark.lin //别忘了修改package 2 3 import org.apache.spark.{SparkConf, SparkContext} 4 import org.apache.spark.SparkContext._ 5 6 import scala.collection.mutable.ListBuffer 7 8 /** 9 * Hello world! 10 * 11 */ 12 object App{ 13 def main(args: Array[String]) { 14 if (args.length != 1) { 15 println("Usage: java -jar code.jar dependencies.jar") 16 System.exit(0) 17 } 18 val jars = ListBuffer[String]() 19 args(0).split(",").map(jars += _) 20 21 val conf = new SparkConf() 22 conf.setMaster("spark://localhost:7077").setAppName("wordcount").set("spark.executor.memory", "128m").setJars(jars) 23 24 val sc = new SparkContext(conf) 25 26 val file = sc.textFile("hdfs://localhost:9000/hduser/wordcount/input/input.csv") 27 val count = file.flatMap(line => line.split(" ")).map(word => (word, 1)).reduceByKey(_+_) 28 println(count) 29 count.saveAsTextFile("hdfs://localhost:9000/hduser/wordcount/output/") 30 sc.stop() 31 } 32 }
7. 编译&运行
使用maven编译源代码。点击左下角,点击右侧package,点击绿色三角形,开始编译。

在target目录下,可以看到maven生成的jar包。其中,hellworld-1.0-SNAPSHOT-executable.jar是我们需要放到Spark集群上运行的。
在运行jar包之前,保证hadoop和Spark处于运行状态:

将jar包拷贝到Ubuntu的本地文件系统上,输入以下命令运行jar包:
1 java -jar helloworld-1.0-SNAPSHOT-executable.jar helloworld-1.0-SNAPSHOT-executable.jar
在浏览器中输入地址http://localhost:8080/可以查看任务运行情况:

8. Q&A
Q:在Spark集群上运行jar包,抛出异常“No FileSystem for scheme: hdfs”:

A:这是由于hadoop-common-2.4.0.jar中的core-default.xml缺少hfds的相关配置属性引起的异常。在maven仓库目录下找到hadoop-common-2.4.0.jar,以rar的打开方式打开:

将core-default.xml拖出,并添加配置:
1 <property> 2 <name>fs.hdfs.impl</name> 3 <value>org.apache.hadoop.hdfs.DistributedFileSystem</value> 4 <description>The FileSystem for hdfs: uris.</description> 5 </property>
再将修改后的core-default.xml替换hadoop-common-2.4.0.jar中的core-default.xml,重新编译生成jar包。
Q:在Spark集群上运行jar包,抛出异常“Failed on local exception”:

A:检查你的代码,一般是由于hdfs路径错误引起。
Q:在Spark集群上运行jar包,重复提示“Connecting to master spark”:

A:检查你的代码,一般是由于setMaster路径错误引起。
Q:在Spark集群上运行jar包,重复提示“Initial job has not accepted any resource; check your cluster UI to ensure that workers are registered and have sufficient memory”:

A:检查你的代码,一般是由于内存设置不合理引起。此外,还需要检查Spark安装目录下的conf/spark-env.sh对worker内存的设置。
Q:maven报错:error: org.specs.Specification does not have a constructor

A: 删除test目录下的文件,重新编译。
9. 参考资料
[1] Spark Documentation from Apache. [Link]
Spark 的详细介绍:请点这里
Spark 的下载地址:请点这里
本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-01/127856.htm

