Eclipse創建包含Scala的Maven工程_Spark學習demo

爲了學習spark,在mac上使用eclipse創建包含scala的maven工程,並打包至於服務器運行。

1.1 hadoop安裝

安裝hadoop2.6.0,參考博客

1.2 spark下載

下載spark-1.6.0-bin-hadoop2.6.tgz,在官網下載,在 choose a download type中建議選擇 select apache mirror。
下載完成之後放在自己的相應目錄,
運行spark命令:
./sbin/start-master.sh #啓動/停止主機
./sbin/stop-master.sh #啓動/停止主機
./bin/spark-class org.apache.spark.deploy.worker.Worker spark://DevindeMac-mini.local:7077 #加子嗣
完成之後可以在 http://localhost:8080/ 查看運行詳情。

1.3 scala下載

安裝包下載地址,進入官網:http://www.scala-lang.org/,選擇相應的版本,我選擇的scala-2.10.4, 這裏可能在機器上不能正常運行的原因是jdk版本過低。
下載完成之後,放在指定目錄,在 ~/.bash_profile配置環境變量
export PATH=$PATH:/usr/local/Cellar/scala-2.10.4/bin
成功之後可以使用 在終端運行 scala -version 、scala 查看是否成功。

1.4 安裝scala IDE

網上都是直接下載 http://scala-ide.org/download/sdk.html,這裏的鏡像或者在線安裝:eclipse在安裝插件的方式安裝。(eclipse 尤其注意版本)
(1) 在Eclipse中選擇Help->Install new Software
http://download.scala-ide.org/sdk/helium/e38/scala210/stable/site

這裏寫圖片描述

ps: 這裏需要翻牆,且時間比較長
(2)加插件下載鏈接安裝後,會提示重新啓動Eclipse
(3)測試
新建Scala Project工程Test

object Testd {
  def main(args: Array[String]): Unit = {
     val array = Array("devin","shuai")
     array.foreach(println)
  }
}

(4)建立基於spark的scala程序
在eclipse中,依次選擇“File” –>“New” –> “Other…” –> “Scala Wizard” –> “Scala Project”,創建一個Scala工程,並命名爲“SparkScala”。
右擊“SaprkScala”工程,選擇“Properties”,在彈出的框中,按照下圖所示,依次選擇“Java Build Path” –>“Libraties” –>“Add External JARs…”,導入spark-assembly-1.6.0-hadoop2.6.0.jar在spark-1.6.0-bin-hadoop2.6的lib目錄下。如下圖
這裏寫圖片描述

package com.devin.spark.demo

import org.apache.spark.SparkConf
import org.apache.spark.SparkContext

object WordCount {

  def main(args: Array[String]) {      
   if (args.length < 0) {
       System.err.println("Usage: <file>")
       System.exit(1)
    }  
   println( "Hello World!" )
   //.setMaster("local")
   val conf = new SparkConf().setAppName("wordcount")//.setMaster("local")
   val sc = new SparkContext(conf)
   val line = sc.textFile("hdfs://localhost:9000/spark_in/").cache()  
   line.flatMap(_.split(" ")).map((_, 1)).reduceByKey(_+_).collect().foreach(println)
   sc.stop();
  }
}

ps:運行代碼時,可能出現scala衝突的情況,需要設置scala版本

1.5 建立基於maven的scala項目

(1) 安裝m2e-scala ,url是:http://alchim31.free.fr/m2e-scala/update-site/ 方法也是在eclipse中 install new soft 一樣,如下圖
這裏寫圖片描述
(2) 添加遠程的原型或模板目錄
http://repo1.maven.org/maven2/archetype-catalog.xml建議先下載到本地再用加載本地的方式去添加(我用遠程試了幾次都沒有成功)
這裏寫圖片描述
完成之後,重啓eclipse,再創建maven工程就好。代碼可以複用上面的。

這裏寫圖片描述

這裏尤其注意修改原pom中內容(下面是我可用的pom文件):

<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>com.devin</groupId>
  <artifactId>spark.demo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>${project.artifactId}</name>
  <description>My wonderfull scala app</description>
  <inceptionYear>2015</inceptionYear>

  <properties>
    <maven.compiler.source>1.6</maven.compiler.source>
    <maven.compiler.target>1.6</maven.compiler.target>
    <encoding>UTF-8</encoding>
    <scala.version>2.10.4</scala.version>
    <scala.compat.version>2.10</scala.compat.version>
  </properties>

  <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.10</artifactId>
      <version>1.6.0</version>
    </dependency> 


  </dependencies>

  <build>
    <sourceDirectory>src/main/scala</sourceDirectory>
    <testSourceDirectory>src/test/scala</testSourceDirectory>
    <plugins>
      <plugin>
        <!-- see http://davidb.github.com/scala-maven-plugin -->
        <groupId>net.alchim31.maven</groupId>
        <artifactId>scala-maven-plugin</artifactId>
        <version>3.2.0</version>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>
              <goal>testCompile</goal>
            </goals>
            <configuration>
              <args>
                <arg>-make:transitive</arg>
                <arg>-dependencyfile</arg>
                <arg>${project.build.directory}/.scala_dependencies</arg>
              </args>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.18.1</version>
        <configuration>
          <useFile>false</useFile>
          <disableXmlReport>true</disableXmlReport>
          <!-- If you have classpath issue like NoDefClassError,... -->
          <!-- useManifestOnlyJar>false</useManifestOnlyJar -->
          <includes>
            <include>**/*Test.*</include>
            <include>**/*Suite.*</include>
          </includes>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

這裏可能遇見一些scala版本不對呀問題,處理方式
http://scala-ide.org/docs/current-user-doc/faq/index.html
If this check returns a false-negative, it can be disabled at the workspace level, or at the project level. The setting is withVersionClasspathValidator in the Scala → Compiler → Build Manager preference section.
如下圖;
這裏寫圖片描述

在mvn打包的時候有可能會遇到:
[ERROR] Failed to execute goal net.alchim31.maven:scala-maven-plugin:3.2.0:compi
le (default) on project iteblog: wrap: org.apache.commons.exec.ExecuteException:
Process exited with an error: 1 (Exit value: 1) -> [Help 1]
原因是mvn clean package默認只處理Java源代碼的編譯、打包,而不管scala,所以編譯時遇到Hello這個由scala語言編寫的class,此時scala還沒編譯生成class,所以找不到相應的調用入口。
解決辦法:
mvn clean scala:compile compile package
也可以分開命令
mvn clean
mvn scala:compile compile
mvn package

打包完成之後,提交到spark集羣

./bin/spark-submit --master spark://DevindeMac-mini.local:7077 --class com.devin.spark.demo.WordCount /usr/local/Cellar/spark.demo-0.0.1-SNAPSHOT.jar

參考:
http://blog.csdn.net/ldds_520/article/details/51830721
http://blog.csdn.net/BeautyGao/article/details/38490193
http://blog.csdn.net/fz1989/article/details/50499389
http://blog.csdn.net/wanglz666/article/details/44224349

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章