本文先敘述如何配置eclipse中maven+scala的開發環境,之后,敘述如何實現spark的本地運行。最后,成功運行scala編寫的spark程序。
剛開始我的eclipse+maven環境是配置好的。
系統:win7
eclipse版本:Luna Release(4.4.0)
maven是從EclipseMarket中安裝的,如圖1。
當初構建eclipse+maven環境時,僅僅安裝了第一個。
這里可以先不用急着安裝maven,下面在安裝maven for scala時,也提供了maven for eclipse。
圖1-eclipse安裝的m2e插件
一、配置eclipse + maven + scala環境
1. 在Eclipse Market中安裝Scala IDE
圖2-eclipse安裝Scala IDE
2. 安裝m2e-scala
如圖3,圖中的url是:http://alchim31.free.fr/m2e-scala/update-site/
從圖3中搜索到的插件名稱中可以看到,這里同時也配置了m2e,也即eclipse需要的maven插件。如果eclipse沒有eclipse插件,則可以全部選中安裝;若已經有了可以單獨安裝第三個Maven Integration for Scala IDE。
安裝完成了MavenIntegration for Scala IDE之后,再輸入上面的url,可安裝列表里就沒有Maven Integration for Scala IDE這一項了。
(PS:此處我是將MavenIntegration for Scala IDE卸載了之后重新截圖的)
(PS:如果再看圖1,除了第一個MavenIntegration for Eclipse(Luna and newer)1.5之外,還有一個MavenIntegration for Eclipse(Luna)1.5.0,。這是我在使用上述 url安裝m2e-scala時,沒有注意其中還包含了MavenIntegration for Eclipse,導致安裝了兩個版本的Maven Integration for Eclipse)
(PS:雖然我已經安裝上述url中的MavenIntegration for Eclipse,並且並沒有卸載,而圖3中依然顯示了Maven Integration for Eclipse的選項,是因為其版本有了更新。可以從其中看到其最新的版本是1.5.1,此時若繼續安裝該Maven Integration for Eclipse,則是對版本進行更新。)
(PS:圖1中還有一個MavenIntegration for Eclipse WTP(Juno)1.0.1暫時不知道是怎么安裝上去的)
圖3-安裝m2e-scala
二、測試eclipse+maven+scala的運行環境
1. 先來簡單測試一下eclipse+scala
新建一個名為Scala Project,右鍵工程添加一個名為test的Scala Object,代碼如下:
package test object test { def main(args : Array[String]) { println("hello world") } }
最終如圖4、5所示。
圖4-新建scalaproject
、
圖5-scala工程目錄
右鍵test.scala,Run as…-> Scala Application,在終端成功輸出了hello world。
從圖5中可以看到,我們安裝的ScalaIDE中自帶的scala版本是2.11.5的。
(PS:如果不在終端以命令行的形式使用scala的話,似乎可以不用單獨下載scala包並設置環境變量)
2. 再來測試一下ecliipse+scala+maven
本來新建一個scala+maven的流程可以是這樣的,如圖6所示。
新建maven工程,不勾選Createa simple project,選擇與scala有關的archetype。
eclipse的archetype是一種模板,給人的感覺就是其中的目錄架構及相關文件(比如說pom.xml)都是按照某種模式(如scala maven)構造好的。如果選擇如圖6中的1.2版本的scala相關archetype,則新建的maven工程就有了scala maven工程的目錄結構,pom.xml也是配置好的,並且還有幾個scala的代碼文件。
但是,有一些錯誤,編譯無法通過。我想,這主要是因為scala的版本問題,從工程中的pom.xml中可以看到,這個模板是基於scala 2.7.0構建的。而我們安裝的scala IDE是基於scala 2.11.5。
圖6-新建scala maven工程
scala的新版本對老版本的兼容似乎並不好。這里可以自己修正pom.xml文件,不過估計代碼可能也要修改。
我這里是從git上下載了一個現成的基於scala2.11.5的maven工程。
git網址:https://github.com/scala/scala-module-dependency-sample
使用git clone下來之后,在eclipse中導入maven工程(maven-sample)。
從其pom.xml中可以看到,是基於scala-2.11.5的。其中只有一個代碼文件,即是XMLHelloWorld.scala。只要能夠順利的拉取到pom.xml中的依賴包,就可以直接右鍵XMLHelloWorld.scala, Run as -> Scala Application。
至此,ecipse+scala+maven就搭建好了。接下來配置spark的本地運行環境。
三、配置spark的本地運行
1. 配置所需依賴包
這里我是在maven-sample工程的基礎上配置spark的。
在pom.xml中添加spark-core。
<dependency> <groupId>org.Apache.spark</groupId> <artifactId>spark-core_2.11</artifactId> <version>1.2.1</version> </dependency>
在default package中添加scala object – SimpleApp。代碼如下:
/* SimpleApp.scala */ import org.apache.spark.SparkContext import org.apache.spark.SparkContext._ import org.apache.spark.SparkConf object SimpleApp { def main(args: Array[String]) { val logFile = "test.txt" // Should be some file on your system val conf = new SparkConf().setAppName("Simple Application").setMaster("local[2]") val sc = new SparkContext(conf) val logData = sc.textFile(logFile, 2).cache() val numAs = logData.filter(line => line.contains("a")).count() val numBs = logData.filter(line => line.contains("b")).count()
println("Lines with a: %s, Lines with b: %s".format(numAs, numBs)) } }
此時,編譯已經通過了,但是如果Run as–> Scala Application的話,會有ClassDefNotFound的異常。
這是因為spark-core其實需要依賴很多其他的jar包來運行,但這些包在spark-core包中並沒有,並且在我們的classpath中也沒有。
我們可以方便的從在線maven庫里找到spark-core包。
-------------------------------------------------------------------------------------------------------------------------------
項目需求:我們采用spark開發項目,使用的開發語言采用java和scala的混合,這個時候我們的項目需要支持java和scala,一般方法兩種
(1)通過IDEA開發工具,下載SBT安裝包,通過SBT創建項目,自動支持java和scala比較方便,但包的下載很慢
(2)項目我們使用IDEA開發工具,通過maven來完成java和scala混合項目
下面我們專門介紹如何通過maven來支持Java和Scala語言的項目,主要涉及的內容如下
1、執行創建語句(命令行模式下執行)
mvn archetype:generate -DarchetypeGroupId=org.scala-tools.archetypes -DarchetypeArtifactId=scala-archetype-simple -DremoteRepositories=http://scala-tools.org/repo-releases
執行過程中需要讓您輸入以下參數,根據步驟輸入即可
Define value for property 'groupId': :
Define value for property 'artifactId': :
Define value for property 'version': 1.0-SNAPSHOT: :
Define value for property 'package': : :
輸入完成后,通過回車結束,然后就可以創建成功了(緊scala項目)
2、項目結構
說明:
bigdata-user-profile 父目錄(在pom.xml 中,通過model方式引入)
userprofile-db 子工程
userprofile-es 子工程
3、手動方式添加java目錄(兩個項目)
4、介紹一下3個工程pom.xml 配置
4.1 bigdata-user-profile 的pom.xml配置
<?xml version="1.0" encoding="UTF-8"?> <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.demo.userprofile</groupId> <artifactId>bigdata-user-profile</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>userprofile-db</module> <module>userprofile-es</module> </modules> <properties> <encoding>UTF-8</encoding> <scala.version>2.10.5</scala.version> </properties> <dependencies> <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-core_2.10</artifactId> <version>1.6.0</version> </dependency> <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-sql_2.10</artifactId> <version>1.6.0</version> </dependency> <dependency> <groupId>org.scala-lang</groupId> <artifactId>scala-library</artifactId> <version>${scala.version}</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.scala-lang</groupId> <artifactId>scala-compiler</artifactId> <version>${scala.version}</version> <scope>compile</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <pluginManagement> <plugins> <plugin> <groupId>net.alchim31.maven</groupId> <artifactId>scala-maven-plugin</artifactId> <version>3.2.1</version> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.0.2</version> </plugin> </plugins> </pluginManagement> <plugins> <plugin> <groupId>net.alchim31.maven</groupId> <artifactId>scala-maven-plugin</artifactId> <executions> <execution> <id>scala-compile-first</id> <phase>process-resources</phase> <goals> <goal>add-source</goal> <goal>compile</goal> </goals> </execution> <execution> <id>scala-test-compile</id> <phase>process-test-resources</phase> <goals> <goal>testCompile</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <executions> <execution> <phase>compile</phase> <goals> <goal>compile</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>1.4</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <filters> <filter> <artifact>*:*</artifact> <excludes> <exclude>META-INF/*.SF</exclude> <exclude>META-INF/*.DSA</exclude> <exclude>META-INF/*.RSA</exclude> </excludes> </filter> </filters> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
4.2 user-profile-db 的pom.xml配置(user-profile-es 中pom.xml 同user-profile-db,不在介紹)
<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"> <parent> <groupId>com.demo.userprofile</groupId> <artifactId>bigdata-user-profile</artifactId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <groupId>com.demo.userprofile</groupId> <artifactId>userprofile-db</artifactId> <version>1.0-SNAPSHOT</version> <name>${project.artifactId}</name> <dependencies> <dependency> <groupId>org.mongodb</groupId> <artifactId>bson</artifactId> <version>3.0.4</version> </dependency> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> <version>3.0.4</version> </dependency> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20141113</version> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.3</version> </dependency> </dependencies> </project>
5、編寫測試代碼
分別在java和scala目錄下創建測試代碼,主要測試打包是否完整,具體結構如圖所示
6、執行maven的命令
mvn clean compile package
直到出現下面的命令表示成功
[INFO] Replacing D:\demo\user_profiles\bigdata-user-profile\userprofile-es\target\userprofile-es-1.0-SNAPSHOT.jar with D:\demo\user_profiles\bigdata-user-p
rofile\userprofile-es\target\userprofile-es-1.0-SNAPSHOT-shaded.jar
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] bigdata-user-profile ............................... SUCCESS [ 33.012 s]
[INFO] userprofile-db ..................................... SUCCESS [ 33.083 s]
[INFO] userprofile-es ..................................... SUCCESS [ 31.985 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:38 min
[INFO] Finished at: 2016-09-26T11:34:38+08:00
[INFO] Final Memory: 84M/857M
[INFO] ------------------------------------------------------------------------
7、通過編譯工具查看是否把java和scala代碼打包成功
實際測試工程中已經ok
到這里我們使用maven創建scala和java混合項目就成功了,以后再開發Spark項目時候,就可以使用這種方式替代sbt方式了
------------------------------------------------------------------------------------------------------------------------
上面的“MavenIntegration for Scala IDE”我沒有下載成功,直接使用maven命令行創建scala工程如下命令:
1、安裝maven及配置環境變量
2、要裝Scala IDE即eclipse中的插件。
3、跳過m2e-scala這個插件 http://alchim31.free.fr/m2e-scala/update-site/
4、cd到要創建項目的目錄,再輸入如下命令創建:
mvn archetype:generate -DarchetypeGroupId=net.alchim31.maven
-DarchetypeArtifactId=scala-archetype-simple
-DremoteRepositories=http://scala-tools.org/repo-releases
-DgroupId=com.sf
-DartifactId=scalademo3
-Dversion=1.0-SNAPSHOT
創建成功后,導入到eclipse中結果如下:
在src的scala下,創建一個scala object
在src下創建一個"java"的package,在下面創建一個java class
在target目錄下生產的文件如下: