Book Image

Apache Spark 2.x Cookbook

By : Rishi Yadav
Book Image

Apache Spark 2.x Cookbook

By: Rishi Yadav

Overview of this book

While Apache Spark 1.x gained a lot of traction and adoption in the early years, Spark 2.x delivers notable improvements in the areas of API, schema awareness, Performance, Structured Streaming, and simplifying building blocks to build better, faster, smarter, and more accessible big data applications. This book uncovers all these features in the form of structured recipes to analyze and mature large and complex sets of data. Starting with installing and configuring Apache Spark with various cluster managers, you will learn to set up development environments. Further on, you will be introduced to working with RDDs, DataFrames and Datasets to operate on schema aware data, and real-time streaming with various sources such as Twitter Stream and Apache Kafka. You will also work through recipes on machine learning, including supervised learning, unsupervised learning & recommendation engines in Spark. Last but not least, the final few chapters delve deeper into the concepts of graph processing using GraphX, securing your implementations, cluster optimization, and troubleshooting.
Table of Contents (19 chapters)
Title Page
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

Understanding resilient distributed dataset - RDD


Though RDD is getting replaced with DataFrame/DataSet-based APIs, there are still a lot of APIs that have not been migrated yet. In this recipe, we will look at how the concept of lineage works in RDD.

Externally, RDD is a distributed, immutable collection of objects. Internally, it consists of the following five parts:

  • Set of partitions (rdd.getPartitions)
  • List of dependencies on parent RDDs (rdd.dependencies)
  • Function to compute a partition, given its parents
  • Partitioner, which is optional (rdd.partitioner)
  • Preferred location of each partition, which is optional (rdd.preferredLocations)

The first three are needed for an RDD to be recomputed in case data is lost. When combined, it is called lineage. The last two parts are optimizations.

A set of partitions is how data is divided into nodes. In the case of HDFS, it means InputSplits, which are mostly the same as the block (except when a record crosses block boundaries; in that case, it will be slightly bigger than a block).

How to do it...

Let's revisit our word count example to understand these five parts. This is how an RDD graph looks for wordCount at the dataset level view:

Basically, this is how the flow goes:

  1. Load the words folder as an RDD:
scala> val words = sc.textFile("hdfs://localhost:9000/user/hduser/words")

The following are the five parts of the words RDD:

Part

Description

Partitions

One partition per HDFS inputsplit/block (org.apache.spark.rdd.HadoopPartition)

Dependencies

None

Compute function

To read the block

Preferred location

The HDFS block's location

Partitioner

None

  1. Tokenize the words of the words RDD with each word on a separate line:
scala> val wordsFlatMap = words.flatMap(_.split("W+"))

The following are the five parts of the wordsFlatMap RDD:

Part

Description

Partitions

Same as the parent RDD, that is, words (org.apache.spark.rdd.HadoopPartition)

Dependencies

Same as the parent RDD, that is, words (org.apache.spark.OneToOneDependency)

Compute function

To compute the parent and split each element, which flattens the results

Preferred location

Ask parent RDD

Partitioner

None

  1. Transform each word in the wordsFlatMap RDD into the (word,1) tuple:
scala> val wordsMap = wordsFlatMap.map( w => (w,1))

The following are the five parts of the wordsMap RDD:

Part

Description

Partitions

Same as the parent RDD, that is, wordsFlatMap (org.apache.spark.rdd.HadoopPartition)

Dependencies

Same as the parent RDD, that is, wordsFlatMap (org.apache.spark.OneToOneDependency)

Compute function

To compute the parent and map it to PairRDD

Preferred Location

Ask parent RDD

Partitioner

None

  1. Reduce all the values of a given key and sum them up:
scala> val wordCount = wordsMap.reduceByKey(_+_)

The following are the five parts of the wordCount RDD:

Part

Description

Partitions

One per reduce task (org.apache.spark.rdd.ShuffledRDDPartition)

Dependencies

Shuffle dependency on each parent (org.apache.spark.ShuffleDependency)

Compute function

To perform additions on shuffled data

Preferred location

None

Partitioner

HashPartitioner (org.apache.spark.HashPartitioner)

This is how an RDD graph of wordcount looks at the partition level view: