1、Scala高级语言
1.1 Scala简介
Scala是一门多范式(multi-paradigm)的编程语言,设计初衷是要集成面向对象编程和函数式编程的各种特性。 Scala运行在Java虚拟机上,并兼容现有的Java程序。 Scala源代码被编译成Java字节码,所以它可以运行于JVM之上,并可以调用现有的Java类库。 函数编程范式更适合用于Map/Reduce和大数据模型,着眼于函数本身,函数范式逻辑清晰、简单,非常适合用于处理基于不变数据的批量处理工作。 像Spark,kafka等都是采用Scala开发的,所以学习好大数据,掌握scala是必要的。Flink是带有scala的api支持。 官网:The Scala Programming Language
1.2 Scala安装部署
1、Scala需要Java运行时库,安装Scala需要首先安装JVM虚拟机,推荐安装JDK8。
2、进入Scala的官网The Scala Programming Language 下载页面,点击 All Previous Releases
3、选择 Scala 2.12.8
4、根据操作系统选择安装包
1.2.1 Windows下的环境配置
首先下载scala-2.12.8.msi 或 scala-2.12.8.zip都可以(二选一)。
1.msi是Windows下的安装文件,直接安装。一直下一步无需配置即可安装完成。 2.zip是压缩文件,只需要将压缩包中的文件解压到任意盘符即可
配置scala的环境变量:window左下角搜索环境变量--->编辑环境变量--->系统环境变量--->添加到path下面--->一步一步确定完即可。 scala环境变量不配置:记住对应scala安装位置。 然后新建“SCALA_HOME”环境变量,并将“%SCALA_HOME%\bin”追加到"%Path%"中。
1.2.2 Linux下的环境配置(选配)
在官方网址进入下载界面下载 scala-2.12.8.tgz
#通过命令tar -xvf scala-2.12.8.tar -C 解压到路径
#例如:
tar -xvf scala-2.12.8.tgz -C /usr/java
#在配置系统环境变量,将Scala
vi /etc/profile
#添加如下参数
export JAVA_HOME=Java安装目录
export PATH=$PATH:$JAVA_HOME/bin:/scala解压目录/bin
#例如:
export JAVA_HOME=/usr/java/jdk1.8.0_251
export PATH=$PATH:$JAVA_HOME/bin:/usr/local/scala-2.12.8/bin
#保存退出后重新加载
source /etc/profile
1.3 Scala解释器
Scala解释器也被称为REPL交互式编码环境,会快速编译scala代码为字节码,然后交给JVM来执行。
REPL:Read(取值)-> Evaluation(求值)-> Print(打印)-> Loop(循环)。其特点如下:
# 1.计算表达式:在"scala>"命令行内,键入scala代码,解释器会直接返回结果给你。如果你没有指定变量来存放这个值,那么值默认的名称为res,而且会显示结果的数据类型,比如Int、Double、String等等。
例如,输入:1 + 1,会看到
res0: Int = 2
# 2.内置变量:在后面可以继续使用res这个变量,以及它存放的值。
例如,2.0 * res0,返回
res1: Double = 4.0
例如,"Hi, " + res0,返回
res2: String = Hi, 2
# 3.自动补全:在scala>命令行内,可以使用Tab键进行自动补全。
在Windows下使用cmd命令进入DOS中,然后输入“scala”进入到REPL。
请编写你的第一条scala代码:
println("hello world")
1.4 IDEA集成Scala开发环境配置
目前Scala的开发工具主要有两种:Eclipse和IDEA,这两个开发工具都有相应的Scala插件,如果使用Eclipse,直接到Scala官网下载即可Download Scala IDE for Eclipse - Scala IDE for Eclipse。
由于IDEA的Scala插件更优秀,大多数Scala程序员都选择IDEA,可以到Download IntelliJ IDEA – The Leading Java and Kotlin IDE 下载社区免费版,点击下一步安装即可。
1.IDEA在线安装Scala插件 打开IDEA--->左上脚File--->Settings--->选中Plugins--->选择MarketPlace--->搜索Scala--->点击绿色的Install按钮--->等待联网下载安装--->安装完成重启IDEA。 如果再MarketPlace搜索不到Scala,则选择使用离线方式安装Scala插件,即参考"2.IDEA离线安装Scala插件(可选)"。
2.IDEA离线安装Scala插件(可选)
因为IDEA的插件下载网站是国外网站,所以建议在线下载后离线安装。建议通过IDEA插件网站进行下载。
插件网址:JetBrains Marketplace |
打开IDEA--->左上脚File--->Settings--->选中Plugins--->选中此轮状设置图标--->选择Install pulgin from disk... --->选择已经下载的插件压缩包即可--->选择restart IDEA。
安装Maven Maven是一个企业级Java项目的依赖和构建管理工具。 将maven安装包解压到某一个安装目录--->配置Maven的环境变量--->配置conf/settings.xml--->如果配置环境变量:测试是否安装好(进入cmd--->mvn) --->如果没有配置环境变量:测试是否安装好(进入cmd,走到maven解压目录下的bin目录--->执行./mvn) 。 关于settings.xml配置如下:
-
修改如下配置中的第55行,将/opt/mavenrepository/替换成已经创建一个目录即可。
-
添加如下配置中的159-164(含)这一段配置到你你的配置文件中的159行位置即可。
<?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <!-- | This is the configuration file for Maven. It can be specified at two levels: | | 1. User Level. This settings.xml file provides configuration for a single user, | and is normally provided in ${user.home}/.m2/settings.xml. | | NOTE: This location can be overridden with the CLI option: | | -s /path/to/user/settings.xml | | 2. Global Level. This settings.xml file provides configuration for all Maven | users on a machine (assuming they're all using the same Maven | installation). It's normally provided in | ${maven.conf}/settings.xml. | | NOTE: This location can be overridden with the CLI option: | | -gs /path/to/global/settings.xml | | The sections in this sample file are intended to give you a running start at | getting the most out of your Maven installation. Where appropriate, the default | values (values used when the setting is not specified) are provided. | |--> <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> <!-- localRepository | The path to the local repository maven will use to store artifacts. | | Default: ${user.home}/.m2/repository <localRepository>/path/to/local/repo</localRepository> --> <localRepository>/opt/mavenrepository/</localRepository> <!-- interactiveMode | This will determine whether maven prompts you when it needs input. If set to false, | maven will use a sensible default value, perhaps based on some other setting, for | the parameter in question. | | Default: true <interactiveMode>true</interactiveMode> --> <!-- offline | Determines whether maven should attempt to connect to the network when executing a build. | This will have an effect on artifact downloads, artifact deployment, and others. | | Default: false <offline>false</offline> --> <!-- pluginGroups | This is a list of additional group identifiers that will be searched when resolving plugins by their prefix, i.e. | when invoking a command line like "mvn prefix:goal". Maven will automatically add the group identifiers | "org.apache.maven.plugins" and "org.codehaus.mojo" if these are not already contained in the list. |--> <pluginGroups> <!-- pluginGroup | Specifies a further group identifier to use for plugin lookup. <pluginGroup>com.your.plugins</pluginGroup> --> </pluginGroups> <!-- proxies | This is a list of proxies which can be used on this machine to connect to the network. | Unless otherwise specified (by system property or command-line switch), the first proxy | specification in this list marked as active will be used. |--> <proxies> <!-- proxy | Specification for one proxy, to be used in connecting to the network. | <proxy> <id>optional</id> <active>true</active> <protocol>http</protocol> <username>proxyuser</username> <password>proxypass</password> <host>proxy.host.net</host> <port>80</port> <nonProxyHosts>local.net|some.host.com</nonProxyHosts> </proxy> --> </proxies> <!-- servers | This is a list of authentication profiles, keyed by the server-id used within the system. | Authentication profiles can be used whenever maven must make a connection to a remote server. |--> <servers> <!-- server | Specifies the authentication information to use when connecting to a particular server, identified by | a unique name within the system (referred to by the 'id' attribute below). | | NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are | used together. | <server> <id>deploymentRepo</id> <username>repouser</username> <password>repopwd</password> </server> --> <!-- Another sample, using keys to authenticate. <server> <id>siteServer</id> <privateKey>/path/to/private/key</privateKey> <passphrase>optional; leave empty if not used.</passphrase> </server> --> </servers> <!-- mirrors | This is a list of mirrors to be used in downloading artifacts from remote repositories. | | It works like this: a POM may declare a repository to use in resolving certain artifacts. | However, this repository may have problems with heavy traffic at times, so people have mirrored | it to several places. | | That repository definition will have a unique id, so we can create a mirror reference for that | repository, to be used as an alternate download site. The mirror site will be the preferred | server for that repository. |--> <mirrors> <!-- mirror | Specifies a repository mirror site to use instead of a given repository. The repository that | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used | for inheritance and direct lookup purposes, and must be unique across the set of mirrors. | <mirror> <id>mirrorId</id> <mirrorOf>repositoryId</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>http://my.repository.com/repo/path</url> </mirror> --> <mirror> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror> </mirrors> <!-- profiles | This is a list of profiles which can be activated in a variety of ways, and which can modify | the build process. Profiles provided in the settings.xml are intended to provide local machine- | specific paths and repository locations which allow the build to work in the local environment. | | For example, if you have an integration testing plugin - like cactus - that needs to know where | your Tomcat instance is installed, you can provide a variable here such that the variable is | dereferenced during the build process to configure the cactus plugin. | | As noted above, profiles can be activated in a variety of ways. One way - the activeProfiles | section of this document (settings.xml) - will be discussed later. Another way essentially | relies on the detection of a system property, either matching a particular value for the property, | or merely testing its existence. Profiles can also be activated by JDK version prefix, where a | value of '1.4' might activate a profile when the build is executed on a JDK version of '1.4.2_07'. | Finally, the list of active profiles can be specified directly from the command line. | | NOTE: For profiles defined in the settings.xml, you are restricted to specifying only artifact | repositories, plugin repositories, and free-form properties to be used as configuration | variables for plugins in the POM. | |--> <profiles> <!-- profile | Specifies a set of introductions to the build process, to be activated using one or more of the | mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/> | or the command line, profiles have to have an ID that is unique. | | An encouraged best practice for profile identification is to use a consistent naming convention | for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc. | This will make it more intuitive to understand what the set of introduced profiles is attempting | to accomplish, particularly when you only have a list of profile id's for debug. | | This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo. <profile> <id>jdk-1.4</id> <activation> <jdk>1.4</jdk> </activation> <repositories> <repository> <id>jdk14</id> <name>Repository for JDK 1.4 builds</name> <url>http://www.myhost.com/maven/jdk14</url> <layout>default</layout> <snapshotPolicy>always</snapshotPolicy> </repository> </repositories> </profile> --> <!-- | Here is another profile, activated by the system property 'target-env' with a value of 'dev', | which provides a specific path to the Tomcat instance. To use this, your plugin configuration | might hypothetically look like: | | ... | <plugin> | <groupId>org.myco.myplugins</groupId> | <artifactId>myplugin</artifactId> | | <configuration> | <tomcatLocation>${tomcatPath}</tomcatLocation> | </configuration> | </plugin> | ... | | NOTE: If you just wanted to inject this configuration whenever someone set 'target-env' to | anything, you could just leave off the <value/> inside the activation-property. | <profile> <id>env-dev</id> <activation> <property> <name>target-env</name> <value>dev</value> </property> </activation> <properties> <tomcatPath>/path/to/tomcat/instance</tomcatPath> </properties> </profile> --> </profiles> <!-- activeProfiles | List of profiles that are active for all builds. | <activeProfiles> <activeProfile>alwaysActiveProfile</activeProfile> <activeProfile>anotherAlwaysActiveProfile</activeProfile> </activeProfiles> --> </settings>
测试是否可用,进入cmd中输入mvn即可。
IDEA整合Maven
-
打开IDEA--->左上脚File--->Settings--->搜索Maven--->选择Build tools下的Maven选项--->其它如下图
-
为以后的每一个新项目配置maven信息
左上脚搜索Maven--->选择Build tools下的Maven选项--->其它如下图
课上练习:请编写你的第一个Scala程序HelloWorld 打开IDEA--->点击左上脚File--->new--->new project--->如下图所示
我在this window展示项目如下
到此为止,maven类型的工程创建好。
IDEA整合ScalaSDK
1、hn-bigdata--->main--->new --->directory--->新建一个scala目录。 2、idea左上角File--->project structure..--->如下图所示
到此为止,我们创建hn-bigdata已经和scala SDK整合好。
创建Scala第一个代码 1、选择hn-bidata下的scala包--->右键--->new--->package--->创建com.qianfeng.scala的包 2、在scala包中创建Demo01_FirstScala--->代码如下
package com.qianfeng.scala
/**
* 第一个scala代码
*/
object Demo01_FirstScala {
def main(args: Array[String]): Unit = {
println("hello world!!!")
val a = 1
val b = 2
println(a+b)
}
}
右键运行上述代码即可。