Professional Documents
Culture Documents
Big Data analytics is the process of collecting, organizing and analyzing large sets of
data (called Big Data) to discover patterns and other useful information. Big Data
analytics can help organizations to better understand the information contained within
the data and will also help identify the data that is most important to the business and
future business decisions. Analysts working with Big Data typically want
the knowledge that comes from analyzing the data.
In today’s big data context, the previous approaches are either incomplete or
suboptimal. For example, the SEMMA methodology disregards completely data
collection and preprocessing of different data sources. These stages normally constitute
most of the work in a successful big data project.
A big data analytics cycle can be described by the following stage −
Business Problem Definition
Research
Human Resources Assessment
Data Acquisition
Data Munging
Data Storage
Exploratory Data Analysis
Data Preparation for Modeling and Assessment
Modeling
Implementation
[Date] 1
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
SCALA AND INSTALLATION
Scala, short for Scalable Language, is a hybrid functional programming language. It was created by
Martin Odersky. Scala smoothly integrates the features of object-oriented and functional languages.
Scala is compiled to run on the Java Virtual Machine. Many existing companies, who depend on Java
for business critical applications, are turning to Scala to boost their development productivity,
applications scalability and overall reliability.
Scala the first choice of application developers.
Scala is object-oriented
Scala is a pure object-oriented language in the sense that every value is an object. Types and
behavior of objects are described by classes and traits which will be explained in subsequent
chapters.
Classes are extended by subclassing and a flexible mixin-based composition mechanism as a
clean replacement for multiple inheritance.
Scala is functional
Scala is also a functional language in the sense that every function is a value and every value is
an object so ultimately every function is an object.
Scala provides a lightweight syntax for defining anonymous functions, it supports higher-
order functions, it allows functions to be nested, and supports currying. These concepts will
be explained in subsequent chapters.
[Date] 2
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
Scala allows you to express general programming patterns in an effective way. It reduces the
number of lines and helps the programmer to code in a type-safe way. It allows you to write
codes in an immutable manner, which makes it easy to apply concurrency and parallelism
(Synchronize).
Scala vs Java
Scala has a set of features that completely differ from Java. Some of these are −
INSTALLATION
Scala IDE for Eclipse is best installed (and updated) directly from within Eclipse.
This is done by using Help → Install New Software..., add the Add... button in the dialog.
Choose a name for the update site (Scala IDE is an obvious choice). Then read the next
section to select which version you will install.
[Date] 3
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
o For Scala 2.10: provides support for projects using Scala 2.10 (any minor version).
This is the current version of Scala. Pick this one if you are unsure.
o For Scala 2.9: provides support for projects using Scala 2.9 (any minor version).
o If you want to live on the bleeding edge (i.e., using the development stream for Scala
and Scala IDE), check the nightlies section to find the one that fits your needs.
o The version of Scala used inside Scala IDE cannot be chosen per project. So, if you
want to work with a project using different version of Scala (like 2.9.3 and 2.10.1),
you need different installation of Scala IDE.
FINISHING INSTALLATION
Copy the URL as location and hit OK to validate. Select Scala IDE for Eclipse from the list
of available features. If you want to install any additional plug-ins (this step is optional),
expand the Scala IDE plugins category and select the plug-ins that fits you best. Go through
the next screens to validate the list of plug-ins to install, the License and start the installation.
After having restarted Eclipse, Scala IDE is installed.
[Date] 4
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
BASIC SCALA SYNTAX AND EXAMPLES
SCALA EXAMPLE
object scalaexample {
def main(args:Array[String]){
println("Hello Scala")
}
}
OUTPUT:
Hello Scala
Variable is a name which is used to refer memory location. It can create mutable and
immutable variable in scala. Let's see how to declare variable.
MUTABLE VARIABLE
It can create mutable variable using var keyword. It allows to change value after
declaration of variable.
In the above code, var is a keyword and data is a variable name. It contains an integer
value 100. Scala is a type infers language so no need to specify data type explicitly. It
can also mention data type of variable explicitly as we have used in below.
Another example of variable
IMMUTABLE VARIABLE
[Date] 5
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
The above code throws an error because we have changed content of immutable
variable, which is not allowed. So if we want to change content then it is advisable to
use var instead of val.
Data types in scala are much similar to java in terms of their storage, length, except that
in scala there is no concept of primitive data types every type is an object and starts
with capital letter. A table of data types is given below.
[Date] 6
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
SCALA OBJECT AND CLASS
OBJECT
Object is a real world entity. It contains state and behavior. Laptop, car, cell phone are
the real world objects. Object typically has two characteristics:
CLASS
1. Data member
2. Member method
3. Constructor
4. Block
5. Nested class
6. Super class information etc.
It initialize all instance variables in the class. There is no default scope. Don't specify
access scope, it is public. There must be an object in which main method is defined. It
provides starting point for the program. Here, it created an example of class.
[Date] 7
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
EX.NO.1 SCALA OBJECT AND CLASS
19.11.2018
AIM:
PROCEDURE:
STEP 1:
STEP 2:
Create a class and define the class name before starting process of an object
STEP 3:
In the main function we create an object for the class student using the keyword new
and define by var
STEP 4:
Once define an object it access the data members and member function seems java
STEP 5:
5.1: In first program it just initialized all the fields in the class and access the data
members using the object
5.2: In second program, in scala the constructor is created in class definition. This is called
primary constructor.
STEP 6:
STEP 7:
[Date] 8
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
PROGRAM:
PROGRAM 1:
class Student {
var id:Int = 0; // All fields must be initialized
var name:String = null;
}
object mainn {
def main(args:Array[String]){
var s = new Student() // Creating an object
println(s.id+" "+s.name);
}
OUTPUT:
0 null
PROGRAM 2:
object student {
def main(args: Array[String]){
var student1 = new Studentt(1,"AADESH");
var student2 = new Studentt(7,"DEEPAN");
student1.getRecord();
student2.getRecord();
}
}
OUTPUT:
1 AADESH
7 DEEPAN
RESULT:
Program to demonstrate classes and object using scala has been executed successfully.
Hence the output is verified.
[Date] 9
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
QUICKSORT
Quick Sort is also based on the concept of Divide and Conquer, just like merge sort.
But in quick sort all the heavy lifting(major work) is done while dividing the array into
subarrays, while in case of merge sort, all the real work happens during merging the
subarrays. In case of quick sort, the combine step does absolutely nothing.
It is also called partition-exchange sort. This algorithm divides the list into three main
parts:
Pivot element can be any element from the array, it can be the first element, the last
element or any random element. In this tutorial, we will take the rightmost element or
the last element as pivot.
For example: In the array {52, 37, 63, 14, 17, 8, 6, 25}, we take 25 as pivot. So after
the first pass, the list will be changed like this.
{6 8 17 14 25 63 37 52}
Hence after the first pass, pivot will be set at its position, with all the elements smaller
to it on its left and all the elements larger than to its right. Now 6 8 17 14 and 63 37 52
are considered as two separate sunarrays, and same recursive logic will be applied on
them, and we will keep doing this until the complete array is sorted.
1. After selecting an element as pivot, which is the last index of the array in our case,
we divide the array for the first time.
2. In quick sort, we call this partitioning. It is not simple breaking down of array into
2 subarrays, but in case of partitioning, the array elements are so positioned that all
the elements smaller than the pivot will be on the left side of the pivot and all the
elements greater than the pivot will be on the right side of it.
3. And the pivot element will be at its final sorted position.
4. The elements to the left and right, may not be sorted.
5. Then we pick subarrays, elements on the left of pivot and elements on the right of
pivot, and we perform partitioning on them by choosing a pivot in the subarrays.
Let's consider an array with values {9, 7, 5, 11, 12, 2, 14, 3, 10, 6}
[Date] 10
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
Below, we have a pictorial representation of how quick sort will sort the given array.
In step 1, we select the last element as the pivot, which is 6 in this case, and call for
partitioning, hence re-arranging the array in such a way that 6 will be placed in its final
position and to its left will be all the elements less than it and to its right, we will have
all the elements greater than it.
Then we pick the subarray on the left and the subarray on the right and select a pivot
for them, in the above diagram, we chose 3 as pivot for the left subarray and 11 as pivot
for the right subarray.
And we again call for partitioning.
[Date] 11
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
EX.NO.2 IMPLEMENTS QUICKSORT USING SCALA
30.11.2018
AIM:
PROCEDURE:
STEP 1:
STEP 2:
Create a new scala object named as quick sort and initialize the values using array
var mess = Array(3, 9, 8, 13, 2, 5, 4);
STEP 3:
Create a function quicksort with the parameters which defines no of elements, high
and low position for implementing sorting methodology
STEP 4:
STEP 5:
STEP 6:
In the partition function, It partition an array around some pivot. Chooses a pivot, moves
all values less than the pivot to its left, and moves all values greater than the pivot to its right.
To optimize: choose pivot corresponding to a median value of some sample. This helps ensure
the partitioning results in similarly-sized sub-arrays, and thus the optimal O(n*ln(n))
performance. @return index (location) of pivot in partitioned array
STEP 7:
STEP 8:
[Date] 12
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
PROGRAM:
object quicksort {
def main(args: Array[String]) {
var mess = Array(3, 9, 8, 13, 2, 5, 4);
quicksort(mess, 0, mess.length-1);
println("Quicksort")
mess.foreach( println )
}
[Date] 13
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
OUTPUT:
Quicksort
13
RESULT:
Quicksort algorithm has been implemented in scala and executed successfully. Hence
the output is verified
[Date] 14
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
SCALA FUNCTION
A function is a group of statements that perform a task. You can divide up your code
into separate functions. How you divide up your code among different functions is up
to you, but logically, the division usually is so that each function performs a specific
task.
Scala has both functions and methods and we use the terms method and function
interchangeably with a minor difference. A Scala method is a part of a class which has
a name, a signature, optionally some annotations, and some bytecode where as a
function in Scala is a complete object which can be assigned to a variable. In other
words, a function, which is defined as a member of some object, is called a method.
A function definition can appear anywhere in a source file and Scala permits nested
function definitions, that is, function definitions inside other function definitions. Most
important point to note is that Scala function's name can have characters like +, ++, ~,
&,-, --, \, /, :, etc.
FUNCTION DECLARATIONS
Methods are implicitly declared abstract if you don’t use the equals sign and the method
body.
FUNCTION DEFINITIONS
Syntax
Eg:
object add {
def addInt( a:Int, b:Int ) : Int = {
var sum:Int = 0
[Date] 15
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
sum = a + b
return sum }}
A function,that does not return anything can return a Unit that is equivalent to void in
Java and indicates that function does not return anything. The functions which do not
return anything in Scala, they are called procedures.
Syntax
object Hello{
def printMe( ) : Unit = {
println("Hello, Scala!")
}
}
CALLING FUNCTIONS
Scala provides a number of syntactic variations for invoking methods. Following is the
standard way to call a method −
If a function is being called using an instance of the object, then we would use dot
notation similar to Java as follows −
Try the following example program to define and then call the same function.
Example
object Demo {
def main(args: Array[String]) {
println( "Returned Value : " + addInt(5,7) );
}
[Date] 16
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
EX.NO.3 SCALA FUNCTION TO PERFORM ARITHMETIC
06.12.2018 OPERATIONS
AIM:
PROCEDURE:
STEP 1:
STEP 2:
Create a new scala object named as arith and define five functions
STEP 3:
STEP 4:
Pass the argument values when the function calls in the main function
STEP 5:
STEP 6:
STEP 7:
[Date] 17
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
PROGRAM:
object arith {
def main(args: Array[String]) {
println("Arithmetic Operation");
println( "Returned Value Addition: " + addInt(5,7) );
println( "Returned Value Subtraction: " + subInt(8,4) );
println( "Returned Value Multiplication: " + mulInt(6,2) );
println( "Returned Value Division: " + divInt(9,3) );
println( "Returned Value Modulus: " + modInt(10,4) );
}
def addInt( a:Int, b:Int ) : Int = {
var sum:Int = 0
sum = a + b
return sum
}
def subInt( a:Int, b:Int ) : Int = {
var sub:Int = 0
sub = a - b
return sub
}
def mulInt( a:Int, b:Int ) : Int = {
var mul:Int = 0
mul = a * b
return mul
}
def divInt( a:Int, b:Int ) : Int = {
var div:Int = 0
div = a / b
return div
}
def modInt( a:Int, b:Int ) : Int = {
var mod:Int = 0
mod = a % b
return mod
}
}
[Date] 18
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
OUTPUT:
Arithmetic Operation
RESULT:
[Date] 19
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
EX.NO.4 FACTORIAL USING RECURSION
14.12.2018
AIM:
PROCEDURE:
STEP 1:
STEP 2:
Create a new scala object named as FACT and define the function named as factorial
STEP 3:
In the function the arguments which defines the datatype as BIG INT similar to java
LONG INT
STEP 4:
STEP 5:
Check whether the given number is 1 it returns 1 otherwise it multiplies each value
recursively
STEP 6:
Select the program and execute by run as application methodology
STEP 7:
[Date] 20
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
PROGRAM
object FACT {
def main(args: Array[String]) {
println("FACTORIAL USING RECURSION")
for (i <- 1 to 10)
println( "Factorial of " + i + ": = " + factorial(i) )
}
OUTPUT:
Factorial of 1: = 1
Factorial of 2: = 2
Factorial of 3: = 6
Factorial of 4: = 24
Factorial of 5: = 120
Factorial of 6: = 720
Factorial of 7: = 5040
Factorial of 8: = 40320
Factorial of 9: = 362880
RESULT:
Factorial of given number using recursion has been executed successfully. Hence the
output is verified
[Date] 21
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
STRING IN SCALA
In Scala, as in Java, a string is an immutable object, that is, an object that cannot be
modified. On the other hand, objects that can be modified, like arrays, are called mutable
objects. Strings are very useful objects, in the rest of this section, we present important
methods of java.lang.String class.
CREATING A STRING
The following code can be used to create a String −
or
Example
object Demo {
println( greeting )
[Date] 22
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
EX.NO.5 STRING MANIPULATIONS
04.01.2019
AIM:
The methods used to obtain information about an object are known as accessor
methods. The length()method is one of the accessor methods which returns the number
of characters in the string.
PROGRAM:
object length {
def main(args: Array[String]) {
var str = "Strings are immutable in scala";
var len = str.length();
println( "String Length is : " + len );
}
}
OUTPUT:
String Length is : 30
Strings can be concatenated in two ways – one is using a concat method and the other
is using the + operator.
PROGRAM:
object concat {
def main(args: Array[String]) {
var str1 = "String concatenation can be ";
var str2 = "done using concat method";
var st = str1.concat(str2)
println( "Concatenated String is : " + st );
}
}
[Date] 23
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
OUTPUT:
Concatenated String is: String concatenation can be done using concat method
PROGRAM:
object strcon {
def main(args: Array[String]) {
var str1 = "Student name ";
var str2 = "is Ashok";
var st = str1+ str2
println( "Concatenated String is : " + st );
}
}
OUTPUT:
The printf() and format() methods are used to format the output numbers. The String
class has format() which returns a String object
PROGRAM:
object format {
def main(args: Array[String]) {
var n1 = 78.99
var n2 = 49
var s1 = "Hello, World!"
var f1 = printf("The value of the float variable is " +
"%f, while the value of the integer " +
"variable is %d, and the string " +
"is %s", n1, n2, s1)
println(f1)
}
}
OUTPUT:
The value of the float variable is 78.990000, while the value of the integer variable
is 49, and the string is Hello, World!()
[Date] 24
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
The format method can be used as below
PROGRAM:
object formatobject {
def main(args:Array[String]){
val firstName = "Thomas"
val lastName = "Mathew"
val age = 12;
OUTPUT:
Thomas Mathew, 12
STRING INTERPOLATION
String Interpolation is the new way to create Strings in Scala programming language. This
feature supports the versions of Scala-2.10 and later. String Interpolation: The mechanism
to embed variable references directly in process string literal.
There are three types (interpolators) of implementations in String Interpolation.
The ‘s’ String Interpolator
The literal ‘s’ allows the usage of variable directly in processing a string, when you
prepend ‘s’ to it. Any String variable with in a scope that can be used with in a String.
The following are the different usages of ‘s’ String interpolator.
The following example code snippet for the implementation of ‘s’ interpolator in
appending String variable ($name) to a normal String (Hello) in println statement.
[Date] 25
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
PROGRAM:
object inter1 {
def main(args: Array[String]) {
val name = "Ajith"
println(s"Hello, $name")
println(s"1 + 1 = ${1 + 1}")
}
}
OUTPUT
Hello, Ajith
1+1=2
The ‘ f ’ Interpolator
The literal ‘f’ interpolator allows to create a formatted String, similar to printf in C language.
While using ‘f’ interpolator, all variable references should be followed by the printf style
format specifiers such as %d, %i, %f, etc.
Let us take an example of append floating point value (height = 1.9d) and String variable
(name = “Vijay”) with normal string. The following code snippet of implementing ‘f’
Interpolator. Here $name%s to print (String variable) James and $height%2.2f to print
(floating point value) 1.90.
PROGRAM:
object inter2 {
def main(args: Array[String]) {
val height = 1.9d
val name = "Vijay"
println(f"$name%s is $height%2.2f meters tall") //Vijay is 1.90
meters tall
}
}
OUTPUT
Hello, Vijay
1+1=2
It is type safe (i.e.) the variable reference and following format specifier should match
otherwise it is showing error. The ‘ f ’ interpolator makes use of the String format utilities
(format specifiers) available in Java. By default means, there is no % character after variable
reference. It will assume as %s (String).
[Date] 26
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
‘raw’ Interpolator
The ‘raw’ interpolator is similar to ‘s’ interpolator except that it performs no escaping of
literals within a string. The following code snippets in a table will differ the usage of ‘s’
and ‘raw’ interpolators. In outputs of ‘s’ usage ‘\n’ effects as new line and in output of ‘raw’
usage the ‘\n’ will not effect. It will print the complete string with escape letters.
PROGRAM − PROGRAM −
} }
} }
OUTPUT −
OUTPUT −
Result =
a Result = \n a \n b
b
RESULT:
String manipulations program has been executed successfully. Hence the output is
verified.
[Date] 27
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
EX.NO.6 ALPHABETIC ARRANGEMENT OF GIVEN STRING.
10.01.2019
AIM:
PROCEDURE:
STEP 1:
STEP 2:
Create a new scala object named as sortstring and define the variables such as
count,temo, and str
STEP 3:
Define scan1 and scan2 for read the string using scanner class. Scanner class is
acceptable as similar as java because scale runs on JVM.
STEP 4:
STEP 5:
Using for loop check each character of the string to compare with the next string of
each one and assign it io temp. Hereby sorting logic has been taken place.
for (i <- 0 until count; j <- i + 1 until count
if str(i).compareTo(str(j)) > 0) {
temp = str(i)
str(i) = str(j)
str(j) = temp
}
STEP 6:
STEP 7:
STEP 8:
[Date] 28
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
PROGRAM:
import java.util.Scanner
object sortstring {
def main(args: Array[String]): Unit = {
var count: Int = 0
var temp: String = null
val scan: Scanner = new Scanner(System.in)
//User will be asked to enter the count of strings
System.out.print("Enter number of strings you would like to enter:")
count = scan.nextInt()
val str: Array[String] = Array.ofDim[String](count)
val scan2: Scanner = new Scanner(System.in)
//User is entering the strings and they are stored in an array
println("Enter the Strings one by one:")
for (i <- 0 until count) {
str(i) = scan2.nextLine()
}
scan.close()
scan2.close()
for (i <- 0 until count; j <- i + 1 until count
if str(i).compareTo(str(j)) > 0) {
temp = str(i)
str(i) = str(j)
str(j) = temp
}
//Displaying the strings after sorting them based on alphabetical order
System.out.println("Strings in Sorted Order:")
var i: Int = 0
while (i <= count - 1) {
System.out.print(str(i) + ", ")
i += 1; i - 1
}
}
}
[Date] 29
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
OUTPUT:
Deepan
Ajith
Vijay
Sharukhan
Aadesh
RESULT:
The Collection of names has been sorted in alphabetical order successfully. Hence the
output is verified
[Date] 30
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
SCALA LIST
Scala Lists are quite similar to arrays which means, all the elements of a list have the same
type but there are two important differences. First, lists are immutable, which means
elements of a list cannot be changed by assignment. Second, lists represent a linked list
whereas arrays are flat.
The type of a list that has elements of type T is written as List[T].
Try the following example, here are few lists defined for various data types.
// List of Strings
// List of Integers
// Empty List.
List(
List(1, 0, 0),
List(0, 1, 0),
List(0, 0, 1)
All lists can be defined using two fundamental building blocks, a tail Nil and ::, which is
pronounced cons. Nil also represents the empty list. All the above lists can be defined as
follows.
// List of Strings
// List of Integers
// Empty List.
[Date] 31
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
BASIC OPERATIONS ON LISTS
All operations on lists can be expressed in terms of the following three methods.
EXAMPLE:
object Demo {
OUTPUT:
[Date] 32
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
EX.NO.7 IMPLEMENT THE STUDENTS DETAILS USING SCALA LIST
25.01.2019 IN VARIOUS METHOD
AIM:
To write a program to implement the student details using scala list in various method.
PROCEDURE:
STEP 1:
STEP 2:
In Program1, Create a new scala object named as immut and initializing immutable
lists such as student named and subjects
STEP 3:
STEP 4:
In Program2, Create a new scala object named as immut2 and initializing two
dimensional lists such as student id and age.
STEP 5:
In Program3, Create a new scala object named as immut2 and initializing two
dimensional lists such as Subject.
STEP 6:
subject.head
subject.tail
subject.isempty
STEP 7:
STEP 8:
[Date] 33
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
PROGRAM 1:
object immut {
def main(args:Array[String])
println("Student name:")
println(student)
println("\nSubject name:")
for(mylist<-subject)
println(mylist)
}}}
OUTPUT:
Student name:
Subject name:
Python
Java
Scala
PHP
[Date] 34
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
PROGRAM 2:
object immut2 {
def main(args:Array[String])
List(
List(100, 19),
List(101, 20),
List(102, 21)
println(ida)
OUTPUT:
PROGRAM 3:
object immut2 {
def main(args:Array[String])
[Date] 35
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
println("The head of the list is:")
println(subject.head)
println(subject.tail)
println(subject.isEmpty)
OUTPUT:
false
RESULT:
Program to implement the student details using scala list has been executed
successfully. Hence the output is verified.
[Date] 36
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
SCALA MAP
Scala map is a collection of key/value pairs. Any value can be retrieved based on its key. Keys
are unique in the Map, but values need not be unique. Maps are also called Hash tables. There
are two kinds of Maps, the immutable and the mutable. The difference between mutable and
immutable objects is that when an object is immutable, the object itself can't be changed.
By default, Scala uses the immutable Map. If you want to use the mutable Map, you'll have to
import scala.collection.mutable.Map class explicitly. If you want to use both mutable and
immutable Maps in the same, then you can continue to refer to the immutable Map as Map but
you can refer to the mutable set as mutable.Map.
All operations on maps can be expressed in terms of the following three methods.
EXAMPLE
object Demo {
def main(args: Array[String]) {
val colors = Map("red" -> "#FF0000", "azure" -> "#F0FFFF", "peru" -> "#CD853F")
val nums: Map[Int, Int] = Map()
println( "Keys in colors : " + colors.keys )
println( "Values in colors : " + colors.values )
println( "Check if colors is empty : " + colors.isEmpty )
println( "Check if nums is empty : " + nums.isEmpty )
}
}
[Date] 37
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
EX.NO.8 MAPPING METHOD IN SCALA
12.02.2019
AIM:
To implement any 5 map methods for maintaining the data using scala.
PROCEDURE:
STEP 1:
STEP 2:
Create a new scala object named as mapp and define the variables
STEP 3:
Declare an empty Scala map or one with values. When it provide values, Scala will
use those to infer the type of variables and add a key-value pair to this.
STEP 4:
STEP 5:
Several methods to call on a map. In that Few methods were implemented such that
STEP 6:
STEP 7:
[Date] 38
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
PROGRAM:
object Mapp {
def main(args: Array[String])
{
//Declaring a Scala Map with Values
var m=Map("Sthuthi"->0,"Sobika"->1)
//Operations on a Map in Scala
println("keys")
m+=("Dhana"->2)
println(m)
println(m.keys)
println(m("Sobika"))
println("values")
println(m.values)
println("Is empty")
println(m.isEmpty)
Map().isEmpty
println("Concatenating maps in scala")
var m1=Map("Sobika"->3,"Dhana"->2,"Harinee"->4)
println("++ operator")
println(m++m1)
println(m1++m)
println("Printing keys and values from a map")
m.keys.foreach{i=>println(i+" "+m(i))}
println("Searching for a key in a scala map")
m.contains("Dhana")
m.contains("Vijay")
println("METHODS TO CALL ON A SCALA MAP")
println("CONCATENATES TWO MAPS")
//def ++(xs: Map[(A, B)]): Map[A, B]
println(m.++(m1))
println("new Map eliding the pairs for the keys")
//def -(elem1: A, elem2: A, elems: A*): Map[A, B]
println(m.-("Sthuthi","Sobika"))
println("value associated with the key")
//def get(key: A): Option[B]
println(m.get("Sthuthi"))
println(m.get("Sorna"))
println("Iterator")
//def iterator: Iterator[(A, B)]
println(m.iterator)
println("Elements map to the Stringbuilder")
[Date] 39
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
//def addString(b: StringBuilder): StringBuilder
println(m.addString(new StringBuilder()))
println("Seperator between the pairs")
//def addString(b: StringBuilder, sep: String): StringBuilder
println(m.addString(new StringBuilder(),"*"))
println("searches for a key")
//def apply(key: A): B
println(m.apply("Sobika"))
println(m.apply("Deepan"))
}
}
OUTPUT:
keys
values
MapLike.DefaultValuesIterable(0, 1, 2)
Is empty
false
++ operator
Sthuthi 0
Sobika 1
Dhana 2
[Date] 40
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
METHODS TO CALL ON A SCALA MAP
Map(Dhana -> 2)
Some(0)
None
Iterator
non-empty iterator
at scala.collection.immutable.Map$Map3.apply(Map.scala:156)
at Mapp$.main(Mapp.scala:52)
at Mapp.main(Mapp.scala)
RESULT:
Implement any 5 map methods for maintaining the data using scala has been executed
successfully. Hence the output is verified
[Date] 41
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
FILE MANIPULATION
Scala is open to make use of any Java objects and java.io.File is one of the
objects which can be used in Scala programming to read and write files.
The following is an example program to writing to a file.
EXAMPLE
import java.io._
object Demo {
def main(args: Array[String]) {
val writer = new PrintWriter(new File("test.txt" ))
writer.write("Hello Scala")
writer.close()
}}
Save the above program in Demo.scala. The following commands are used to
compile and execute this program.
OUTPUT
Hello Scala
Sometime you need to read user input from the screen and then proceed for
some further processing. Following example program shows you how to read
input from the command line.
EXAMPLE
object Demo {
}}
OUTPUT
[Date] 42
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
EX.NO.9 EMPLOYEE RECORDS USING FILES.
20.02.2019
AIM:
PROCEDURE:
STEP 1:
STEP 2:
Create a new scala object named as createfile and declare the variable fileObject to
create a new file to store the content.
STEP 3:
printWriter class to define fileObject for passing reference of file to the printwriter
STEP 4:
STEP 5:
Write the content to the fileObject using write() function. Then Execute the file.
STEP 6:
Create a another scala object named as filee to read the source from the file and write
into the “Employee.txt” file.Read and display the content line by line.
STEP 7:
STEP 8:
[Date] 43
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
PROGRAM 1:
import java.io._
import java.util.Scanner
object createfile {
def main(args:Array[String]){
val empid:String=scan2.nextLine()
val empname:String=scan2.nextLine()
val basic:Int=scan2.nextInt()
val hra:Int=scan2.nextInt()
val da:Int=scan2.nextInt()
val tot:Int=basic+hra+da
printWriter.write("Emp id:"+empid)
printWriter.write("Emp name:"+empname)
printWriter.write("Basic salary:"+basic)
printWriter.write("HRA:"+hra)
printWriter.write("DA:"+da)
[Date] 44
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
printWriter.close()
OUTPUT:
16cau002
Ajith
20000
234
345
PROGRAM 2:
import scala.io.Source
object filee {
def main(args:Array[String])
for(line<-fileSource.getLines)
println(line) }
[Date] 45
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
fileSource.close()
}}
OUTPUT:
SCREENSHOT:
RESULT:
Implement employee records using files has been executed successfully. Hence the
output is verified.
[Date] 46
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
SCALA PATTERN MATCHING
Pattern matching is a way of checking the given sequence of tokens for the
presence of the specific pattern. It is the most widely used feature in Scala. It is
a technique for checking a value against a pattern. It is similar to the switch
statement of Java and C.
Here, “match” keyword is used instead of switch statement. “match” is always
defined in Scala’s root class to make its availability to the all objects. This can
contain a sequence of alternatives. Each alternative will start from case
keyword. Each case statement includes a pattern and one or more expression
which get evaluated if the specified pattern gets matched. To separate the pattern
from the expressions, arrow symbol(=>) is used.
Important Features
Each match keyword must have at least one case clause.
The last “_“, is a “catch-all” case, will be executed if none of the cases matches.
Cases are also called alternatives.
Pattern matching does not have any break statement.
Pattern matching always returns some value.
Match blocks are expressions, not statements. This means that they evaluate the
body of whichever case matches. This is a very important feature of functional
programming.
Pattern matching can also be used for value assignment and for comprehension,
not only in match block.
Pattern matching allows matching any sort of data with the first match policy.
Each case statement returns a value, and the whole match statement is virtually
a function that returns a matched value.
Multiple values can be tested in a single line by using “|“.
[Date] 47
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
EX.NO.10 PATTERN MATCHING
28.02.2019
AIM:
PROCEDURE:
STEP 1:
STEP 2:
Create a new scala object named as PATTERNN and define the function named
called test
STEP 3:
In this method containing match keyword, based on the value given to the x (Passed
to the arguments) each case has been executed
STEP 4:
If the value of x which is passed in test method call matches any of the cases, the expression
within that case is evaluated. Here we are passing 1 so case 1 will be evaluated. case_ => is the
default case which will get executed if value of x is not 0 or 1.
STEP 5:
STEP 6:
[Date] 48
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
PROGRAM 1:
object PATTERNN {
def main(args: Array[String]) {
// calling test method
println(test(1));
}
// method containing match keyword
def test(x:Int): String = x match {
// if value of x is 0,
// this case will be executed
case 0 => "Hello, bca!!"
// if value of x is 1,
// this case will be executed
case 1 => "Welcome to karpagam?"
// if x doesnt match any sequence,
// then this case will be executed
case _ => "Good Luck!!"
}
}
OUTPUT:
Welcome to karpagam?
RESULT:
Implement pattern matching program has been executed successfully. Hence the
output is verified.
[Date] 49
BIG DATA ANALYTICS PRACTICAL [16CAU613A]