Scala Training
Scala Training
• Day 1:
• Introduction to Scala
• Scala vs Java
• Installation and setup Scala
• Datatypes, variables
• Conditions and Loops statements
• Functions
High order functions
Anonymous functions
Partially applied functions
Function with variable arguments
Function call-by-name
• Access Modifiers
• Day 2:
• Basic object orient programming
Classes
Immutable and Mutable Fields
Methods
Default and Named Arguments
Objects
• Extractors
• Pattern matching
• Day 3:
• Exception handling
• File handling
• Introduction to Spark
Spark architecture
Introduction to Big Data and Hadoop
Spark Framework
• RDD Vs DSM
• RDD’s Features
• RDD Transformation and operations
• Day 4:
• Hands on program for RDD
Data Loading and Saving Through RDDs
Key-Value Pair RDDs and Other Pair RDDs
RDD Persistence
RDD Partitions
RDD Lineage
• Data frames and Spark SQL
• Spark SQL architecture
• Data Frames & Datasets
• JSON and Parquet File Formats(loading data into Hive)
• Loading Data through Different Sources
• Day 5:
• Uses cases/Sample project
• Code and development
Business Problem
Decline in the no. of transactions on a Bank’s credit/
debit cards.
He receives
an SMS
Get 2% off on your next purchase in the
Jewelry store within 20 minutes.
Wrong Place
Customer is attending
Nature’s call (7 AM)
GPS location
Loyalty preferences
What it means in terms of BIG DATA…
GPS location Feeds from target segments: Mon-Fri ( 7 PM -11PM), Sat-Sun (10 AM -11PM)
0.01 Terabyte of data to be processed within few seconds during weekends, 0.005 TB of data in
off-peak hours
Purchase History
0.02 TB of unstructured social media data to be
analyzed within few seconds.
if( x == 10 ){
println("Value of X is 10");
} else if( x == 20 ){
println("Value of X is 20");
} else if( x == 30 ){
println("Value of X is 30");
} else{
println("This is else statement");
}
}
While loop:
object Demo {
def main(args: Array[String]) {
var i=0
while (i<10)
{
println("i value is "+i)
i+=1
}
}
}
Do while loop:
object Helloworld {
object Demo {
def main(args: Array[String])
{
val result = for (i <- 1 to 10; j <- 1 to 10) yield i+j
println(result)
}
}
Functions:
• def functionName ([list of parameters]) : [return type] = {
• function body
• return [expr]
• }
• scala> def myFirstfun() = { “This is a sample function" }
• myFirstMethod: ()java.lang.String
• scala> myFirstfun()
• res1: java.lang.String = This is a sample function
• ---------------------------------------------------------------------------------------
• scala> def myFirstfun(){ “This is a sample function" }
• myFirstfun : ()Unit
• scala> myFirstfun()
Function with arguments
object Demo {
def main(args: Array[String])
{
println("before function")
myfun(30,"Sara")
def myfun(Age:Int, Name:String): Unit =
{
println("My name is :" +Name)
println("Age is "+Age)
}
}
}
Named parameters
object Demo {
def main(args: Array[String])
{
println("before function")
myfun(30)
myfun(Age = 10, Name = "xyz")
def myfun(Age:Int, Name:String = "unknown"): Unit =
{
println("My name is :" +Name)
println("Age is "+Age)
}
}
}
Functions with variable arguments
object Demo {
def main(args: Array[String]) {
printStrings("Java", "Scala", "Python");
}
object Demo {
def main(args: Array[String]) {
delayed(time());
}
def time() = {
println("Getting time in nano seconds")
System.currentTimeMillis()
}
def delayed( t: => Long ) = {
println("In delayed method")
println("Param: " + new Date(t) )
}
}
Higher-order Function
object Demo {
def main(args: Array[String]) {
println( apply( layout, sumInts(1,3)) )
println(sumInts(1,3))
}
def sumInts(a: Int, b: Int) = sum(id, a, b)
def apply(f: Int => String, v: Int) = f(v)
def id(x: Int): Int = x
def layout[A](x: A) = "[" + x.toString() + "]"
def sum(f: Int => Int, a: Int, b: Int): Int =
if (a > b) 0
else f(a) + sum(f, a + 1, b)
}
Anonymous function
• scala> increase = (x: Int) => {
• println("We")
• println("are")
• println("here!")
• x+1
•}
• increase: (Int) => Int =
<function>
Partially called functions
import java.util.Date
object Demo {
def main(args: Array[String]) {
val date = new Date
val logWithDateBound = log(date, _ : String)
logWithDateBound("message1" )
Thread.sleep(1000)
logWithDateBound("message2" )
Thread.sleep(1000)
logWithDateBound("message3" )
}
object Demo {
def main(args: Array[String]) {
var closure_val = 10
def addclosure(x:Int): Unit =
{
println(x+closure_val)
}
addclosure(1)
closure_val = 100
addclosure(1)
}
}
Class
Classes in Scala are static templates that can be instantiated into many
objects at runtime
A Class can contain information about:
• Fields
• Constructors
• Methods
• Superclasses(inheritance)
• Interfaces implemented by the class, etc.
Example
object Demo {
def main(args: Array[String]) {
class addition{
var x:Int=10
def add(a:Int,b:Int)
{
x = x+a
println("Value of x after modification :"+ x)
}
}
val p=new addition()
p.add(5,10);
}
}
class Employee {
}
class Point(xc: Int, yc: Int) {
var x: Int = xc
var y: Int = yc