scala安装使用教程_一篇搞定!

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

  1. 打开IDEA--->左上脚File--->Settings--->搜索Maven--->选择Build tools下的Maven选项--->其它如下图

image.png

  1. 为以后的每一个新项目配置maven信息

image.png

左上脚搜索Maven--->选择Build tools下的Maven选项--->其它如下图

image.png

课上练习:请编写你的第一个Scala程序HelloWorld 打开IDEA--->点击左上脚File--->new--->new project--->如下图所示

image.png

image.png

我在this window展示项目如下

image.png

到此为止,maven类型的工程创建好。

IDEA整合ScalaSDK

1、hn-bigdata--->main--->new --->directory--->新建一个scala目录。 2、idea左上角File--->project structure..--->如下图所示

image.png

image.png

到此为止,我们创建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)
  }
}

右键运行上述代码即可。

  • 20
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 要在Linux上安装Scala,您可以按照以下步骤进行操作: 1. 首先,确保您的系统已经安装了Java。您可以在终端中输入“java -version”来检查Java是否已经安装。 2. 下载Scala二进制文件。您可以在Scala官方网站上下载最新版本的Scala二进制文件。 3. 解压Scala二进制文件。您可以使用tar命令来解压Scala二进制文件。 4. 将Scala二进制文件添加到系统路径中。您可以在.bashrc文件中添加Scala路径,以便在终端中使用Scala命令。 5. 检查Scala是否已经成功安装。您可以在终端中输入“scala -version”来检查Scala是否已经成功安装。 希望这些步骤能够帮助您在Linux上安装Scala。 ### 回答2: 要在Linux上安装Scala,可以按照以下步骤进行操作: 1. 首先,确保在Linux系统上已经安装了Java JDK。打开终端,并输入以下命令来检查是否已经安装Java: ``` java -version ``` 如果没有输出Java版本信息,那么需要先安装Java。可以执行以下命令来安装OpenJDK: ``` sudo apt-get install openjdk-8-jdk ``` 2. 安装Scala需要使用Scala编译器和构建工具sbt。执行以下命令来下载和安装sbt: ``` curl https://bintray.com/sbt/rpm/rpm | sudo tee /etc/yum.repos.d/bintray-sbt-rpm.repo sudo yum install sbt ``` 3. 现在可以下载和安装Scala编译器了。打开终端,并执行以下命令来下载Scala安装包: ``` wget https://downloads.lightbend.com/scala/2.13.6/scala-2.13.6.rpm ``` 4. 执行以下命令来安装Scala: ``` sudo yum install scala-2.13.6.rpm ``` 5. 安装完成后,可以通过执行以下命令来验证Scala是否正确安装: ``` scala -version ``` 如果成功安装,将会显示Scala的版本信息。 现在你已经成功在Linux系统上安装Scala。你可以开始使用Scala编写和运行程序了。 ### 回答3: 要在Linux上安装Scala,您可以按照以下步骤进行操作: 1. 打开终端,可以使用Ctrl+Alt+T快捷键。 2. 确保系统已安装Java运行环境(JRE)。您可以通过在终端中输入"java -version"来检查是否已安装。 3. 在终端中输入以下命令以下载Scala二进制分发版: ``` wget https://downloads.lightbend.com/scala/2.13.6/scala-2.13.6.tgz ``` 4. 解压下载的文件。在终端中输入以下命令: ``` tar -xvf scala-2.13.6.tgz ``` 5. 将解压后的Scala文件夹移动到适当的位置。您可以将其移动到/opt目录下,以便所有用户都可以访问。在终端中输入以下命令: ``` sudo mv scala-2.13.6 /opt/scala ``` 6. 配置环境变量,以便系统可以找到Scala。在终端中打开配置文件。例如,如果您使用的是bash shell,请运行以下命令: ``` sudo nano ~/.bashrc ``` 7. 在文件末尾添加以下行: ``` export PATH=$PATH:/opt/scala/bin ``` 8. 保存并关闭文件。按Ctrl+X,然后按Y。 9. 更新环境配置。在终端中输入以下命令: ``` source ~/.bashrc ``` 10. 现在Scala已经成功安装在您的Linux系统上。您可以在终端中输入以下命令来验证是否安装成功: ``` scala -version ``` 如果成功安装,您将看到Scala的版本信息。 希望以上步骤能帮助您在Linux上成功安装Scala
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Guff_hys

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值