2021 big data common language Scala: introduction to Scala

Posted by tycragg on Fri, 18 Feb 2022 01:12:33 +0100

1, Introduction to Scala

scala is a multi paradigm programming language running on the JVM. It supports both object-oriented and function oriented programming

  • Multi paradigm: it contains a variety of programming ideas. At present, there are four mainstream programming ideas, namely object-oriented, process-oriented, function oriented and generic
  • Function oriented one sentence Description: a function is also an object, which can be passed as a parameter.

That is:

Object oriented: the parameters passed are concrete objects or values

Functional programming: the parameter passed can be a function (processing logic)

  • Running on the JVM:

 

 

 

Scala program compilation and execution process

Just like learning various serializers in MapReduce For example, IntWritable and Text serialize the content, that is, the input is different and the output is the same Different inputs are serialized into the same format and run on hadoop Similarly, we can think that Scala and java are analogous to ScalaWritable and JavaWritable Responsible for serializing java and scala. What is the output? The output is our bytecode, that is class file Used to run on top of the JVM

 

 

 

In the early days, Scala didn't attract much attention when it first appeared. With the rise of big data frameworks based on Scala such as Spark and Kafka, Scala gradually entered the eyes of big data developers. The main advantage of scala is its expressiveness.

 

 

Why use scala

Develop big data applications (Spark Program, Flink program)

Spark ecology is not to replace Hadoop ecology, but to better expand the big data ecology

Spark is developed by Scala. We study Scala in order to learn spark better.

Strong expression ability, one line of code is equivalent to multiple lines of Java, and the development speed is fast. Elegant, code introduction and logic are clearer.

Compatible with Java, you can access huge Java class libraries, such as operating mysql, redis, freemaker, activemq and so on

 

Scala vs Java

 

 

Through the following case, compare the number of codes implemented in Java and Scala respectively

case

Define three entity classes (user, order and commodity)

Java code

/**
 * User entity class
 */
public class User {
    private String name;
    private List<Order> orders;

    public String getName() {
     return name;
    }

    public void setName(String name) {
     this.name = name;
    }

    public List<Order> getOrders() {
     return orders;
    }

    public void setOrders(List<Order> orders) {
     this.orders = orders;
    }
}

/**
 * Order entity class
 */
public class Order {
    private int id;
    private List<Product> products;

    public int getId() {
     return id;
    }

    public void setId(int id) {
     this.id = id;
    }

    public List<Product> getProducts() {
     return products;
    }

    public void setProducts(List<Product> products) {
     this.products = products;
    }
}

/**
 * Commodity entity
 */
public class Product {
    private int id;
    private String category;

    public int getId() {
     return id;
    }

    public void setId(int id) {
     this.id = id;
    }

    public String getCategory() {
     return category;
    }

    public void setCategory(String category) {
     this.category = category;
    }
}

 

Scala code

case class User(var name:String, var orders:List[Order]) // User entity class
case class Order(var id:Int, var products:List[Product]) // Order entity class
case class Product(var id:Int, var category:String)   // Commodity entity class

 

Topics: Big Data