Spark learning -------- Scala acquaintance

Creative Commons License Copyright: Attribution, allow others to create paper-based, and must distribute paper (based on the original license agreement with the same license Creative Commons )

1. Why learn scala?

1) off-line calculation
2) online Scala calculated the Spark
a.spark bottom is scala, we need to look at the source code
b.scala java-based
development of high efficiency
running fast

2.scala Introduction

    Scala (Scala Language for short) is a language capable of performing general-purpose programming language for the JVM and .Net platforms. Both for large-scale application development, scripting can also be used, developed by Martin Odersk in 2001. 2004 began the implementation of the program on the JVM and .Net platform. Because of its simple, elegant, type-safe programming mode and attention.
    In the beginning of creation Scala, and not how to pay attention, along with Apache Spark and Apache Kafka so based on the rise of big data framework of the Scala , Scala gradually greeted by big data practitioners.
    scala is a multi-paradigm programming language, i.e., an object-oriented programming is a function of
object-oriented: Everything instance of an object class inherits object wrappers
as a function of process-oriented programming: Functional Programming

3.scala language and Java language comparison

The same point:
    1.java scala seamlessly and mixed, they are based on JVM
    2. both may call each other
except that:
    1. The type of automatic inference, and if the type of writing, is written after the variable name

var str = "hello"
var str:String = "hello"

    Var 2.val
        Val represents a constant
        var variable indicating
    3. functional programming support
    4. constructor different
    5.java may automatically default values, must be manually Scala to a default value
    6.scala semicolon is not required, but must be java there
    7.get set method to achieve their own
    age: the equivalent of java getter method of
    age_ $ eq: equivalent of java in the set method
    8.java only in the back with no return in return scala

4. install the compilation tools

1) the JDK
2) IDEA
3) plug-in installation (offline and online can be)
    scala-intellij-bin-2017.2.2.zip (a plug-in to get)
to create a scala item 4) test
5) fonts, backgrounds and tweak

5.main method explained

main(static=object)
1)语法
	关键字  方法名 (参数):返回值类型={
	方法体
	}
 def main(args: Array[String]): Unit = {

}

6. constants and variables

1) defined
        amount during operation, the value does not change, for example:: 3 letter A modified constant value val keyword
         amount during operation, the value of which may vary, for example:: variable time age modified keyword var
2) syntax
val name: type = variable value
var name: type = variable value
Here Insert Picture Description
Note 1:
        type can not be written, scala automatic type inference
Note 2:
        variable name must conform to naming conventions, the key can not be used word (as java naming and)
1. letters, underline, numbers, and can not begin with a number
Val 4_tablename = "T_USER";
2. the name of the variable name to do see EENOW
3 by a plurality of words naming nomenclature hump variable name consisting of
Val TableName = "T_USER";
4. keywords can not use the variable name
val def = 12

The data type 7.scala

Here Insert Picture Description
any all types superclass, has become the top type
anyVal (Value Type):
int Short byte Long Double String char Boolean Uint (. 9 th)
length. 4 2. 1. 8. 8. 4 2. 1
AnyRef (reference type):
List Map Option YourClass ...
Note: the
type is not the type of java and scala Nothing in the corresponding

8. lazy loading

1) scala lazy keywords used in modified variable, the variable is inert, lazy loading realize
Note:
         Inert variables can only be a constant, and only when you call the variable inertia, only to instantiate this variable
2) Case presentation
Here Insert Picture Description
3) the benefits of
using in the time-consuming business, such as a network disk IO IO scenarios:

9. The difference is

There are three scala interpolator
         1. In any string preceded s, can be used directly in the string variables
         2.f interpolator:
         3.raw interpolator: input string as it is, without escaping
Here Insert Picture Description

10. The access modifier

         private: a class available within
         protected: their own class subclasses can also be accessed (this is more stringent than the java, java with a package of other classes can also be accessed)
         public :: If no modifier, such a member in any place can be accessed
Note:
         in the case if not specified, it is public

Example 1 :( our only access)

 class Test01 {
    			  private val name = "kk"
    			  def main(args: Array[String]): Unit = {
    				println(name)
    			  }
    			}

Example 2

class Test01 {
    			  protected val name = "毕老师"
    			}			
class Test02 extends Test01 {
    			  def main(args: Array[String]): Unit = {
    				println(name)
    			  }
    			}	    					

Example 3:

object Test {
				  def main(args: Array[String]): Unit = {
					val test0 = new Test01
					println(test0.name)
				  }
				}

				class Test01 {
				   val name = "kk"
				}

11. Operator

1) arithmetic operators
2) relational operators
3) logical operators
4) assignment operator

Arithmetic operators: addition, subtraction modulo

object Test {
		   def main(args: Array[String]) {
			    val a = 100
				val b = 200
				val c = 250
				val d = 250
				println("a 加 b = " + (a + b))
				println("a 减 b = " + (a - b))
				println("a 乘 b = " + (a * b))
				println("b 除 a = " + (b / a))
				println("b 取余 a = " + (b % a))
		   }
		}		

Relational operators: == => <> = <=!

object Test {
    		   def main(args: Array[String]) {
    			    val a = 100
    				val b = 200
    				println("a 等于 b     是: " + (a == b))
    				println("a 不等于 b   是: " + (a != b))
    				println("a 大于 b     是: " + (a > b))
    				println("a 小于 b  	  是: " + (a < b))
    				println("b 大于等于 a 是: " + (b >= a))
    				println("b 小于等于 a 是: " + (b <= a))
    		   }
    		}	

The logical operators: && ||!

  object Test {
    		   def main(args: Array[String]) {
    			  var a = true;
    			  var b = false;
    			  println(a&&b)
    			  println(a||b)
    			  println(!a)
    		   }
    		} 

Assignment operators: + = = - = = * / =% =

		var a = 10;
		val b = 20;
		var c = 0;
		c = a + b;
		println(c);
		
		  var a = 10;    
		  var c = 0;
		  c += a ;
		  println( c );
		  
		  var a = 10;    
		  var c = 0;
		  c -= a ;
		  println( c );
		
		  var a = 10;    
		  var c = 0;
		  c *= a ;
		  println("c *= a = " + c );

		  val a = 10;
		  var c = 15;
		  c /= a ;
		  println("c /= a  = " + c );

		  val a = 10;
		  var c = 15;
		  c %= a ;
		  println("c %= a  = " + c );

12. Type Conversion

1) String type to Int

val age:String ="123"
    println(age.toInt.getClass.getName)

2) Int type to String

val b:Int =123
println(b.toString.getClass.getName)

3) String type to the type Float

val c:String ="123.123"
println(c.toFloat.getClass.getName)		

Question 1: How scala language accepts keyboard input?

    println("请输入姓名:")
	val int = Console.readLine()
	println("您的姓名是:"+int)		

Question 2: acquired ending string of letters
// Get the first character ??

"Hello"(0)
"Hello".take(1)

// Get the last character ??

"Hello".reverse(0)
"Hello".takeRight(1)

Question 4: random number generation?

val int = Random.nextInt(10)
println(int)

Guess you like

Origin blog.csdn.net/power_k/article/details/94754880