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
Mex$814.99 Mex$1164.99
Full star icon Full star icon Half star icon Empty star icon Empty star icon 2.8 (12 Ratings)
eBook Jul 2017 796 pages 1st Edition
eBook
Mex$814.99 Mex$1164.99
Paperback
Mex$1456.99
Subscription
Free Trial
Arrow left icon
Profile Icon Karim Profile Icon Sridhar Alla
Arrow right icon
Mex$814.99 Mex$1164.99
Full star icon Full star icon Half star icon Empty star icon Empty star icon 2.8 (12 Ratings)
eBook Jul 2017 796 pages 1st Edition
eBook
Mex$814.99 Mex$1164.99
Paperback
Mex$1456.99
Subscription
Free Trial
eBook
Mex$814.99 Mex$1164.99
Paperback
Mex$1456.99
Subscription
Free Trial

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
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 : 9781783550500
Vendor :
Apache
Category :
Languages :
Concepts :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want

Product Details

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

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.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
$199.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 Mex$85 each
Feature tick icon Exclusive print discounts
$279.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 Mex$85 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total Mex$ 3,939.97
Mastering Apache Spark 2.x
Mex$1128.99
Scala for Machine Learning, Second Edition
Mex$1353.99
Scala and Spark for Big Data Analytics
Mex$1456.99
Total Mex$ 3,939.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

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.