使用Eclipse编写Spark应用程序(Scala+Maven) scala ide for eclipse

对Scala代码进行打包编译时,可以采用Maven,也可以采用sbt,相对而言,业界更多使用sbt。本教程介绍如何在 Ubuntu中使用 Eclipse 来开发 scala 程序(使用Maven工具),在Spark 2.1.0,scala 2.11.8 下验证通过。使用 Eclipse,我们可以直接运行代码,省去许多繁琐的命令。(相关文章:如何在 Ubuntu中使用 Eclipse 来开发 scala 程序(使用sbt工具)

 

一.运行环境介绍

Scala ide for eclipse
    ubuntu 16.04
    spark 2.1.0
    scala 2.11

二.安装必备软件

Spark和scala安装方法可以参考,Spark安装和使用 ,Scala安装,
eclipse可以安装scala ide for eclipse,这样可以省下很多工夫去不用安装maven和scala类似的相关插件,进入scala ide for eclipse,图如下所示,选择Linux GTK 64 bit,

接下来解压安装包到/usr/local下,并测试运行eclipse

 
  1. sudo tar -zxvf ~/下载/scala-SDK-4.5.0-vfinal-2.11-linux.gtk.x86_64.tar.gz -C /usr/local
  2. cd /usr/local
  3. ./eclipse/eclipse

Shell

出现如下界面说明运行成功了。
 
如果各位没有安装该版本的eclipse,很可能会花费不必要的麻烦去安装相关的插件,下面笔者也介绍下安装maven和scala插件的方法,(ps:安装了scala ide for eclipse的可以直接跳过该步骤)
安装maven插件和scala插件
先进入eclipse后,点击Help–>Install New Software,在出现的install界面上输入maven插件的网址,http://download.eclipse.org/releases/neno (注意:末尾的neno是eclipse对应的版本名称,如果你用的不是neno版本,请换成自己用的eclipse版本号),然后再在下面的文本框输入maven,点击select all,点击next,接下来一直点击next或finish就可以了。
 

安装scala插件同样,点击Help–>Install New Software,在出现的install界面上输入scala插件的网址, http://download.scala-ide.org/sdk/helium/e38/scala211/stable/site ,摁下回车键,接着select all,然后同样一直next或finish就可以了。

在/usr/local/spark下建立目录/mycode/wordcount,然后在“/usr/local/spark/mycode/wordcount”目录下新建一个包含了一些语句的文本文件word.txt,命令如下:

 
  1. cd /usr/local/spark
  2. mkdir mycode
  3. cd mycode
  4. mkdir wordcount
  5. cd wordcount
  6. vim word.txt

Shell

你可以在文本文件中随意输入一些单词,用空格隔开,我们会编写Spark程序对该文件进行单词词频统计。然后,按键盘Esc键退出vim编辑状态,输入“:wq”保存文件并退出vim编辑器。

三.创建maven工程

打开eclipse后,在工程栏右键New–>Project,在出现的界面上双击maven文件,选择maven project,先点击右下角的Add Archetype,第一个Archetype Group id 选择net.alchim31.maven,第二个Archetype Artifact id选择scala-archetype-simple,第三个选择version是1.6,点击OK,在Catalog里选择All Catalogs,在filter里输入scala,会出现net.alchim31.maven,version为1.6版本的选项,选择它,点击next,然后在Groupid上填写dblab,在Artifact填写WordCount,package填写dblab.WordCount,点击Finish。这样就可以在maven上创建Scala类了。




四.创建scala类,运行scala程序

点击新建的WordCount工程,再选择src/main/scala,右击dblab.WordCount那个建成的包,New–>scala Class,然后在Name那个文本框中输入dblab.WordCount.WordCount,点击Finish,


然后将如下代码完全复制进去

 
  1. import org.apache.spark.SparkContext
  2. import org.apache.spark.SparkContext._
  3. import org.apache.spark.SparkConf
  4.  
  5. object WordCount {
  6. def main(args: Array[String]) {
  7. val inputFile = "file:///usr/local/spark/mycode/wordcount/word.txt"
  8. val conf = new SparkConf().setAppName("WordCount").setMaster("local[2]")
  9. val sc = new SparkContext(conf)
  10. val textFile = sc.textFile(inputFile)
  11. val wordCount = textFile.flatMap(line => line.split(" ")).map(word => (word, 1)).reduceByKey((a, b) => a + b)
  12. wordCount.foreach(println)
  13. }
  14. }

Java

接下来编译pom.xml文件,把代码运行的包导入环境中,如图

然后将pom.xml里的内容清空,复制黏贴如下代码

 
  1. <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">
  2. <modelVersion>4.0.0</modelVersion>
  3. <groupId>dblab</groupId>
  4. <artifactId>WordCount</artifactId>
  5. <version>0.0.1-SNAPSHOT</version>
  6. <inceptionYear>2008</inceptionYear>
  7. <properties>
  8. <scala.version>2.11</scala.version>
  9. <spark.version>2.1.0</spark.version>
  10. </properties>
  11.  
  12. <repositories>
  13. <repository>
  14. <id>scala-tools.org</id>
  15. <name>Scala-Tools Maven2 Repository</name>
  16. <url>http://scala-tools.org/repo-releases</url>
  17. </repository>
  18. </repositories>
  19.  
  20. <pluginRepositories>
  21. <pluginRepository>
  22. <id>scala-tools.org</id>
  23. <name>Scala-Tools Maven2 Repository</name>
  24. <url>http://download.eclipse.org/releases/indigo/</url>
  25. </pluginRepository>
  26. </pluginRepositories>
  27.  
  28. <dependencies>
  29. <dependency>
  30. <groupId>org.apache.spark</groupId>
  31. <artifactId>spark-core_${scala.version}</artifactId>
  32. <version>${spark.version}</version>
  33. </dependency>
  34.  
  35. <dependency>
  36. <groupId>junit</groupId>
  37. <artifactId>junit</artifactId>
  38. <version>4.4</version>
  39. <scope>test</scope>
  40. </dependency>
  41. <dependency>
  42. <groupId>org.specs</groupId>
  43. <artifactId>specs</artifactId>
  44. <version>1.2.5</version>
  45. <scope>test</scope>
  46. </dependency>
  47. </dependencies>
  48.  
  49. <build>
  50. <sourceDirectory>src/main/scala</sourceDirectory>
  51. <testSourceDirectory>src/test/scala</testSourceDirectory>
  52. <plugins>
  53. <plugin>
  54. <groupId>org.scala-tools</groupId>
  55. <artifactId>maven-scala-plugin</artifactId>
  56. <executions>
  57. <execution>
  58. <goals>
  59. <goal>compile</goal>
  60. <goal>testCompile</goal>
  61. </goals>
  62. </execution>
  63. </executions>
  64. <configuration>
  65. <scalaVersion>${scala.version}</scalaVersion>
  66. <args>
  67. <arg>-target:jvm-1.5</arg>
  68. </args>
  69. </configuration>
  70. </plugin>
  71. <plugin>
  72. <groupId>org.apache.maven.plugins</groupId>
  73. <artifactId>maven-eclipse-plugin</artifactId>
  74. <configuration>
  75. <downloadSources>true</downloadSources>
  76. <buildcommands>
  77. <buildcommand>ch.epfl.lamp.sdt.core.scalabuilder</buildcommand>
  78. </buildcommands>
  79. <additionalProjectnatures>
  80. <projectnature>ch.epfl.lamp.sdt.core.scalanature</projectnature>
  81. </additionalProjectnatures>
  82. <classpathContainers>
  83. <classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER</classpathContainer>
  84. <classpathContainer>ch.epfl.lamp.sdt.launching.SCALA_CONTAINER</classpathContainer>
  85. </classpathContainers>
  86. </configuration>
  87. </plugin>
  88. </plugins>
  89. </build>
  90. <reporting>
  91. <plugins>
  92. <plugin>
  93. <groupId>org.scala-tools</groupId>
  94. <artifactId>maven-scala-plugin</artifactId>
  95. <configuration>
  96. <scalaVersion>${scala.version}</scalaVersion>
  97. </configuration>
  98. </plugin>
  99. </plugins>
  100. </reporting>
  101. </project>

Shell

在代码处右击,Run AS–>Maven build,编译,如果在Console栏里出现Build success,说明编译成功

回到scala程序界面,你会发现左边第二个src/test/scala文件有个小红叉,这个对编译结果并没影响,但会影响后面打包,可以删除它

继续右击WordCount.scala.Run AS –>scala Application,得出结果

接着我们返回pom.xml的界面,空白处右击,Run AS–>Maven install,出现如下界面后说明打包已成功,

打开Ubuntu自带的文件夹系统,进入/home/hadoop/workspace/WordCount/target,会看到WordCount-0.0.1-SNAPSHOT.jar,就是maven打包成功的文件

然后,由于Ubuntu系统的原因,包的路径太深,运行很可能会出现找不到类的异常,所以我们可以把这个包移动到常用的较浅的目录下,

 
  1. sudo cp /home/hadoop/workspace/WordCount/target/WordCount-0.0.1-SNAPSHOT.jar /usr/local #/usr/local是本人常用的目录

Shell

接着我们运行以下指令,

 
  1. /usr/local/spark/bin/spark-submit --class "WordCount" /usr/local/WordCount-0.0.1-SNAPSHOT.jar

Shell

出现如下结果,就说明你在终端下已经成功运行了scala的WordCount程序

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值