利用intellij idea 搭建spark开发环境(windows)

利用intellij idea 搭建spark开发环境(windows)

本文配置所有环境

Win10 企业版2016长期服务版
Jdk1.8.0.131
Hadoop2.7.3
Spark2.2.0
Scala2.11.12

一、Jdk环境配置

  • 下载
    http://www.oracle.com/technetwork/cn/java/javase/downloads/jdk8-downloads-2133151-zhs.html
    可能需要oracle账号,目前更新到了151版。没有账号的可以移步
    链接: https://pan.baidu.com/s/1jIaj0NW 密码: kpdb
  • 配置环境变量
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环境搭建

  • 下载代码
    https://mirrors.tuna.tsinghua.edu.cn/apache/hadoop/common/
    这个镜像库中已经没有2.7.3版本,如需下载请移步
  • 配置环境变量
set HADOOP_HOME=D:\javaEnv\hadoop-2.7.3
set PATH=%PATH%;%HADOOP_HOME%\bin;

根据自己的实际情况配置。

三、spark环境搭建

  • 下载代码
    链接: https://pan.baidu.com/s/1jIgUV6e 密码: tphe
  • 配置环境变量
SPARK_HOME=D:\javaEnv\spark-2.2.0-bin-hadoop2.7
set PATH=%PATH%;%SPARK_HOME%\bin;

根据自己的实际情况配置。

四、Scala安装

  • 下载文件
    链接: https://pan.baidu.com/s/1bZdMH4 密码: qtgm
  • 配置环境变量
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
  • 10
    点赞
  • 51
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yiluohan0307

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值