Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Scala and Spark for Big Data Analytics
Scala and Spark for Big Data Analytics

Scala and Spark for Big Data Analytics: Explore the concepts of functional programming, data streaming, and machine learning

Arrow left icon
Profile Icon Karim Profile Icon Sridhar Alla
Arrow right icon
€18.99 per month
Full star icon Full star icon Half star icon Empty star icon Empty star icon 2.8 (12 Ratings)
Paperback Jul 2017 796 pages 1st Edition
eBook
€29.99 €42.99
Paperback
€53.99
Subscription
Free Trial
Renews at €18.99p/m
Arrow left icon
Profile Icon Karim Profile Icon Sridhar Alla
Arrow right icon
€18.99 per month
Full star icon Full star icon Half star icon Empty star icon Empty star icon 2.8 (12 Ratings)
Paperback Jul 2017 796 pages 1st Edition
eBook
€29.99 €42.99
Paperback
€53.99
Subscription
Free Trial
Renews at €18.99p/m
eBook
€29.99 €42.99
Paperback
€53.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

Scala and Spark for Big Data Analytics

Object-Oriented Scala

"The object-oriented model makes it easy to build up programs by accretion. What this often means, in practice, is that it provides a structured way to write spaghetti code."

- Paul Graham

In the previous chapter, we looked at how to get programming started with Scala. Well, if you're writing the procedural program that we followed in the previous chapter, you can enforce the code reusability by creating procedures or functions. However, if you continue working, consequently, your program gets longer, bigger, and more complex. At a certain point, you will not even have any other more simple way to organize the entire code before production.

On the contrary, the object-oriented programming (OOP) paradigm provides a whole new layer of abstraction. You can then modularize your code through defining OOP entities such as classes with related properties...

Variables in Scala

Before entering into the depth of OOP features, first, we need to know details about the different types of variables and data types in Scala. To declare a variable in Scala, you need to use var or val keywords. The formal syntax of declaring a variable in Scala is as follows:

val or var VariableName : DataType = Initial_Value

For example, let's see how can we declare two variables whose data types are explicitly specified as follows:

var myVar : Int = 50
val myVal : String = "Hello World! I've started learning Scala."

You can even just declare a variable without specifying the DataType. For example, let's see how to declare a variable using val or var, as follows:

var myVar = 50
val myVal = "Hello World! I've started learning Scala."

There are two types of variables in Scala: mutable and immutable that can be defined as...

Methods, classes, and objects in Scala

In the previous section, we saw how to work with Scala variables, different data types and their mutability and immutability, along with their usages scopes. However, in this section, to get the real flavor of the OOP concept, we are going to deal with methods, objects, and classes. These three features of Scala will help us understand the object-oriented nature of Scala and its features.

Methods in Scala

In this part, we are going to talk about methods in Scala. As you dive into Scala, you'll find that there are lots of ways to define methods in Scala. We will demonstrate them in some of these ways:

def min(x1:Int, x2:Int) : Int = {
if (x1 < x2) x1 else x2
}

The preceding declaration...

Packages and package objects

Just like Java, a package is a special container or object which contains/defines a set of objects, classes, and even packages. Every Scala file has the following automatically imported:

  • java.lang._
  • scala._
  • scala.Predef._

The following is an example for basic imports:

// import only one member of a package
import java.io.File
// Import all members in a specific package
import java.io._
// Import many members in a single import statement
import java.io.{File, IOException, FileNotFoundException}
// Import many members in a multiple import statement
import java.io.File
import java.io.FileNotFoundException
import java.io.IOException

You can even rename a member while importing, and that's to avoid a collision between packages that have the same member name. This method is also called class alias:

