1. 程式人生 > >Spark2.x學習筆記:6、在Windows平臺下搭建Spark開發環境(Intellij IDEA)

Spark2.x學習筆記:6、在Windows平臺下搭建Spark開發環境(Intellij IDEA)

Spark2.x學習筆記

6、 在Windows平臺下搭建Spark開發環境(Intellij IDEA+Maven)

6.1 整合開發環境IDE

為了方便應用程式開發與測試,提高開發效率,一般使用整合開發工具IDE。同樣,為了方便Spark應用程式編寫和測試,可以選擇整合開發工具Intellij IDEA或Eclipse。由於Intellij IDEA對Scala更好的支援,大多Spark開發團隊選擇了Intellij IDEA作為開發環境。

6.2 安裝JDK

在Linux下安裝配置JDK8,請參見http://blog.csdn.net/chengyuqiang/article/details/77671748


在實際生產環境下,首先是在Windows平臺進行應用程式編寫和除錯,然後,程式編譯後釋出到伺服器端執行。所以,這裡需要介紹的是Windows平臺下JDK8的安裝與配置。
(1)下載
登入Oracle官網http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html,接受協議,選擇對應版本。因為我的本機是64位Windows,所以需要下載64位(Windows x64)JDK安裝包。
這裡寫圖片描述
(2)安裝JDK
Windows下安裝JDK非常方便,雙擊安裝程式後,直接單擊下一步即可,預設安裝到C:\Program Files\Java目錄下。其間會安裝JRE,預設一下步即可。
(3)設定環境變數

右鍵單擊桌面上的“計算機”圖示(在Windows10下是“此電腦”),在彈出的右鍵快捷選單中選擇最後一個“屬性”選項;在彈出的系統視窗中,單擊左側“高階系統設定”選項,彈出“系統屬性”對話方塊,如下圖。
這裡寫圖片描述
然後單擊中間的“高階”選項卡,再單擊下方的“環境變數(N)…”按鈕。在彈出的環境變數對話方塊中,首先單擊下方的“新建(W)…”按鈕,然後在彈出的新建環境變數中輸入對應的值。
這裡寫圖片描述

6.3 安裝Scala

(1)下載
通過Spark官網下載頁面http://spark.apache.org/downloads.html 可知“Note: Starting version 2.0, Spark is built with Scala 2.11 by default.”,建議下載Spark2.2對應的 Scala 2.11。

登入Scala官網http://www.scala-lang.org/,單擊download按鈕,然後再“Other Releases”標題下找到“Last 2.11.x maintenance release - Scala 2.11.11”連結

進入http://www.scala-lang.org/download/2.11.11.html頁面,下拉找到如下圖內容,下載msi格式的安裝包即可。
這裡寫圖片描述
(2)安裝
預設安裝到C:\Program Files (x86)\scala目錄下
這裡寫圖片描述
(3)環境變數
與設定Java環境變數型別,
SCALA_HOME=C:\Program Files (x86)\scala
Path環境變數在最後追加;%SCALA_HOME%\bin
這裡寫圖片描述

6.4 安裝Maven

6.5 Intellij IDEA下載與配置

(1)下載與安裝
登入官網https://www.jetbrains.com/idea/download/,按照自己的需求下載(ultimate,旗艦版)或者(Community,社群版)。比如這裡直接下載ultimate

這裡寫圖片描述
下載得到ideaIU-2017.2.3.exe可執行檔案,雙擊即可安裝。
(2)啟動
安裝完成後,單擊週末IntelliJ IDEA圖示即可啟動IntelliJ IDEA.
(3)匯入設定
由於是第一次安裝,所以不需要匯入配置。預設選項即可。
這裡寫圖片描述
(4)接受協議
這裡寫圖片描述
(5)啟用IntelliJ IDEA 2017.2.3(重要)
對於旗艦版的IntelliJ IDEA需要啟用。如下圖所示,首先選中“License server”,然後再輸入框中輸入http://idea.imsxm.com(注意,輸入是這個地址,不是圖中地址),最後單擊“Activate”按鈕啟用即可。
這裡寫圖片描述
(6)選中風格
為了保護眼睛,我傾向於選中Darcula風格。
這裡寫圖片描述
(7)單擊“Next:Default plugins”–>Featured plugins,單擊“Install”下載Scala外掛
這裡寫圖片描述

6.6 設定

(1)單擊“Create New Project”
這裡寫圖片描述

這裡寫圖片描述

6.7 建立Maven專案

這裡寫圖片描述

這裡寫圖片描述

這裡寫圖片描述

這裡寫圖片描述

選中scala–>右鍵快捷選單–>Mark Directory as –>Sources root
這裡寫圖片描述

這裡寫圖片描述

File–>Project Structure–>Global Libaries
這裡寫圖片描述
單擊“Browse”按鈕,選中Scala安裝目錄
這裡寫圖片描述

6.8 編輯程式碼

(1)pom.xml

<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>cn.hadron</groupId>
  <artifactId>simpleSpark</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <properties>
    <spark.version>2.2.0</spark.version>
  </properties>

  <repositories>
    <repository>
      <id>nexus-aliyun</id>
      <name>Nexus aliyun</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public</url>
    </repository>
  </repositories>

  <dependencies>
    <!-- https://mvnrepository.com/artifact/org.apache.spark/spark-core_2.10 -->
    <dependency>
      <groupId>org.apache.spark</groupId>
      <artifactId>spark-core_2.10</artifactId>
      <version>${spark.version}</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.3</version>
        <configuration>
          <classifier>dist</classifier>
          <appendAssemblyId>true</appendAssemblyId>
          <descriptorRefs>
            <descriptor>jar-with-dependencies</descriptor>
          </descriptorRefs>
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

儲存pom.xml檔案後,如果Intellij IDEA右下角出現如下提示,請單擊“Enable Auto-Import”
這裡寫圖片描述
(2)WordCount.scala
新建Scala Class類WordCount.scala,Scala原始檔字尾名是.scala。

package cn.hadron

import org.apache.spark._

object WordCount {
  def main(args: Array[String]) {
    var masterUrl = "local[1]"
    var inputPath = "D:\\data\\Hamlet.txt"
    var outputPath = "D:\\output"

    if (args.length == 1) {
      masterUrl = args(0)
    } else if (args.length == 3) {
      masterUrl = args(0)
      inputPath = args(1)
      outputPath = args(2)
    }

    println(s"masterUrl:${masterUrl}, inputPath: ${inputPath}, outputPath: ${outputPath}")

    val sparkConf = new SparkConf().setMaster(masterUrl).setAppName("WordCount")
    val sc = new SparkContext(sparkConf)

    val rowRdd = sc.textFile(inputPath)
    val resultRdd = rowRdd.flatMap(line => line.split("\\s+"))
        .map(word => (word, 1)).reduceByKey(_ + _)

    resultRdd.saveAsTextFile(outputPath)
  }
}

6.9 資料檔案

從網路上下載一部文字格式的小說,比如Hamlet.txt,存放到D:\data目錄。

6.10 執行

在原始檔程式碼中右鍵單擊–>Run “WordCount”

這裡寫圖片描述

這裡寫圖片描述

這裡寫圖片描述