好的,假設項目數據調研與需求分析已接近尾聲,立刻進入Coding階段了,辣麼在Coding以前須要幹馬呢?是的,「統一開發工具、開發環境的搭建與本地測試、測試環境的搭建與測試」 - 本文詳細記錄實際Spark項目開發環境的搭建。java
操做系統:win 10
JDK 版本 :jdk1.8.0_91
Scala版本:2.10.6
MAVEN版本:apache-maven-3.3.9
集成開發工具:IntelliJ IDEA 2016.1.3
開發主要語言:scalamysql
一. 搭建過程文檔
一、新建一個Maven工程
這裏以新建一個名稱爲fantasia的maven工程爲例加以說明。sql
設置完了,選擇下一步數據庫
設置完了,選擇下一步apache
點擊 finish 後idea會加載maven與junit等相關的插件,可能須要30分鐘左右的時間(網速決定)。json
二、自定義maven的repository目錄
idea內置了maven插件,且默認repository目錄爲C:\Users\${username}\.m2\repository
,這裏咱們爲項目指定一個新的repository,以方便管理依賴的jar包:markdown
三、在pom.xml文件中配置相關依賴包
這裏一次性導入項目可能用到的jar包,具體內容以下:架構
<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>eclipse
<groupId>com.pl.bdeu.bigdata</groupId>
<artifactId>fantasia</artifactId>
<version>1.0-SNAPSHOT</version>
<inceptionYear>2008</inceptionYear>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<scala.version>2.10.6</scala.version>
<spark.version>1.6.2</spark.version>
<hadoop.version>2.6.0</hadoop.version>
</properties>
<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>
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${scala.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.specs</groupId>
<artifactId>specs</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.10</artifactId>
<version>${spark.version}</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_2.10</artifactId>
<version>${spark.version}</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-hive_2.10</artifactId>
<version>${spark.version}</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-streaming_2.10</artifactId>
<version>${spark.version}</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-mllib_2.10</artifactId>
<version>${spark.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>${hadoop.version}</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-streaming-kafka_2.10</artifactId>
<version>${spark.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.4.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.4.3</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.41</version>
</dependency>
<dependency>
<groupId>fastutil</groupId>
<artifactId>fastutil</artifactId>
<version>5.0.9</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/scala</sourceDirectory>
<testSourceDirectory>src/test/scala</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<scalaVersion>${scala.version}</scalaVersion>
<args>
<arg>-target:jvm-1.5</arg>
</args>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<configuration>
<downloadSources>true</downloadSources>
<buildcommands>
<buildcommand>ch.epfl.lamp.sdt.core.scalabuilder</buildcommand>
</buildcommands>
<additionalProjectnatures>
<projectnature>ch.epfl.lamp.sdt.core.scalanature</projectnature>
</additionalProjectnatures>
<classpathContainers>
<classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER</classpathContainer>
<classpathContainer>ch.epfl.lamp.sdt.launching.SCALA_CONTAINER</classpathContainer>
</classpathContainers>
</configuration>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<configuration>
<scalaVersion>${scala.version}</scalaVersion>
</configuration>
</plugin>
</plugins>
</reporting>
</project>
四、項目基礎架構
新建兩個子包:collector與 core
collector:存放 數據採集相關spark做業
core:存放核心業務類spark做業
resource目錄下存放相關配置文件:數據庫鏈接信息,kafka環境信息等,
其餘的後續根據具體模塊功能個再自行定義。
五、本地環境測試
編寫 FrameworkExeTest類對框架可用性進行測試
package com.pl.bdeu.bigdata
import org.apache.commons.logging.LogFactory
import org.apache.spark.{SparkConf, SparkContext}
/**
* author pengych@pl.com
* date 2016/7/24
* function 框架可用性測試
*
執行結果:
(hello,2)
(pl,1)
(fantasia,1)
*/
object FrameworkExeTest {
def main(args: Array[String]) {
val log = LogFactory.getLog("FrameworkExeTest")
val conf = new SparkConf().setMaster("local[*]").setAppName("fantasia framework test")
val sc = new SparkContext(conf)
if(log.isDebugEnabled){
log.debug(" SparkContext initialized")
}
val linesRDD= sc.textFile("E:\\wordcount.txt")
linesRDD.flatMap(line => line.split(" ") ).map( word => (word,1) ).reduceByKey(_+_).
collect.foreach(println)
sc.stop()
}
}
耐心很重要,由於網速極可能很慢
別在idea加載依賴包的時候手動幹掉正在加載的進程,這樣極可能致使各類找不到包的狀況.
在maven的安裝目錄: ~\apache-maven-3.3.9\conf\settings.xml的標籤裏自定義repository路徑
本文指定repository的路徑爲:E:\apache-maven-3.3.9\repository
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> <localRepository>E:\apache-maven-3.3.9\repository</localRepository>