import java.util.{List => UtilList}
import java.awt.{List...

Java interoperability

Java is one of the most popular languages, and many programmers learn Java programming as their first entrance to the programming world. The popularity of Java has increased since its initial release back in 1995. Java has gained in popularity for many reasons. One of them is the design of its platform, such that any Java code will be compiled to bytecode, which in turn runs on the JVM. With this magnificent feature, Java language to be being written once and run anywhere. So, Java is a cross-platform language.

Also, Java has lots of support from its community and lots of packages that will help you get your idea up and running with the help of these packages. Then comes Scala, which has lots of features that Java lacks, such as type inference and optional semicolon, immutable collections built right into Scala core, and lots more features (addressed in Chapter...

Pattern matching

One of the widely used features of Scala is pattern matching. Each pattern match has a set of alternatives, each of them starting with the case keyword. Each alternative has a pattern and expression(s), which will be evaluated if the pattern matches and the arrow symbol => separates pattern(s) from expression(s). The following is an example which demonstrates how to match against an integer:

object PatternMatchingDemo1 {
def main(args: Array[String]) {
println(matchInteger(3))
}
def matchInteger(x: Int): String = x match {
case 1 => "one"
case 2 => "two"
case _ => "greater than two"
}
}

You can run the preceding program by saving this file in PatternMatchingDemo1.scala and then using the following commands to run it. Just use the following command:

>scalac Test.scala
>scala Test

You will get the...

Implicit in Scala

Implicit is another exciting and powerful feature introduced by Scala, and it can refer to two different things:

  • A value that can be automatically passed
  • Automatic conversion from one type to another
  • They can be used for extending the capabilities of a class

Actual automatic conversion can be accomplished with implicit def, as seen in the following example (supposing you are using the Scala REPL):

scala> implicit def stringToInt(s: String) = s.toInt
stringToInt: (s: String)Int

Now, having the preceding code in my scope, it's possible for me to do something like this:

scala> def add(x:Int, y:Int) = x + y
add: (x: Int, y: Int)Int

scala> add(1, "2")
res5: Int = 3
scala>

Even if one of the parameters passed to add() is a String (and add() would require you to provide two integers), having the implicit conversion in scope allows the compiler...

Generic in Scala

Generic classes are classes which take a type as a parameter. They are particularly useful for collection classes. Generic classes can be used in everyday data structure implementation, such as stack, queue, linked list, and so on. We will see some examples.

Defining a generic class

Generic classes take a type as a parameter within square brackets []. One convention is to use the letter A as a type parameter identifier, though any parameter name may be used. Let's see a minimal example on Scala REPL, as follows:

scala> class Stack[A] {
| private var elements: List[A] = Nil
| def push(x: A) { elements = x :: elements }
| def peek: A = elements.head
| def pop(...

SBT and other build systems

It's necessary to use a build tool for any enterprise software project. There are lots of build tools that you can choose from, such as Maven, Gradle, Ant, and SBT. A good choice of build tool is the one which will let you focus on coding rather than compilation complexities.

Build with SBT

Here, we are going to give a brief introduction to SBT. Before going any further, you need to install SBT using the installation method that fits your system from their official installations methods (URL: http://www.scala-sbt.org/release/docs/Setup.html).

So, let's begin with SBT to demonstrate the use of SBT in a terminal. For this build tool tutorial, we assume that your source code files are in...

Summary

Structuring code in a sane way, with classes and traits, enhances the reusability of your code with generics, and creates a project with standard and widespread tools. Improve on the basics to know how Scala implements the OO paradigm to allow the building of modular software systems. In this chapter, we discussed the basic object-oriented features in Scala, such as classes and objects, packages and package objects, traits, and trait linearization, Java interoperability, pattern matching, implicit, and generics. Finally, we discussed SBT and other build systems that will be needed to build our Spark application on Eclipse or any other IDEs.

In the next chapter, we will discuss what functional programming is and how Scala supports it. We will get to know why it matters and what the advantages of using functional concepts are. Continuing, you will learn pure functions, higher...

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Learn Scala’s sophisticated type system that combines Functional Programming and object-oriented concepts
  • Work on a wide array of applications, from simple batch jobs to stream processing and machine learning
  • Explore the most common as well as some complex use-cases to perform large-scale data analysis with Spark

Description

Scala has been observing wide adoption over the past few years, especially in the field of data science and analytics. Spark, built on Scala, has gained a lot of recognition and is being used widely in productions. Thus, if you want to leverage the power of Scala and Spark to make sense of big data, this book is for you. The first part introduces you to Scala, helping you understand the object-oriented and functional programming concepts needed for Spark application development. It then moves on to Spark to cover the basic abstractions using RDD and DataFrame. This will help you develop scalable and fault-tolerant streaming applications by analyzing structured and unstructured data using SparkSQL, GraphX, and Spark structured streaming. Finally, the book moves on to some advanced topics, such as monitoring, configuration, debugging, testing, and deployment. You will also learn how to develop Spark applications using SparkR and PySpark APIs, interactive data analytics using Zeppelin, and in-memory data processing with Alluxio. By the end of this book, you will have a thorough understanding of Spark, and you will be able to perform full-stack data analytics with a feel that no amount of data is too big.

Who is this book for?

Anyone who wishes to learn how to perform data analysis by harnessing the power of Spark will find this book extremely useful. No knowledge of Spark or Scala is assumed, although prior programming experience (especially with other JVM languages) will be useful to pick up concepts quicker.

What you will learn

  • Understand object-oriented & functional programming concepts of Scala
  • In-depth understanding of Scala collection APIs
  • Work with RDD and DataFrame to learn Spark's core abstractions
  • Analysing structured and unstructured data using SparkSQL and GraphX
  • Scalable and fault-tolerant streaming application development using Spark structured streaming
  • Learn machine-learning best practices for classification, regression, dimensionality reduction, and recommendation system to build predictive models with widely used algorithms in Spark MLlib & ML
  • Build clustering models to cluster a vast amount of data
  • Understand tuning, debugging, and monitoring Spark applications
  • Deploy Spark applications on real clusters in Standalone, Mesos, and YARN

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jul 25, 2017
Length: 796 pages
Edition : 1st
Language : English
ISBN-13 : 9781785280849
Vendor :
Apache
Category :
Languages :
Concepts :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Jul 25, 2017
Length: 796 pages
Edition : 1st
Language : English
ISBN-13 : 9781785280849
Vendor :
Apache
Category :
Languages :
Concepts :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 145.97
Mastering Apache Spark 2.x
€41.99
Scala for Machine Learning, Second Edition
€49.99
Scala and Spark for Big Data Analytics
€53.99
Total 145.97 Stars icon

Table of Contents

18 Chapters
Introduction to Scala Chevron down icon Chevron up icon
Object-Oriented Scala Chevron down icon Chevron up icon
Functional Programming Concepts Chevron down icon Chevron up icon
Collection APIs Chevron down icon Chevron up icon
Tackle Big Data – Spark Comes to the Party Chevron down icon Chevron up icon
Start Working with Spark – REPL and RDDs Chevron down icon Chevron up icon
Special RDD Operations Chevron down icon Chevron up icon
Introduce a Little Structure - Spark SQL Chevron down icon Chevron up icon
Stream Me Up, Scotty - Spark Streaming Chevron down icon Chevron up icon
Everything is Connected - GraphX Chevron down icon Chevron up icon
Learning Machine Learning - Spark MLlib and Spark ML Chevron down icon Chevron up icon
My Name is Bayes, Naive Bayes Chevron down icon Chevron up icon
Time to Put Some Order - Cluster Your Data with Spark MLlib Chevron down icon Chevron up icon
Text Analytics Using Spark ML Chevron down icon Chevron up icon
Spark Tuning Chevron down icon Chevron up icon
Time to Go to ClusterLand - Deploying Spark on a Cluster Chevron down icon Chevron up icon
Testing and Debugging Spark Chevron down icon Chevron up icon
PySpark and SparkR Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Half star icon Empty star icon Empty star icon 2.8
(12 Ratings)
5 star 41.7%
4 star 0%
3 star 0%
2 star 8.3%
1 star 50%
Filter icon Filter
Top Reviews

Filter reviews by




Verified Amazon Customer Jul 27, 2017
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Fantastic book! I purchased this book yesterday and already read some chapters and had a quick look to other chapters too. It contains everything needed for learning big data analytics with Spark. It elaborately covers the Scala programming, Sparks basic operations, many machine learning algorithms (classification, regression, clustering, recommendation system), NLP, graph analytics, structured streaming, and of course some other advanced topics of Spark such as tuning, debugging and cluster deployment.More interestingly, it also discussed PySpark, SparkR, Alixuio, and Zeppelin so I don't need to switch to something else. So in summary, it contains everything I was looking for few months.
Amazon Verified review Amazon
Md Ashiqur Rahman Nov 16, 2017
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This is a fantastic book for getting a real understanding about how to develop big data processing applications using Scala and Spark. This book is very up to date containing wide coverage of all the APIs such as Spark SQL, structured streaming, graphX, Spark MLib and more.
Amazon Verified review Amazon
Amazon Customer Aug 01, 2017
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I am a scala enthusiastic, also professional java programmer, now a days im into bigdata stack. I've read the entire book more or less. I would highly recommend this book if someone working in java area but want to write java code in functional (scala) way to solve bigdata problems - scala makes spark processing much easier. I would also recommend this book who is yet to start bigdata coding using scala for spark processing. This book includes a lot of nice, small and real life bigdata analytics problems and how to solve them easily using scala on top of hadoop ecosystem.Thanks to authors for such an useful book - its not just only concepts but also guide to implement concepts.
Amazon Verified review Amazon
Ariel Herrera Oct 03, 2018
Full star icon Full star icon Full star icon Full star icon Full star icon 5
What I love about this book is that the author explains each concept very well and provides numerous examples. Yes, it is a large read however you can jump around in the book based on your need and it is sufficient.
Amazon Verified review Amazon
Julio Bregeiro Nov 18, 2023
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Aprovadi
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.