Scala environment construction and installation

salca environment setup

       Since scala is developed based on java, the written java class can be compiled into a .class file using the javac command and loaded into the memory by the JVM for execution! Then scala can be compiled into a .class file through the scalac command and loaded by the JVM. into memory, so Scala runs on the JVM platform, so you need to install JDK before installing Scala!

 1. Install the Scala compiler on Windows

method one

Visit the Scala official website http://www.scala-lang.org/ to download the Scala compiler installation package. The latest version is 3.1.3, but most current frameworks are written and developed using 2.12.x, and Spark3.x uses The version is 2.12.x, so the 2.12.x version is recommended here. Download scala-2.12.11.msi and click Next!

Method 2

Unzip and configure system environment variables

 Enter scala in the windows console, and the following message is displayed, indicating that the windows environment is completed!

 

 Note 1: The decompression path cannot have any Chinese path, and it is best not to have spaces.

Note 2: The environment variable must be capitalized SCALA_HOME

 

test:

Requirement: Calculate the sum of two numbers a and b.

step:

1. Press the win+r keys simultaneously on the keyboard and enter the cmd command in the run window

2. Enter Scala and press Enter to start the Scala environment. Then define two variables and calculate the sum.

 Example: Write a wordCount

import scala.io.Source

Source.fromFile("C:\\Users\\yi\\Desktop\\wc.txt")
.getLines
.toList
.flatMap(_.split(","))
.groupBy(w=>w)
.map(t=>(t._1,t._2.size))
.foreach(println)

 2. Install Scala compiler on Linux

  1. After downloading, upload the Scala file to a specific directory on Linux

  2. Unzip

  3. Configure environment variables and add scala to PATH

 vi /etc/profile

export SCALA_HOME=/opt/app/scala/scala-2.12.11

export PATH=$PATH:$JAVA_HOME/bin:/usr/java/scala-2.12.11/bin

 The test verifies that the scala installation is complete. After typing scala anywhere, the scala command line is displayed, and then the addition of two numbers is tested in it.

 3.scala installation plug-in

Method 1: Offline installation

Open the official website of jetbrains: http://www.jetbrains.com and click Develop Tools and select scala.

 

 Then select the corresponding version of the idea to download the plug-in (Just DownLoad)

 After opening the idea, find File in the upper left corner --》settings --》plugins

 Click Settings and select Install Plugin from Disk... and then select the plug-in you just downloaded. Click OK and restart the idea.

 Method 2: Online installation

After opening the idea, find File in the upper left corner --》settings --》plugins--》Search for scala, install it, and then restart the idea.

 

 4 Create scala project and write HelloWorld case

  1. Create a maven project

  2. Create a scala package in src--"main--"

  3. Right-click on the scala package and select Mark Directory as --》 Sources Root

4. Right-click the project name--"Add Framework Support...

 5. Go in and select scala, then add the scala sdk and click ok to create the scala class.

 

Write scala class

  1. scala files end with .scala

  2. The naming convention in scala is consistent with that in java

  3. The main method in scala should be written in the object-modified class

  4. The execution process of Scala is similar to that of Java. It is compiled first and then executed, so there are compile-time error derivation.

  5. It is best not to write the semicolon at the end of the line in scala code to reflect its simplicity.

package com.doit.day01
object HelloWorld {
  def main(args: Array[String]): Unit = {
    println("学大数据来多易")
    System.out.println("java中的一部分代码在scala中也是可以运行的")
  }
}

Guess you like

Origin blog.csdn.net/m0_53400772/article/details/131195378
Recommended