一、IDEA安装

下载Community版的IDEA,Ultimate是免费试用版(相当于用到后面要给钱的)

ideaIC-2019.2.3.tar.gz

解压IDEA:

tar -zxvf ideaIC-2019.2.3.tar.gz

查看目录:

[hadoop@hadoop01 idea-IC-192.6817.14]$ ls -ll

total 52

drwxrwxr-x.  2 hadoop hadoop  4096 Oct  2 22:08 bin

-rw-r--r--.  1 hadoop hadoop    14 Sep 24 12:43 build.txt

-rw-r--r--.  1 hadoop hadoop  1914 Sep 24 12:43 Install-Linux-tar.txt

drwxrwxr-x.  7 hadoop hadoop    83 Oct  2 22:08 jbr

drwxrwxr-x.  4 hadoop hadoop  8192 Oct  2 22:08 lib

drwxrwxr-x.  2 hadoop hadoop  4096 Oct  2 22:08 license

-rw-r--r--.  1 hadoop hadoop 11352 Sep 24 12:43 LICENSE.txt

-rw-r--r--.  1 hadoop hadoop   128 Sep 24 12:43 NOTICE.txt

drwxrwxr-x. 43 hadoop hadoop  4096 Oct  2 22:08 plugins

-rw-r--r--.  1 hadoop hadoop   370 Sep 24 12:46 product-info.json

drwxrwxr-x.  2 hadoop hadoop    35 Oct  2 22:08 redist

启动iedea:

[hadoop@hadoop01 bin]$ ./idea.sh

OpenJDK 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release.

Startup Error: Unable to detect graphics environment

报错:Unable to detect graphics environment

解决办法:不要在外面通过远程的方式执行此启动命令,要在linux图形界面中打开终端来执行此命令

在linux终端中执行命令启动idia:

[hadoop@hadoop01 ~]$ cd idea-IC-192.6817.14

[hadoop@hadoop01 idea-IC-192.6817.14]$ bin/idea.sh

OpenJDK 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release.

下载安装scala插件

将 scala-intellij-bin-2019.2.3.zip 放在 scala主目录plugins文件夹里

注:scala插件一定要与idea版本对应,如我的就是:scala-intellij-bin-2019.2.3.zip对应ideaIC-2019.2.3.tar.gz

【idea】scala&sbt+idea安装配置与测试-LMLPHP

再在启动的idea中找到setting里面的plugins中选择install plugin from disk…

【idea】scala&sbt+idea安装配置与测试-LMLPHP

选中插件包

【idea】scala&sbt+idea安装配置与测试-LMLPHP

最后重启idea,即可。

二、IDEA

Normal
0

7.8 磅
0
2

false
false
false

EN-US
ZH-CN
X-NONE

/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:等线;
mso-ascii-font-family:等线;
mso-ascii-theme-font:minor-latin;
mso-fareast-font-family:等线;
mso-fareast-theme-font:minor-fareast;
mso-hansi-font-family:等线;
mso-hansi-theme-font:minor-latin;
mso-font-kerning:1.0pt;}

创建scala新工程

【idea】scala&sbt+idea安装配置与测试-LMLPHP

【idea】scala&sbt+idea安装配置与测试-LMLPHP

注:选择IDEA 便是创建普通的Scala
项目(注:IJ IDEA版本的不同,这里IDEA可能显示成Scala,不过这个并没有什么影响)

工程的基本页面设置:

【idea】scala&sbt+idea安装配置与测试-LMLPHP

其中:

project name    :工程名称

project location  :工程文件地址

jdk             :使用的java version
版本,未找到时,自己选择jdk的安装目录

scala SDK       :使用scala版本,未找到时,自己选择scala的安装目录

点击finish后,进入设计主页面

【idea】scala&sbt+idea安装配置与测试-LMLPHP

这个时候,第一次系统会对设置进行索引。

这里需要指出,IDEA对新工程的索引需要时间,长短视虚拟机的配置而定

在src处右键new—package,准备创建一个scala包

【idea】scala&sbt+idea安装配置与测试-LMLPHP

Package name :com.hadoop

配置工程结构(project structure),为了导入spark依赖包

依次选择主界面的file-- project structure,在弹出的对话框中选择librarie --+--java,选择虚拟机中已安装spark目录下的jars下所有的jar包

【idea】scala&sbt+idea安装配置与测试-LMLPHP

点击OK后,系统会再次进行索引

【idea】scala&sbt+idea安装配置与测试-LMLPHP

创建scala类

在com.hadoop包上右键new
– scala class,弹出如下界面

【idea】scala&sbt+idea安装配置与测试-LMLPHP

输入sparkPi,并选择object

【idea】scala&sbt+idea安装配置与测试-LMLPHP

