You are on page 1of 49

BIG DATA ANALYTICS

 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.

BIG DATA LIFE CYCLE

 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.

Scala is statically typed


 Scala, unlike some of the other statically typed languages (C, Pascal, Rust, etc.), does not expect
you to provide redundant type information. You don't have to specify a type in most cases, and
you certainly don't have to repeat it.

Scala runs on the JVM


 Scala is compiled into Java Byte Code which is executed by the Java Virtual Machine (JVM).
This means that Scala and Java have a common runtime platform. You can easily move from
Java to Scala.
 The Scala compiler compiles your Scala code into Java Byte Code, which can then be executed
by the 'scala' command. The 'scala' command is similar to the java command, in that it executes
your compiled Scala code.

Scala can Execute Java Code


 Scala enables you to use all the classes of the Java SDK and also your own custom Java classes,
or your favorite Java open source projects.

Scala can do Concurrent & Synchronize processing

[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 −

 All types are objects


 Type inference
 Nested Functions
 Functions are objects
 Domain specific language (DSL) support
 Traits
 Closures
 Concurrency support inspired by Erlang
REQUIREMENTS
 The Java Development Kit, version 1.6.0 or newer.
 Eclipse, including the JDT. Either the “Classic” or “Eclipse for Java Developers” is
sufficient. Scala IDE v3.0 can be installed on both Eclipse 3.7 (Indigo) and Eclipse 4.2
(Juno), through different update sites.
o If you are using Eclipse 3.7 (Indigo), we recommend the latest 1.6.x version,
because there have been issues reported when used with Java 7 (e.g., [1], or [2] if
you are on MacOSX - a workaround for the latter issue is described here).
o Eclipse 4.2 (Juno) has full support for Java 7. We recommend to use the latest
Eclipse 4.2 SR2 release which contains several performance improvements.

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.

CHOOSING WHAT VERSION TO INSTALL


 The list of URLs of the different update sites are available in the download area. The release
ones are in the current section. Scala IDE is linked to specific version of Scala, so you have
to decide which one you are going to use:

[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

ABOUT MAIN FUNCTION:

def In Scala the def keyword is used to define a function.


main This is our function name.
(args: Array[String]) Our main function takes in a named parameter args which is an Array
of String.
: Unit = In Scala, the Unit keyword is used to define a function which does not return anything.
This is similar to the void keyword similar to Java or .NET

SCALA VARIABLES AND DATA TYPES

 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.

var data = 100


data = 101 // It works, No error.

 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

val data:Int = 100 // Here, we have mentioned Int followed by : (colon)

IMMUTABLE VARIABLE

val data = 100


data = 101 // Error: reassignment to val

[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

 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.

Data Type Default Value Size

Boolean False True or false

Byte 0 8 bit signed value (-27 to 27-1)

Short 0 16 bit signed value(-215 to 215-1)

Char '\u0000' 16 bit unsigned Unicode character(0 to 216-


1)

Int 0 32 bit signed value(-231 to 231-1)

Long 0L 64 bit signed value(-263 to 263-1)

Float 0.0F 32 bit IEEE 754 single-precision float

Double 0.0D 64 bit IEEE 754 double-precision float

String Null A sequence of characters

[Date] 6
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
SCALA OBJECT AND CLASS

SCALA OBJECT AND CLASS

 Unlike java, scala is a pure object oriented programming language. It allows us to


create object and class so that it can develop object oriented applications.

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:

1) State: data values of an object are known as its state.

2) Behavior: functionality that an object performs is known as its behavior.

 Object in scala is an instance of class. It is also known as runtime entity.

CLASS

 Class is a template or a blueprint. It is also known as collection of objects of similar


type.
 In scala, a class can contain:

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:

To create a scala program to demonstrate Scala object and class

PROCEDURE:

STEP 1:

Open Scala project in Eclipse IDE-> Create a new scala object

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:

Select the program and execute by run as application methodology

STEP 7:

The expected result has been shown in the console window

[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:

class Studentt(id:Int, name:String){


def getRecord(){
println(id+" "+name);
}
}

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:

1. Elements less than the Pivot element


2. Pivot element(Central element)
3. Elements greater than the pivot element

 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.

How does it works?

 Following are the steps involved in quick sort algorithm:

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:

To create a scala program to implements quicksort using scala.

PROCEDURE:

STEP 1:

Open Scala project in Eclipse IDE-> Create a new scala object

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:

Swaps two values in an array which represented in the swap function

STEP 5:

In the quick sort function to Performs recursive quicksort on an array. There's an


opportunity for limiting space complexity to O(ln(n)) here if we recurse on the larger
partition last (tail recursion).

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:

Select the program and execute by run as application methodology

STEP 8:

The expected result has been shown in the console window

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

/** Swaps two values in an array */


def swap(a: Array[Int], pos1: Int, pos2: Int): Unit = {
val stash = a(pos1)
a(pos1) = a(pos2)
a(pos2) = stash
}

def quicksort(a: Array[Int], low: Int, hi: Int): Unit = {


if (low < hi) {
val p = partition(a, low, hi)
quicksort(a, low, p-1)
quicksort(a, p+1, hi)
}
}
def partition(subArray: Array[Int], low: Int, hi: Int): Int = {
val pivot = hi;
var i = low;
for (
j <- low to hi
if subArray(j) < subArray(pivot)
) {swap(subArray, i, j); i+=1}

// Lastly, move pivot value to the dividing line at i


swap(subArray, i, pivot);
return i
}
}

[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

 A Scala function declaration has the following form −

def functionName ([list of parameters]) : [return type]

 Methods are implicitly declared abstract if you don’t use the equals sign and the method
body.

FUNCTION DEFINITIONS

 A Scala function definition has the following form −

Syntax

def functionName ([list of parameters]) : [return type] = {


function body
return [expr]
}
 Here, return type could be any valid Scala data type and list of parameters will be a list
of variables separated by comma and list of parameters and return type are optional.
Very similar to Java, a return statement can be used along with an expression in case
function returns a value. Following is the function which will add two integers and
return their sum −

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

Here is the 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 −

functionName( list of parameters )

 If a function is being called using an instance of the object, then we would use dot
notation similar to Java as follows −

[instance.]functionName( list of parameters )

 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) );
}

def addInt( a:Int, b:Int ) : Int = {


var sum:Int = 0
sum = a + b
return sum
}
}

[Date] 16
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
EX.NO.3 SCALA FUNCTION TO PERFORM ARITHMETIC
06.12.2018 OPERATIONS

AIM:

To write a scala function to perform arithmetic operations

PROCEDURE:

STEP 1:

Open Scala project in Eclipse IDE-> Create a new scala object

STEP 2:

Create a new scala object named as arith and define five functions

STEP 3:

Each function send two arguments to perform the operations

STEP 4:

Pass the argument values when the function calls in the main function

STEP 5:

Perform the operations which defines in each function

def addInt( a:Int, b:Int ) : Int = {


var sum:Int = 0
sum = a + b
return sum

STEP 6:

Select the program and execute by run as application methodology

STEP 7:

The expected result has been shown in the console window

[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

Returned Value Addition: 12

Returned Value Subtraction: 4

Returned Value Multiplication: 12

Returned Value Division: 3

Returned Value Modulus: 2

RESULT:

Scala function to perform arithmetic operations has been executed successfully.


Hence the output is verified

[Date] 19
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
EX.NO.4 FACTORIAL USING RECURSION
14.12.2018

AIM:

To write a program to find the factorial of a given number using recursion

PROCEDURE:

STEP 1:

Open Scala project in Eclipse IDE-> Create a new scala object

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:

Using the loop condition we declare the values from 1 to 10

STEP 5:

Check whether the given number is 1 it returns 1 otherwise it multiplies each value
recursively

def factorial(n: BigInt): BigInt = {


if (n <= 1)
1
else
n * factorial(n - 1)
}
}

STEP 6:
Select the program and execute by run as application methodology

STEP 7:

The expected result has been shown in the console window

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

def factorial(n: BigInt): BigInt = {


if (n <= 1)
1
else
n * factorial(n - 1)
}
}

OUTPUT:

FACTORIAL USING RECURSION

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

Factorial of 10: = 3628800

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 −

var greeting = "Hello world!";

or

var greeting:String = "Hello world!";


 Whenever compiler encounters a string literal in the code, it creates a String object with its
value, in this case, “Hello world!”. String keyword can also be given in alternate declaration
as shown above.
 Try the following example program.

Example

object Demo {

val greeting: String = "Hello, world!"

def main(args: Array[String]) {

println( greeting )

[Date] 22
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
EX.NO.5 STRING MANIPULATIONS
04.01.2019

AIM:

To write a program for string manipulations.

PROCEDURE AND CODING:

STRING LENGTH IN SCALA:

 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

SCALA STRING CONCATENATION

 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

Using “+” operator

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:

Concatenated String is : Student name is Ashok

SCALA STRING FORMATTING

 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;

println("%s %s, %d".format(firstName, lastName, age));


}
}

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.

val name = “Hari


println(s “Hello, $name”) //output: Hello, Hari
 String interpolater can also process arbitrary expressions. The following code snippet
for Processing a String (1 + 1) with arbitrary expression (${1 + 1}) using ‘s’ String
interpolator. Any arbitrary expression can be embedded in ‘${}’.

println(s “1 + 1 = ${1 + 1}”) //output: 1 + 1 = 2


 Try the following example program of implementing ‘s’ interpolator.

[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.

‘s’ interpolator usage ‘raw’ interpolator usage

PROGRAM − PROGRAM −

object sinter { object rawinter{

def main(args: Array[String]) { def main(args: Array[String]) {

println(s"Result = \n a \n b") println(raw"Result = \n a \n b")

} }

} }

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:

To write a program for alphabetic order arrangement of given string

PROCEDURE:

STEP 1:

Open Scala project in Eclipse IDE-> Create a new scala object

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:

Read the no of strings and get the names using array

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:

Displaying the strings after sorting them based on alphabetical order

STEP 7:

Select the program and execute by run as application methodology

STEP 8:

The expected result has been shown in the console window

[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:

Enter number of strings you would like to enter:

Enter the Strings one by one:

Deepan

Ajith

Vijay

Sharukhan

Aadesh

Strings in Sorted Order:

Aadesh, Ajith, Deepan, Sharukhan, Vijay,

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

val fruit: List[String] = List("apples", "oranges", "pears")

// List of Integers

val nums: List[Int] = List(1, 2, 3, 4)

// Empty List.

val empty: List[Nothing] = List()

// Two dimensional list

val dim: List[List[Int]] =

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

val fruit = "apples" :: ("oranges" :: ("pears" :: Nil))

// List of Integers

val nums = 1 :: (2 :: (3 :: (4 :: Nil)))

// Empty List.

val empty = Nil

[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.

Sr.No Methods & Description


head
1
This method returns the first element of a list.
tail
2
This method returns a list consisting of all elements except the first.
isEmpty
3
This method returns true if the list is empty otherwise false.

EXAMPLE:

object Demo {

def main(args: Array[String]) {

val fruit = "apples" :: ("oranges" :: ("pears" :: Nil))

val nums = Nil

println( "Head of fruit : " + fruit.head )

println( "Tail of fruit : " + fruit.tail )

println( "Check if fruit is empty : " + fruit.isEmpty )

println( "Check if nums is empty : " + nums.isEmpty )

OUTPUT:

Head of fruit : apples

Tail of fruit : List(oranges, pears)

Check if fruit is empty : false

Check if nums is empty : true

[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:

Open Scala project in Eclipse IDE-> Create a new scala object

STEP 2:

In Program1, Create a new scala object named as immut and initializing immutable
lists such as student named and subjects

STEP 3:

Display the value of mylist2 using for loop

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:

Use head, tail and is.empty method in the list

subject.head

subject.tail

subject.isempty

STEP 7:

Select the program and execute by run as application methodology

STEP 8:

The expected result has been shown in the console window

[Date] 33
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
PROGRAM 1:

object immut {

def main(args:Array[String])

// Creating and initializing immutable lists

val student: List[String] = List("Hari", "Subhiksha","Vishnu", "Suriya","Thomas")

val subject = List("C", "Python", "Java", "Scala","PHP", "R")

// Display the value of Student

println("Student name:")

println(student)

// Display the value of mylist2 using for loop

println("\nSubject name:")

for(mylist<-subject)

println(mylist)

}}}

OUTPUT:

Student name:

List(Hari, Subhiksha, Vishnu, Suriya, Thomas)

Subject name:

Python

Java

Scala

PHP

[Date] 34
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
PROGRAM 2:

object immut2 {

def main(args:Array[String])

//Creating a two-dimensional List

val ida: List[List[Int]] =

List(

List(100, 19),

List(101, 20),

List(102, 21)

println("Student id and age:")

println(ida)

OUTPUT:

Student id and age:

List(List(100, 19), List(101, 20), List(102, 21))

PROGRAM 3:

object immut2 {

def main(args:Array[String])

//Creating a two-dimensional List

val subject = List("C", "Python", "Java", "Scala","PHP", "R")

[Date] 35
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
println("The head of the list is:")

println(subject.head)

println("The tail of the list is:")

println(subject.tail)

println("List is empty or not:")

println(subject.isEmpty)

OUTPUT:

The head of the list is:

The tail of the list is:

List(Python, Java, Scala, PHP, R)

List is empty or not:

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.

BASIC OPERATIONS ON MAP

 All operations on maps can be expressed in terms of the following three methods.

Sr.No Methods & Description


keys
1
This method returns an iterable containing each key in the map.
values
2
This method returns an iterable containing each value in the map.
isEmpty
3
This method returns true if the map is empty otherwise false.

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:

Open Scala project in Eclipse IDE-> Create a new scala object

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:

Basic operations we can carry out on a Map

 Keys, Values and Pairs

STEP 5:

Several methods to call on a map. In that Few methods were implemented such that

 def ++(xs: Map[(A, B)]): Map[A, B]


 def -(elem1: A, elem2: A, elems: A*): Map[A, B]
 def get(key: A): Option[B]
 def iterator: Iterator[(A, B)]
 def addString(b: StringBuilder): StringBuilder
 def addString(b: StringBuilder, sep: String): StringBuilder
 def apply(key: A): B

STEP 6:

Select the program and execute by run as application methodology

STEP 7:

The expected result has been shown in the console window

[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

Map(Sthuthi -> 0, Sobika -> 1, Dhana -> 2)

values

MapLike.DefaultValuesIterable(0, 1, 2)

Is empty

Set(Sthuthi, Sobika, Dhana)

false

Concatenating maps in scala

++ operator

Map(Sthuthi -> 0, Sobika -> 3, Dhana -> 2, Harinee -> 4)

Map(Sobika -> 1, Dhana -> 2, Harinee -> 4, Sthuthi -> 0)

Printing keys and values from a map

Sthuthi 0

Sobika 1

Dhana 2

Searching for a key in a scala map

[Date] 40
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
METHODS TO CALL ON A SCALA MAP

CONCATENATES TWO MAPS

Map(Sthuthi -> 0, Sobika -> 3, Dhana -> 2, Harinee -> 4)

new Map eliding the pairs for the keys

Map(Dhana -> 2)

value associated with the key

Some(0)

None

Iterator

non-empty iterator

Elements map to the Stringbuilder

Sthuthi -> 0Sobika -> 1Dhana -> 2

Seperator between the pairs

Sthuthi -> 0*Sobika -> 1*Dhana -> 2

searches for a key

Exception in thread "main" java.util.NoSuchElementException: key not found: Deepan

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

READING A LINE FROM COMMAND LINE

 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 {

def main(args: Array[String]) {

print("Please enter your input : " )

val line = Console.readLine

println("Thanks, you just typed: " + line)

}}

OUTPUT

Please enter your input : Scala is great

[Date] 42
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
EX.NO.9 EMPLOYEE RECORDS USING FILES.
20.02.2019

AIM:

To write a program to implement employee records using files

PROCEDURE:

STEP 1:

Open Scala project in Eclipse IDE-> Create a new scala object

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:

Read the employee details such as empid,empname,basicsal,hra and da using scanner


class

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:

Select the program and execute by run as application methodology

STEP 8:

The expected result has been shown in the console window

[Date] 43
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
PROGRAM 1:

import java.io._

import java.util.Scanner

object createfile {

def main(args:Array[String]){

val fileObject = new File("Employee.txt" ) // Creating a file

val printWriter = new PrintWriter(fileObject) // Passing reference of file to the


printwriter

printWriter.write("EMPLOYEE DETAILS")// Writing to the file

val scan2: Scanner = new Scanner(System.in)

println("Enter the Employee id");

val empid:String=scan2.nextLine()

println("Enter the Employee name");

val empname:String=scan2.nextLine()

println("Enter the basic salary");

val basic:Int=scan2.nextInt()

println("Enter the amouht of HRA");

val hra:Int=scan2.nextInt()

println("Enter the amount of DA");

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)

printWriter.write("Total Salary :"+tot)

[Date] 44
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
printWriter.close()

println("Report has been moved to employee file!")

OUTPUT:

Enter the Employee id

16cau002

Enter the Employee name

Ajith

Enter the basic salary

20000

Enter the amouht of HRA

234

Enter the amount of DA

345

Report has been moved to employee file!

PROGRAM 2:

import scala.io.Source

object filee {

def main(args:Array[String])

val filename = "Employee.txt"

val fileSource = Source.fromFile(filename)

for(line<-fileSource.getLines)

println(line) }

[Date] 45
BIG DATA ANALYTICS PRACTICAL [16CAU613A]
fileSource.close()

}}

OUTPUT:

EMPLOYEE DETAILSEmp id:16cau002Emp name:AjithBasic

salary:20000HRA:234DA:345Total Salary :20579

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:

To write a program to implement the pattern matching

PROCEDURE:

STEP 1:

Open Scala project in Eclipse IDE-> Create a new scala object

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:

Select the program and execute by run as application methodology

STEP 6:

The expected result has been shown in the console window

[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]

You might also like