利用intellij idea 搭建spark開發環境(windows)

利用intellij idea 搭建spark開發環境(windows)

本文配置所有環境

Win10 企業版2016長期服務版
Jdk1.8.0.131
Hadoop2.7.3
Spark2.2.0
Scala2.11.12

一、Jdk環境配置

set JAVA_ENV=D:\javaEnv\Java\jdk1.8.0_131
set JAVA_HOME=%JAVA_ENV%\Java\jdk1.8.0_131
set CLASSPATH=.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar;
set PATH=%PATH%;%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin;

根據實際情況配置。

二、hadoop環境搭建

set HADOOP_HOME=D:\javaEnv\hadoop-2.7.3
set PATH=%PATH%;%HADOOP_HOME%\bin;

根據自己的實際情況配置。

三、spark環境搭建

SPARK_HOME=D:\javaEnv\spark-2.2.0-bin-hadoop2.7
set PATH=%PATH%;%SPARK_HOME%\bin;

根據自己的實際情況配置。

四、Scala安裝

SCALA_HOME=D:\javaEnv\scala-2.11.12
set PATH=%PATH%;%SCALA_HOME%\bin;

根據自己的實際情況配置。

五、安裝intellij idea

我這裏使用的是ideaIU-2016.3.7
下載地址https://www.jetbrains.com/idea/download/previous.html
下載安裝即可
破解方法,自己去百度即可

六、IntelliJ IDEA自定義配置和緩存位置

將.IntelliJIdeaXX 目錄移到其他分區的辦法:
1、將C盤的.IntelliJIdeaXX 目錄拷貝至自定義位置;
2、修改intellij idea安裝目錄下 \bin\idea.properties文件,將所有的 ${user.home} 替換爲自定義位置 (注意斜槓的方向)

cfg = D:/JetBrains
idea.config.path=${cfg}/.IntelliJIdea2016.3/config
idea.system.path=${cfg}/.IntelliJIdea2016.3/system

3、啓動 IntelliJ Idea 16,選擇previous配置(即自定義位置)

七、添加scala插件和配置JDK、SDK

1.添加scala插件
這裏寫圖片描述
搜索scala並安裝
這裏寫圖片描述
重啓程序即可。
2.配置SDK和JDK
這裏寫圖片描述
添加JDK
這裏寫圖片描述
添加scala sdk
這裏寫圖片描述

八、配置maven

這裏寫圖片描述
這裏寫圖片描述

九、創建WordCount工程實例

1.創建maven工程
這裏寫圖片描述
2.設置groupId和artifactId
這裏寫圖片描述
4.設置工程目錄
這裏寫圖片描述
點擊完成即可
5.爲工程添加scala框架支持
這裏寫圖片描述
這裏寫圖片描述
6.在src文件夾中創建一個WordCount文件夾並設定爲resources root
這裏寫圖片描述
這裏寫圖片描述
7.在WordCount中新建package和scala程序
這裏寫圖片描述
這裏寫圖片描述
將以下代碼複製進去

package com.unicom.cuiyufei
/**
  * Created by cuiyufei on 2018/2/12.
  */
import org.apache.spark.SparkContext
import org.apache.spark.SparkContext._
import org.apache.spark.SparkConf
object WordCount {
  def main(args: Array[String]) {
    val inputFile =  "F:\\spark\\spark.txt"
    val conf = new SparkConf().setAppName("WordCount").setMaster("local")
    val sc = new SparkContext(conf)
    val textFile = sc.textFile(inputFile)
    val wordCount = textFile.flatMap(line => line.split(" ")).map(word => (word, 1)).reduceByKey((a, b) => a + b)
    wordCount.foreach(println)
  }
}

在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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>WODAS</groupId>
    <artifactId>WordCount</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <spark.version>2.1.0</spark.version>
        <scala.version>2.11</scala.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>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-core_${scala.version}</artifactId>
            <version>${spark.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-streaming_${scala.version}</artifactId>
            <version>${spark.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-sql_${scala.version}</artifactId>
            <version>${spark.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-hive_${scala.version}</artifactId>
            <version>${spark.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-mllib_${scala.version}</artifactId>
            <version>${spark.version}</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.scala-tools</groupId>
                <artifactId>maven-scala-plugin</artifactId>
                <version>2.15.2</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19</version>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

8.右鍵整個工程Generate Sources and Update Folders,在scala代碼界面右鍵點擊執行代碼
這裏寫圖片描述
執行的過程中出現如下錯誤

Command line is too long. Shorten command line for WordCount or also for Application default configure
通過在shorten command line選擇JAR manifest
這裏寫圖片描述
10.打包執行
這裏寫圖片描述
這裏寫圖片描述
這裏寫圖片描述
這裏寫圖片描述

十、Intellij idea 常用插件

1.Key promoter
Key promoter這款插件適合新手使用。當你點擊鼠標一個功能的時候,可以提示你這個功能快捷鍵是什麼
2.Maven Helper
3. JRebel for IntelliJ
jrebel,熱部署插件,能夠在開發過程中幫助開發者節約大量的部署等待時間,幾乎所有的代碼改動都不需要重啓應用服務器,連Spring增加一個Bean都可以熱部署。
4. IdeaVim
如果喜歡Vim那種移動光標的快捷鍵,也有一個類似Vim的插件,IdeaVim,可以在Editor裏面體驗Vim的感覺。
5.畫UML的話推薦 PlantUML 插件
6.mybatis plus
自由在java的interface與mapper文件間跳轉
7.GsonFormat
Key promoter 快捷鍵提示 https://plugins.jetbrains.com/plugin/4455?pr=idea
String Manipulation 駝峯式命名和下劃線命名交替變化 https://plugins.jetbrains.com/plugin/2162?pr=idea
CheckStyle-IDEA 代碼規範檢查 https://plugins.jetbrains.com/plugin/1065?pr=idea
FindBugs-IDEA 潛在 Bug 檢查 https://plugins.jetbrains.com/plugin/3847?pr=idea
MetricsReloaded 代碼複雜度檢查 https://plugins.jetbrains.com/plugin/93?pr=idea
Statistic 代碼統計 https://plugins.jetbrains.com/plugin/4509?pr=idea
JRebel Plugin 熱部署 https://plugins.jetbrains.com/plugin/?id=4441
CodeGlance 在編輯代碼最右側,顯示一塊代碼小地圖 https://plugins.jetbrains.com/plugin/7275?pr=idea
GsonFormat 把 JSON 字符串直接實例化成類 https://plugins.jetbrains.com/plugin/7654?pr=idea
MultiMarkdown 書寫 Markdown 文章 https://plugins.jetbrains.com/plugin/7896?pr=idea
Eclipse Code Formatter 使用 Eclipse 的代碼格式化風格,在一個團隊中如果公司有規定格式化風格,這個可以使用。 https://plugins.jetbrains.com/plugin/6546?pr=idea
Jindent-Source Code Formatter 自定義類、方法、doc、變量註釋模板http://plugins.jetbrains.com/plugin/2170?pr=idea
ECTranslation 翻譯插件 https://github.com/Skykai521/ECTranslation/releases
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章