大数据进阶之路——Scala入门
Posted 孙中明
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了大数据进阶之路——Scala入门相关的知识,希望对你有一定的参考价值。
概述
https://www.scala-lang.org/
Scala combines object-oriented and functional programming in one concise, high-level language. Scala’s static types help avoid bugs in complex applications, and its JVM and javascript runtimes let you build high-performance systems with easy access to huge ecosystems of libraries.
- 学习Scala的意义:
Spark、Kafka、Flink
优雅
开发速度快
融合到生态圈
安装
-
安装 Java 8
-
下载 download scala 网址:https://www.scala-lang.org/download/2.11.8.html
-
解压 unzip scala
-
配置环境变量(可选)
Windows 需配置两个 Path中: D:\\scala\\bin 和 D:\\scala\\jre\\bin
-
查看是否生效
Linux或Mac中操作步骤:
1.tar -zxvf scala-2.11.8.tgz -C 解压路径
2.到解压目录下 pwd 复制整个路径
3.将上面的路径 添加到环境变量中
vi ~/.bash_profile
export SCALA_HOME=复制的路径
export PATH=$SCALA_HOME/bin:$Path
保存
source ~/.bash_profile
echo $SCALA_HOME
下载之后的scala目录下的bin目录中 有普通文件 和 .bat文件
.bat文件是在Windows中用的,Linux或Mac中用不到,所以可以删掉 rm *.bat
Java VS Scala
Java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World..");
}
}
Scala(每行代码并不强求使用;结束,但是Java是必须的)
Object HelloWorld {
def main(args : Array[String]) {
println("Hello World..")
}
}
val 和 var
- val:值
final
val 值名称:类型=xx
val a = 1 (不可变)
val a : int = 1
- var:变量
var 值名称:类型=xxx
var b = 1
var b : int = 1
基本数据类型
- Byte/Char
- Short/Int/Long/Float/Double
- Boolean
只有Float声明时比较特别 - var c : Float = 1.1f
scala> b=20
b: Int = 20
scala> val b:Int =10
b: Int = 10
scala> val c:Boolean=true
c: Boolean = true
scala> val d =1.1
d: Double = 1.1
scala> val e:Float=1.2f
e: Float = 1.2
lazy在Scala中的应用
lazy var d : int = 1;
延迟加载,只有在第一次使用时才加载
读取文件并以字符串形式输出
import scala.io.Source._
var info = fromFile("...").mkString
如果用lazy var info = fromFile("…").mkString,开始是检测不到错误的,要小心使用
*注意:当一个变量声明为lazy,只有当你第一次操作时才会去真正访问,如果不去访问,即使写错了,也不会发现
开发工具IDEA
Maven
1.下载IDEA和Maven
2.进入IDEA,新建项目 选择Maven 勾选create from archetype 选择scala-archetype simple-> 正常创建(注意Maven仓库位置)
3.IDEA默认是不支持Scala的,需要下载Scala插件
File -> settings -> Plugins -> install JetBrains plugin -> scala
之后就可以new 一个Scala类了
4.新建测试类,运行报错
删除pom.xml中<arg>make:transitive</args>
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>untitled5</artifactId>
<version>1.0-SNAPSHOT</version>
<inceptionYear>2008</inceptionYear>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<encoding>UTF-8</encoding>
<scala.version>2.11.8</scala.version>
<spark.version>2.3.0</spark.version>
</properties>
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${scala.version}</version>
</dependency>
<!--引入Spark Core的依赖-->
<!-- https://mvnrepository.com/artifact/org.apache.spark/spark-core -->
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.11</artifactId>
<version>2.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.spark/spark-sql -->
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_2.12</artifactId>
<version>2.4.3</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/scala</sourceDirectory>
<testSourceDirectory>src/test/scala</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<version>2.15.0</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
<configuration>
<args>
<arg>-dependencyfile</arg>
<arg>${project.build.directory}/.scala_dependencies</arg>
</args>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.6</version>
<configuration>
<useFile>false</useFile>
<disableXmlReport>true</disableXmlReport>
<!-- If you have classpath issue like NoDefClassError,... -->
<!-- useManifestOnlyJar>false</useManifestOnlyJar -->
<includes>
<include>**/*Test.*</include>
<include>**/*Suite.*</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</project>
以上是关于大数据进阶之路——Scala入门的主要内容,如果未能解决你的问题,请参考以下文章