Introduction to Apache Spark
Tags: spark, hadoop, beginners, mapreduce
Published: 2019-09-25 06:13:20

MapReduce and Spark are both used for **large-scale data processing**. However, MapReduce has some shortcomings which renders Spark more useful in a number of scenarios.

![Source](https://miro.medium.com/max/1320/1*wTBw3LjcuG2QtaixTrtENw.png)

#Shortcomings of MapReduce
1. Every workflow has to go through a map and reduce phase: Can’t accommodate a join, filter or more complicated workflows like map- reduce-map.
2. MapReduce relies heavily on **reading data from disk**: Performance bottleneck, especially bad for iterative algorithms which may cycle through the data several times.
3. Only native Java programming interface available: Python is also available, but it makes implementation complex and is not very efficient for floating point data.
4. Not that easy in terms of programming and requires lots of hand coding.

#Solution — Apache Spark
* A new framework: Not a complete replacement of the Hadoop stack, just a replacement for Hadoop MapReduce and more
* Capable to using Hadoop ecosystem, e.g., HDFS, yarn

![Source](https://miro.medium.com/max/4096/1*6e2oYaa6rtNNRHbmbenETQ.png)

#Solutions by Spark
1. Spark provides **over 20 highly efficient distributed operations**: Can be used in combination
2. User can choose to **cache data in memory**: Increases performance for iterative algorithms
3. **Polyglot**: Native Java, Python, Scala, R interfaces along with **interactive shell** (test and explore data interactively on shell)
4. Easy to program and does not require that much hand coding

![100 TB Benchmark](https://miro.medium.com/max/2812/1*iuudIyuJ-YRYOHN50C2RAA.png)

#Spark Architecture
Apache Spark has a well-defined layered architecture where all the spark components and layers are loosely coupled.

##Worker Node
* General JVM executor to execute spark workflows
* Core on which **all computation is done**
* Interface to rest of the Hadoop ecosystem, e.g., HDFS

##Cluster Manager
* System to manage **provisioning and starting** of worker nodes
* Cluster manager interfaces supported by spark –
1. **YARN (Hadoop cluster manager):** Same cluster can be used with both Hadoop MR and spark.
2. **Standalone:** Special spark process that takes care of starting nodes at beginning of computation and restarts them on failures.

##Driver Program
* Interface with cluster
* Has a JVM that has a **spark context**: Gateway for us to connect to our spark instance and submit jobs.
* Jobs can be submitted in -
1. **Batch mode :** Send program for execution and wait for result
2. **Streaming mode**: Use spark shell and interact in real-time with data

![Source](https://miro.medium.com/max/1380/1*XRVKLtWBGA6hIioVuHF2zg.png)

#Cloudera VM Setup vs Amazon EMR

##Cloudera VM Setup
* Using spark in standalone mode
* Everything running locally on one machine
* Worker node (executor JVM), spark process and driver program on the same machine

##Amazon EMR
* Supports spark natively
* Web interface to configure number, type of instances, memory required, etc.
* Amazon EMR automatically runs YARN to spawn instances and prepares them to be executed with spark.
* Executor JVMs run on EC2 interfaces
* Driver program and YARN running on the master node

#Resilient Distributed Datasets

##Dataset
* Data storage created from HDFS, S3, HBase, JSON, text, etc.: Once spark reads the data, it can be referenced with an RDD
* Or from transforming another RDD: **RDD are immutable**

##Distributed
* Distributes across cluster of machines
* Data is divided into **partitions** and partitions divided across machines

##Resilient
* Spark tracks history of each partition
* Error recovery like node failures, slow processes

#Spark Transformations
RDD are immutable but we can transform one RDD to another RDD. Spark does **lazy transformations** (execution will not start until an action is triggered).

##Narrow transformations
![Source](https://miro.medium.com/max/796/1*uICzOUSP6GjO2oX6sEt76A.png)
* Like map and filter
* Do not imply transferring data through the network
* Depends on **memory and CPU**

##Wide transformations
![Source](https://miro.medium.com/max/824/1*G7lp70xsxDknpISarZv9FA.png)
* For example, groupByKey transfers data with same key to same partition
**Shuffle operation** across network
* Also depended on **interconnection speed between nodes**

For further reading: [Apache Spark Tutorial: Get Started With Serving ML Models With Spark](https://neptune.ai/blog/apache-spark-tutorial)

Source: [Introduction to Apache Spark](https://towardsdatascience.com/introduction-to-apache-spark-207a479c3001)