新建maven工程:
- 在第一次使用idea时,如下图选择新建工程

- 如下图,选择maven工程,sdk通过new,选择安装的jdk,然后点击next:

- 填写groupid和artifactid

- 项目名根据需要可修改

-
import maven
Paste_Image.png
配置工程属性、工程目录、scala库
如下图,左边为项目结构,src 目录下为编写的代码,main为程序代码,test为测试代码。pom.xml文件为maven的打包关联依赖及编译文件

- 设置scalaSDK
点击file –> project Structure,如下图:

- 点击Global Libraries, 然后点击左侧 + ,选择Scala SDK,然后再弹出框中选择2.10.4,弹出框的model选择我们的当前工程;



- 设置scala程序路径
在model –> source 区域,如下图,新建两个scala文件夹,并且,main中的scala文件夹设置为source,test中的scala文件夹设置为test,点击ok完成。

- 修改pom文件,添加spark支持
修改pom文件后,maven会自行下载对应的jar包,以便实现工程库文件依赖。
一般情况下,我们只需要添加scala-library、spark-core、spark-mllib、spark-sql、spark-hive几个就够了,如果涉及到其他内容,则另行添加便可。添加内容如下所所示:
<dependencies>
<!-- https://mvnrepository.com/artifact/org.scala-lang/scala-library -->
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.11.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.spark/spark-core_2.11 -->
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.11</artifactId>
<version>2.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.spark/spark-mllib_2.11 -->
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-mllib_2.11</artifactId>
<version>2.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.spark/spark-sql_2.11 -->
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_2.11</artifactId>
<version>2.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.spark/spark-hive_2.11 -->
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-hive_2.11</artifactId>
<version>2.1.0</version>
</dependency>
</dependencies>
- 添加完后pom文件截图如下所示,maven会自行下载对应库(需要联网),并依赖到工程中。修改完后的pom文件如下所示:

- 工程重新构建完毕后,可以看见maven下载的关联包如下图所示:

编写helloworld
- 在src ->main ->scala 目录下,点击创建scala class

- 在弹出框,名字填写为HelloWord ,类型选择为object,然后点击ok

- 打开新建的程序 helloworld,编写程序如下并保存:
object HelloWord {
def main(args: Array[String]) {
println("Hello World!");
}
}
-右键点击 HelloWord 程序代码,点击 run HelloWord :

- 待编译完成后,执行结果如下图:

网友评论