0% found this document useful (0 votes)
14 views8 pages

Spark Scala101

This document discusses Spark commands and various operations that can be performed on strings in Scala like concatenation, interpolation, splitting, regular expressions, comparison and more. It also covers user-defined functions, control flow, collections and working with different data types.

Uploaded by

jahnabi122
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views8 pages

Spark Scala101

This document discusses Spark commands and various operations that can be performed on strings in Scala like concatenation, interpolation, splitting, regular expressions, comparison and more. It also covers user-defined functions, control flow, collections and working with different data types.

Uploaded by

jahnabi122
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 8

SPARK COMMANDS

spark-shell

paste mode==
scala> :paste
// Entering paste mode (ctrl-D to finish)

print("hello")
print("3")
println("ejk")
println("33")

// Exiting paste mode, now interpreting.

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

=======BUT float to long===


O/p== Eroor in type mismatch

A+B=A and B are operands and + is operator

======working with operators==


1.Arithmetic
val a=5
val b=3
println(a+b)
println(a-b)
println(a*b)
println(a/b) =>>> 5/7 =0; 7/5=1; 5%7=5; 7%5=2
println(a%b)

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

####Creating variable in two ways


val 1stvariable:Int = 5
OR
var 2ndvariable = 5

###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'

##USER DEFINED FUNCTIONS IN SCALA


def functionName(parameters1:datatype,

SPARK COMMANDS
spark-shell

paste mode==
scala> :paste
// Entering paste mode (ctrl-D to finish)

print("hello")
print("3")
println("ejk")
println("33")

// Exiting paste mode, now interpreting.

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

=======BUT float to long===


O/p== Eroor in type mismatch

A+B=A and B are operands and + is operator

======working with operators==


1.Arithmetic
val a=5
val b=3
println(a+b)
println(a-b)
println(a*b)
println(a/b) =>>> 5/7 =0; 7/5=1; 5%7=5; 7%5=2
println(a%b)

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

####Creating variable in two ways


val 1stvariable:Int = 5
OR
var 2ndvariable = 5

###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'

##USER DEFINED FUNCTIONS IN SCALA


def functionName(parameters1:datatype,

def functionName(parameters1:datatype, parameters2:datatype) : return datatype ={


function body
}
default return tye is()unit .

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)

###function with parameter and return type


def sum(a:Int, b:Int):Int={
return (a+b)
}
sum(2,9) O/P=> res6: Int = 11

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")
}
}

==>>declaring string without typeinference


val myFIrstString:String="Hello"
==>>with type inference
val myFIrstString = "Hello"
=>scala supports few java methods
myFirstString.length() O/P=5

====String concatenation===

=====String interpolatyion=====it is the ability to create a new string or modify


existing ones by embedding some expression with it
1.with help of s
val a=4
val b=5
print(s"a+b is ${a + b}") O/P =a+b is 9

2.with help of f= format specifier(%d,%f,%s)


val pi=3.14159F
println(f"the value of pi is $pi%.2f")

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")}

==>>// Convert the input to an integer


val age = ageStr.toInt

=====String equality====
val str1="Hi"
val str2="hi"
val c = str1==str2
print(c) O/P=false

===>>It's important to note that == in Scala is used for value equality.


If you want to check reference equality (whether two variables reference the same
object),
you can use the eq method:

val str1: String = "Hello"


val str2: String = new String("Hello")

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

===Find and replace==


str.replaceFirst("search expression","zx")
regex.replaceFirstIn
str.replaceAll("Search expression","replace expression")
regex.replaceAllIn()
str.replaceFirst("av","zx")= find first occurence of av in str and replace it wil
zx
str.findFirst("search expression")

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

===Methods for comparing Strings===


1.variableName.matches(regular expression)
2.variableName.equals(regular expression)
3.String1.compareTo(String2)
4.variableName.equalsIgnoreCase(regular expression) //scala is case sensitive
EG=
val str1="ABC"
val str2="abc"
val comparingStrings=str1.equals(str2)

Eg=
val str1="This is Scala"
val str2="Hello Scala"
val str3="Hello Scala"
val compare=str1.compareTo(str2)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy