Scala commands for Spark

  • date 4th April, 2019 |
  • by Prwatech |
  • 0 Comments

 

Scala commands for Spark

 

Scala commands for Spark, welcome to the world of Apache Spark Basic Scala Commands. Are you the one who is looking forward to knowing the scala commands for spark List which are useful for Spark developers? Or the one who is very keen to explore the list of all the basic scala commands in Apache Spark with examples that are available? Then you’ve landed on the Right path which provides the standard and Apache Spark Basic scala Commands.

 

If you are the one who is keen to learn the technology then learn the advanced certification course from the best Apache Spark training institute who can help guide you about the course from the 0 Level to Advanced level. So don’t just dream to become the certified Pro Developer achieve it by choosing the best World classes Apache Spark training institute in Bangalore which consists of World-class Trainers.

 

We, Prwatech listed some of the top Apache Spark basic Scala Commands which Every Spark Developer should know about. So follow the Below Mentioned scala commands for spark and Learn the Advanced Apache Spark course from the best Spark Trainer like a Pro.

 

Apache Spark Basic Scala Commands

 

Expressions are computable statements

 

scala> 12 * 10

res0: Int = 120

 

Scala commands for Spark

 

You can output results of expressions using println

Or (what You want to print you can print using println)

 

Scala commands for Spark

 

Ex:-println(“Hello Prwatech”)

scala> println(“Hello,” + “Prwatech”)

 

Scala commands for Spark

 

Values

 

You can name the results of expressions with the value keyword.

Here such as  ‘x’ is value “Referencing a value doesn’t re-compute it”

 

Scala commands for Spark

 

 Variable

 

Variables are like values, except you can re-assign them. You can define a variable with the var keyword.

varx=6 + 4

x=10

 

Scala commands for Spark

 

uBlocks

 

You can combine expressions by surrounding them with {}. We call this a block.

The result of the last expression in the block is the result of the overall block too.

 

Scala commands for Spark

Scala commands for Spark

 

Functions

 

Functions are expressions that take parameters.

You can define an anonymous function (i.e. no name) that returns a given integer plus two

On the left of =,> is a list of parameters. On the right is an expression involving the parameters.

You can also name functions.

 

 scala commands for spark

 scala commands for spark

 

Functions may take multiple parameters.

 

Scala commands for Spark

 

Or it can take no parameters.

 

Scala commands for Spark

Methods

 

Methods look and behave very similar to functions, but there are a few key differences between them

Methods are defined with the def keyword.

 

Scala commands for Spark

 

Notice how the return type is declared after the parameter list and a colon: Int.

Methods can take multiple parameter lists.

 

Scala commands for Spark

 

Classes

 

You can define classes with the class keyword followed by its name and constructor parameters.

 

Scala commands for Spark

 

Objects

 

Objects are single instances of their own definitions. You can think of them as singletons of their own classes.

You can define objects with the object keyword.

 

Scala commands for Spark

 

SINGLETON OBJECT

 

Scala classes do not have a static keyword, so they have Singleton Object.

The constructor of the singleton object is executed when the object is first used.

An object has all the features of the class.

The parameters cannot be provided to the constructor.

They can be imported from anywhere in the program.

Singleton Objects are used in the following cases:

When a singleton instance is required for co-ordinating a service.

When a single immutable instance could be shared for efficiency purposes.

When an immutable instance is required for utility functions or constants.

 

Scala commands for Spark

 

 If else Condition

 

Command:-var a  = 8

val s = if ( a>1 &&a<9)1else 0

Command:-var a  = 9

val s = if ( a>1 && a<9)1else 0

 

 Scala commands for Spark

 

While Loop

 

Command:-var args = “PRWATECH”

                            var i = 0

  while (i<args.length)

 {

println(args(i))

i +=1

                           }

 

 Scala commands for Spark

 

Foreach Loop

 

Command:-var args = “SCALA”

                                 args.foreach(println)

 

 

Scala commands for Spark

 

For Loop

 

Command:-for(i<1 to 5)

print(i)

Command:- for(i<11 to 25)

print(i)

 

Scala commands for Spark

 

Nested for loop

 

Command:-for (i<-1 to 5; j =i) println(20*i+j)

 

Scala commands for Spark

 

How to write a function

 

First define the function and then call it with the value

Command:-def area(radius:Int):Double= {7.20*radius*radius}

 

Scala commands for Spark

 

Argument to function

 

Command:-def concatstr(arg1:String,arg2:String = “Institute”,arg3:String = “Prwatech”) = println(arg1 + arg2 +arg3)

            –  concatstr(“BigData”)

            – concatstr(“BigData”, arg3=”spark”)

            – concatstr(“BigData”)

 

 Scala commands for Spark  

 

Scala Procedure

 

Scala has a special function that doesn’t return any value.

If there is a scala function without a preceding “=” symbol, then  the return type of the function is unit

      Such a function is called procedures.

      Procedures don’t return any value in scala

Commands:-def rect_area(length:Float, breadth:Float) {val area = length*breadth; println(area)}

 

Scala commands for Spark

 

 Scala

Collection

Scala has a rich library of collection, they are:-

Array

ArrayBuffers

Maps

Tuples

Lists

 

Array

 

It is the collection of elements or records of same type

Command:-var i = Array(“Hello”, “Prwatech”)

 print(i(0))

 print(i(1))

 

 Scala commands for Spark

 

Array Buffers

 

Commands for initializing ArrayBuffers: import scala.collection.mutable.ArrayBuffer

 

 

 

Quick Support

image image