*1.本地idea配置scala*
访问scala官网下载scala安装包,windows和linux都需要。
先配置本地scala,解压scala-{版本号}.zip文件配置环境变量,和jdk一样。
SCALA_HOME->new %SCALA_HOME%=”解压路径”
PATH->添加%SCALA_HOME%/bin;
Win+R,输入scala可以运行就配置成功。
打开idea->File->Settings->plugins
从本地选择插件安装
在idea官网上可以找到scala的相关插件,自行选择版本
https://plugins.jetbrains.com/plugin/1347-scala
安装完创建maven项目,要么从pom中导入scala相关jar包,要么右键点击项目
在里面找到scala手动添加##每个项目都需要手动添加,嫌麻烦就在pom里导入jar包依赖
右键main文件夹->new dictionary “scala”->右键scala文件夹->Mark Dictionary as->source root
这样把新创建的scala文件夹作为了项目的代码根文件夹。
创建package->new scala object->scala需要选择object创建。
下面展示一些 内联代码片
。
object wordcount {
def main(args: Array[String]): Unit = {
Printf(hello world!)
}
}
先来个helloworld,scala换行就相当于分号了,除非两条代码在同一行需要分号隔开,其他情况基本不需要分号,当然写了不会报错。
至此scala算是完事儿了。
*2.编写spark第一个wordcount程序*
*Pom依赖*
Pom中引入相关依赖,务必刷新maven下载jar包。
<dependencies>
<dependency>
<!-- 引入sparkjar -->
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.12</artifactId>
<version>2.4.6</version>
</dependency>
<dependency>
<!-- 引入scala依赖,就不用再手动添加插件了 -->
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.12.8</version>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-reflect</artifactId>
<version>2.12.8</version>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-compiler</artifactId>
<version>2.12.8</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<!-- scala的编译相关插件 -->
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.2.2</version>
<executions