运行之前,需要修改运行参数

在主界面选择run菜单中的edit  configurations ,在弹出的对话框中左上角点击“+”,在弹出的列表中,选择application,填写运行参数:

【idea】scala&sbt+idea安装配置与测试-LMLPHP

修改的参数有三个:

Name :sparkPi

Main class:com.hadoop.sparkPi

VM options :-Dspark.master=local
-Dspark.app.name=sparkPi

注:VM options 也可以不填写,但是要在代码中加入

.setMaster("local").setAppName("sparkPi")

填写完成后,直接点击OK

三、测试

1、测试程序:

package com.hadoop

import scala.math.random
import org.apache.spark._ object sparkPi { def main(args: Array[String]) { println("Hello, World!")
}
}

Run程序后,结果如下:

【idea】scala&sbt+idea安装配置与测试-LMLPHP

2、编写工程:

package com.hadoop

import scala.math.random
import org.apache.spark._ object sparkPi {
def main(args: Array[String]) {
val conf = new SparkConf().setMaster("local").setAppName("sparkPi")
val spark = new SparkContext(conf)
val slices = if (args.length > 0) args(0).toInt else 2
val n = 10000 * slices
val count = spark.parallelize(1 to n, slices).map { i =>
val x = random * 2 - 1
val y = random * 2 - 1
if (x * x + y * y <) 1 else 0
}.reduce(_ + _)
println("Pi is roughly " + 4.0 * count / n)
spark.stop()
}
}

第一次运行结果:

报错:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/hadoop/fs/FSDataInputStream
at org.apache.spark.SparkConf.loadFromSystemProperties(SparkConf.scala:76)
at org.apache.spark.SparkConf.<init>(SparkConf.scala:71)
at org.apache.spark.SparkConf.<init>(SparkConf.scala:58)
at com.hadoop.sparkPi$.main(sparkPi.scala:9)
at com.hadoop.sparkPi.main(sparkPi.scala)
Caused by: java.lang.ClassNotFoundException: org.apache.hadoop.fs.FSDataInputStream
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 5 more

原因:缺少对应的依赖包

解决办法:下载spark-2.4.4-bin-hadoop2.6,将里面jars包拷贝进去,或者直接重新配置spark-2.4.4-bin-hadoop2.6

第二次运行结果:

报错:

Exception in thread "main" java.lang.IllegalArgumentException: System memory 425197568 must be at least 471859200. 
Please increase heap size using the --driver-memory option or spark.driver.memory in Spark configuration.

原因:Spark是非常依赖内存的计算框架,在虚拟环境下使用local模式时,实际上是使用多线程的形式模拟集群进行计算,因而对于计算机的内存有一定要求,这是典型的因为计算机内存不足而抛出的异常。

解决办法:

在下面这出代码后面添加:“.set("spark.testing.memory","2147480000")”,其实上面的错误提醒了的,如“Please increase heap size using the --driver-memory option or spark.driver.memory in Spark configuration.

val conf = new SparkConf().setMaster("local").setAppName("sparkPi") //添加后

第三次运行(成功):

结果:Pi is roughly 3.1304

【idea】scala&amp;sbt+idea安装配置与测试-LMLPHP

附_完整测试代码:

package com.hadoop

import scala.math.random
import org.apache.spark._ object sparkPi {
def main(args: Array[String]) {
val conf = new SparkConf().setMaster("local").setAppName("sparkPi").set("spark.testing.memory","2147480000") Normal
0 7.8 磅
0
2 false
false
false EN-US
ZH-CN
X-NONE /* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:等线;
mso-ascii-font-family:等线;
mso-ascii-theme-font:minor-latin;
mso-fareast-font-family:等线;
mso-fareast-theme-font:minor-fareast;
mso-hansi-font-family:等线;
mso-hansi-theme-font:minor-latin;
mso-font-kerning:1.0pt;} val spark = new SparkContext(conf)
val slices = if (args.length > 0) args(0).toInt else 2
val n = 10000 * slices
val count = spark.parallelize(1 to n, slices).map { i =>
val x = random * 2 - 1
val y = random * 2 - 1
if (x * x + y * y <) 1 else 0
}.reduce(_ + _)
println("Pi is roughly " + 4.0 * count / n)
spark.stop()
}
}

v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}

Normal
0
false

7.8 磅
0
2

false
false
false

EN-US
ZH-CN
X-NONE

/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:等线;
mso-ascii-font-family:等线;
mso-ascii-theme-font:minor-latin;
mso-fareast-font-family:等线;
mso-fareast-theme-font:minor-fareast;
mso-hansi-font-family:等线;
mso-hansi-theme-font:minor-latin;
mso-font-kerning:1.0pt;}

05-27 21:53