Spark Scala101
Spark Scala101
spark-shell
paste mode==
scala> :paste
// Entering paste mode (ctrl-D to finish)
print("hello")
print("3")
println("ejk")
println("33")
hello3ejk
33
......
=======Immutable type======\
val msg:String="hello"
msg:String ="World"
print(msg)
O/p=Error
======type conversion====
Float => double ; but double cant be converetd into float
Eg= type casting Long to Float
val oldtype:Long=89556652556
val newtype:Float = oldtype
2.Relational Operator
val a=5
val b=3
println(a>b)
println(a<b)
println(a>=b)
println(a<=b)
println(a!=b)
3. Logical operator===
val A = true
val B = false
val exp = A && B
val exp1 = A || B
O/P= A: Boolean = true
B: Boolean = false
exp: Boolean = false
4. assignment operation===
A=
A+=B
A = A+B
Eg-
val A = 10
val B = 30
val A -= B
O/P = ERROR
var A = 10
var B = 12
A -= B
A: Int = -2
B: Int = 12
5.Bitwise operator
val A = 5
val B = 12
print(A & B)
O/P =res4: Int = 4
###function call
objectname.methodname(parameter)
OR
objectName Methodname parameter
Eg=>
val string1 ="Hello World"
val result = string1.indexOf('W') O/P=> 6 =counts from 0
OR, string1 indexOf'W'
SPARK COMMANDS
spark-shell
paste mode==
scala> :paste
// Entering paste mode (ctrl-D to finish)
print("hello")
print("3")
println("ejk")
println("33")
hello3ejk
33
......
=======Immutable type======\
val msg:String="hello"
msg:String ="World"
print(msg)
O/p=Error
======type conversion====
Float => double ; but double cant be converetd into float
Eg= type casting Long to Float
val oldtype:Long=89556652556
val newtype:Float = oldtype
2.Relational Operator
val a=5
val b=3
println(a>b)
println(a<b)
println(a>=b)
println(a<=b)
println(a!=b)
3. Logical operator===
val A = true
val B = false
val exp = A && B
val exp1 = A || B
O/P= A: Boolean = true
B: Boolean = false
exp: Boolean = false
4. assignment operation===
A=
A+=B
A = A+B
Eg-
val A = 10
val B = 30
val A -= B
O/P = ERROR
var A = 10
var B = 12
A -= B
A: Int = -2
B: Int = 12
5.Bitwise operator
val A = 5
val B = 12
print(A & B)
O/P =res4: Int = 4
###function call
objectname.methodname(parameter)
OR
objectName Methodname parameter
Eg=>
val string1 ="Hello World"
val result = string1.indexOf('W') O/P=> 6 =counts from 0
OR, string1 indexOf'W'
Eg1:
def sayhello(){
print("hello")}
O/P= sayhello: ()Unit
Eg2:
def sum(a:Int, b:Int){
print(a+b)
}
calling sum fucntion = sum(2,3)
Eg3:
def checkEven(a:Int): Boolean = {
if (a%2 ==0){
true}
else {false}
}
Eg3:
def maxNum(a:Int, b:Int){
if(a>b){
print(s"$a is maximum")}
else {
print(s"$b is maximum")
}
}
====String concatenation===
3.with help of raw=doesnt recognise any coding symbols inside it, it prints it as
plain text
println("without raw:\nfirst\nSecond")
println(raw"with raw\n\n\n")
======User Input=====
import scala.io.StdIn.readLine
val myInput = readLine() ==>>by defalut String type input
Q.
import scala.io.StdIn.readLine
val name = readLine()
val age = readInt()
if(age<19 && age>13){
print("You are a teenager")
}
else if(age<59 && age>20){
print("You are an adult")
}else if(age>60){
print("You are a senior")}
else{
print("invalid age")}
=====String equality====
val str1="Hi"
val str2="hi"
val c = str1==str2
print(c) O/P=false
if (str1 eq str2) {
println("str1 and str2 reference the same object")
} else {
println("str1 and str2 do not reference the same object")
}
==>In this example, str1 and str2 have the same content, but they are different
objects.
The eq method checks whether they reference the same object in memory.
======Multiline Strings===
val name:String="""hello world
first string
second """"
=====Splitting String===
val pizza="Pizza dough, tomato sauce,Cheese, Toppings"
val splitPizza = pizza.split(",") O/p=it will return the values in a array of
string
splitPizza.foreach(println)
====Regular Expression===
* = 0 or any number
+ = 1 or anu number
"ab*c"= starts with a and ends with c. in middle b can be of any number even 0
"ab+c"= starts with a and ends with c
in middle b can be of any number but not 0
ab{2}c =b will come exactly two times =abbc. curly for exact number
ab{2,5}c=b can be 2 to 5
[abc]= any letter among them= a or b or c
[abc]+= anything combination of abc. a,b,c,ab,bc,ba,abc,acb,bca,cba
[a-zA-Z]= any letter between a-z andA-Z
[1-9]=any num between 1=9 and combination of them
val reg="ab*c".r
eg=
var var1="the".r //rxpression
var var2="the big data course" //String to frind from
var var3=var1.findFirstIn(var2)
var3.foreach(println)
O/P=the
eg=
var v1="[1-5]+".r //combination of any number
var v2="12 67 93 48 51"
var v3=v1.findAllIn(v2)
v3.foreach(println)
O/p=12,3,4,51
eg=
var v1="[1-5]{2}+".r //combination of only two num
var v2="12 67 93 48 51"
var v3=v1.findAllIn(v2)
v3.foreach(println)
O/p=12,51
eg=
var v1="8201530"
var v2=v1.replaceFirst("[01]","x") //replaced first 0 or 1 with x
println(v2)
O/p=82x1530
v1: String = 8201530
v2: String = 82x1530
Eg=
var var1="H".r
var var2="Hello woirld"
var var3=var1.replaceFirstIn(var2,"J")
var3.foreach(println)
O/p=var2: String = Hello woirld
var3: String = Jello woirld
eg=
var v1="8201530"
var v2=v1.replaceAllIn("[01]","x") //replaced first 0 or 1 with x
println(v2)
Eg=
var reg="[a=-z]+".r
var replaceIn="dk79rx5c44lj2cbge"
var replaced=reg.replaceAllIn(replaceIn,"1")
println(replaced)
O/p=17915144121
Eg=
val str1="This is Scala"
val str2="Hello Scala"
val str3="Hello Scala"
val compare=str1.compareTo(str2)