Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 9 Next »

https://courses.cognitiveclass.ai/courses/course-v1:BigDataUniversity+BD0211EN+2016/courseware/8fdff50a06d44cd2a01cf0277e131f02/637f5a0b033e429abee1f7ccc8c4565d/

Learning Objectives


In this course you will learn about:

    • the purpose of Spark and understand why and when you would use Spark.
    • how to list and describe the components of the Spark unified stack.
    • the basics of the Resilient Distributed Dataset, Spark's primary data abstraction.
    • how to download and install Spark standalone.
    • an overview of Scala and Python.

Introduction to Spark


Learning Objectives

In this lesson you will learn about:

  • Explain the purpose of Spark
  • List and describe the components of the Spark unified stack
  • Understand the basics of Resilient Distributed Dataset (RDD)
  • Downloading and installing Spark standalone
  • Scala and Python overview
  • Launch and use Spark’s Scala and Python shell

Objectives: After completing this lesson, you should be able to explain the purpose of Spark and understand why and when you would use Spark. You should be able to list and describe the components of the Spark unified stack. You will be able to understand the basics of the Resilient Distributed Dataset, Spark's primary data abstraction. Then you will see how to download and install Spark standalone to test it out yourself. You will get an overview of Scala and Python to prepare for using the two Spark shells.

There is an explosion of data. No matter where you look, data is everywhere. You get data from social media such as Twitter feeds, Facebook posts, SMS, and a variety of others. The need to be able to process those data as quickly as possible becomes more important than ever. How can you find out what your customers want and be able to offer it to them right away? You do not want to wait hours for a batch job to complete. You need to have it in minutes or less.

MapReduce has been useful, but the amount of time it takes for the jobs to run is no longer acceptable in most situations. The learning curve to writing a MapReduce job is also difficult as it takes specific programming knowledge and the know-how. Also, MapReduce jobs only work for a specific set of use cases. You need something that works for a wider set of use cases. Apache Spark was designed as a computing platform to be fast, general-purpose, and easy to use.

It extends the MapReduce model and takes it to a whole other level. The speed comes from the in-memory computations. Applications running in memory allows for a much faster processing and response. Spark is even faster than MapReduce for complex applications on disks.

This generality covers a wide range of workloads under one system. You can run batch application such as MapReduce types jobs or iterative algorithms that builds upon each other. You can also run interactive queries and process streaming data with your application. In a later slide, you'll see that there are a number of libraries which you can easily use to expand beyond the basic Spark capabilities.

The ease of use with Spark enables you to quickly pick it up using simple APIs for Scala, Python and Java. As mentioned, there are additional libraries which you can use for SQL, machine learning, streaming, and graph processing. Spark runs on Hadoop clusters such as Hadoop YARN or Apache Mesos, or even as a standalone with its own scheduler.

You may be asking, why would I want to use Spark and what would I use it for? As you know, Spark is related to MapReduce in a sense that it expands on its capabilities.

Like MapReduce, Spark provides parallel distributed processing, fault tolerance on commodity hardware, scalability, etc. Spark adds to the concept with aggressively cached in-memory distributed computing, low latency, high level APIs and stack of high level tools described on the next slide. This saves time and money. There are two groups that we can consider here who would want to use Spark: Data Scientists and Engineers.

You may ask, but aren't they similar? In a sense, yes, they do have overlapping skill sets, but for our purpose, we'll define data scientist as those who need to analyze and model the data to obtain insight. They would have techniques to transform the data into something they can use for data analysis. They will use Spark for its ad-hoc analysis to run interactive queries that will give them results immediately. Data scientists may also have experience using SQL, statistics, machine learning and some programming, usually in Python, MatLab or R.

Once the data scientists have obtained insights on the data and later someone determines that there's a need develop a production data processing application, a web application, or some system to act upon the insight, the person called upon to work on it would be the engineers.

Engineers would use Spark's programming API to develop a system that implement business use cases. Spark parallelize these applications across the clusters while hiding the complexities of distributed systems programming and fault tolerance. Engineers can use Spark to monitor, inspect and tune applications. For everyone else, Spark is easy to use with a wide range of functionality. The product is mature and reliable.

Here we have a picture of the Spark unified stack. As you can see, the Spark core is at the center of it all. The Spark core is a general-purpose system providing scheduling, distributing, and monitoring of the applications across a cluster. Then you have the components on top of the core that are designed to interoperate closely, letting the users combine them, just like they would any libraries in a software project. The benefit of such a stack is that all the higher layer components will inherit the improvements made at the lower layers.

Example: Optimization to the Spark Core will speed up the SQL, the streaming, the machine learning and the graph processing libraries as well. The Spark core is designed to scale up from one to thousands of nodes. It can run over a variety of cluster managers including Hadoop YARN and Apache Mesos. Or simply, it can even run as a standalone with its own built-in scheduler. Spark SQL is designed to work with the Spark via SQL and HiveQL (a Hive variant of SQL). Spark SQL allows developers to intermix SQL with Spark's programming language supported by Python, Scala, and Java.

Spark Streaming provides processing of live streams of data. The Spark Streaming API closely matches that of the Sparks Core's API, making it easy for developers to move between applications that processes data stored in memory vs arriving in real-time. It also provides the same degree of fault tolerance, throughput, and scalability that the Spark Core provides. Machine learning, MLlib is the machine learning library that provides multiple types of machine learning algorithms. All of these algorithms are designed to scale out across the cluster as well.

GraphX is a graph processing library with APIs to manipulate graphs and performing graph-parallel computations.

Here's a brief history of Spark. I'm not going to spend too much time on this as you can easily find more information for yourself if you are interested. Basically, you can see that MapReduce started out over a decade ago. MapReduce was designed as a fault tolerant framework that ran on commodity systems. Spark comes out about a decade later with the similar framework to run data processing on commodity systems also using a fault tolerant framework. MapReduce started off as a general batch processing system, but there are two major limitations. 1) Difficulty in programming directly in MR and 2) Batch jobs do not fit many use cases. So this spawned specialized systems to handle other use cases. When you try to combine these third party systems in your applications, there are a lot of overhead.

Taking a looking at the code size of some applications on the graph on this slide, you can see that Spark requires a considerable amount less. Even with Spark's libraries, it only adds a small amount of code due to how tightly everything is integrated with very little overhead. There is great value to be able to express a wide variety of use cases with few lines of code. Now let's get into the core of Spark.

Spark's primary core abstraction is called Resilient Distributed Dataset or RDD. Essentially it is just a distributed collection of elements that is parallelized across the cluster. You can have two types of RDD operations. Transformations and Actions. Transformations are those that do not return a value. In fact, nothing is evaluated during the definition of these transformation statements. Spark just creates these Direct Acyclic Graphs or DAG, which will only be evaluated at runtime. We call this lazy evaluation.

The fault tolerance aspect of RDDs allows Spark to reconstruct the transformations used to build the lineage to get back the lost data. Actions are when the transformations get evaluted along with the action that is called for that RDD.

Actions return values. For example, you can do a count on a RDD, to get the number of elements within and that value is returned. So you have an image of a base RDD shown here on the slide. The first step is loading the dataset from Hadoop. Then uou apply successive transformations on it such as filter, map, or reduce. Nothing actually happens until an action is called. The DAG is just updated each time until an action is called. This provides fault tolerance. For example, let's say a node goes offline. All it needs to do when it comes back online is to re-evaluate the graph to where it left off.

Caching is provided with Spark to enable the processing to happen in memory. If it does not fit in memory, it will spill to disk.

Spark runs on Windows or Unix-like systems. The lab exercises that are part of this course will be running with IBM BigInsights. The information on this slide shows you how you can install the standalone version yourself with all the default values that come along with it.

To install Spark, simply download the pre-built version of Spark and place a compiled version of Spark on each node of your cluster. Then you can manually start the cluster by executing ./sbin/start-master.sh . In the lab, the start script is different, and you will see this in the lab exercise guide.

Again, the default URL to the master UI is on port 8080. The lab exercise environment will be different and you will see this in the guide as well.

You can check out Spark's website for more information: Spark jobs can be written in Scala, Python or Java. Spark shells are available for Scala or Python. This course will not teach how to program in each specific language but will cover how to use them within the context of Spark. It is recommended that you have at least some programming background to understand how to code in any of these.

If you are setting up the Spark cluster yourself, you will have to make sure that you have a compatible version of the programming language you choose to use. This information can be found on Spark's website. In the lab environment, everything has been set up for you - all you do is launch up the shell and you are ready to go.

Spark itself is written in the Scala language, so it is natural to use Scala to write Spark applications. This course will cover code examples from by Scala, Python, and Java. Java 8 actually supports the functional programming style to include lambdas, which concisely captures the functionality that are executed by the Spark engine. This bridges the gap between Java and Scala for developing applications on Spark. Java 6 and 7 are supported but would require more work and an additional library to get the same amount of functionality as you would using Scala or Python.

Here we have a brief overview of Scala. Everything in Scala is an object. The primitive types that is defined by Java such as int or boolean are objects in Scala. Functions are objects in Scala and will play an important role in how applications are written for Spark.

Numbers are objects. This means that in an expression that you see here: 1 + 2 * 3 / 4 actually means that the individual numbers invoke the various identifiers +,-,*,/ with the other numbers passed in as arguments using the dot notation. Functions are objects. You can pass functions as arguments into another function. You can store them as variables. You can return them from other functions. The function declaration is the function name followed by the list of parameters and then the return type. This slide and the next is just to serve as a very brief overview of Scala. If you wish to learn more about Scala, check out its website for tutorials and guide.

Throughout this course, you will see examples in Scala that will have explanations on what it does. Remember, the focus of this course is on the context of Spark. It is not intended to teach Scala, Python or Java. Anonymous functions are very common in Spark applications. Essentially, if the function you need is only going to be required once, there is really no value in naming it. Just use it anonymously on the go and forget about it. For example, suppose you have a time flies function and it in, you just print a statement to the console. In another function, once per second, you need to call this time flies function. Without anonymous functions, you would code it like the top example be defining the time flies function. Using the anonymous function capability, you just provide the function with arguments, the right arrow, and the body of the function after the right arrow as in the bottom example. Because this is the only place you will be using this function, you do not need to name the function.

The Spark shell provides a simple way to learn Spark's API. It is also a powerful tool to analyze data interactively. The Shell is available in either Scala, which runs on the Java VM, or Python. To start up Scala, execute the command spark-shell from within the Spark's bin directory. To create an RDD from a text file, invoke the textFile method with the sc object, which is the SparkContext. We'll talk more about these functions in a later lesson.

To start up the shell for Python, you would execute the pyspark command from the same bin directory. Then, invoking the textFile command will also create an RDD for that text file.

In the lab exercise, you will start up either of the shells and run a series of RDD transformations and actions to get a feel of how to work with Spark. In a later lesson and exercise, you will get to dive deeper into RDDs. Having completed this lesson, you should now understand what Spark is all about and why you would want to use it. You should be able to list and describe the components in the Spark stack as well as understand the basics of Spark's primary abstraction, the RDDs.

You also saw how to download and install Spark's standalone if you wish to, or you can use the provided lab environment. You got a brief overview of Scala and saw how to launch and use the two Spark Shells.

You have completed this lesson. Go on to the first lab exercise and then proceed to the next lesson in this course.

Resilient Distributed Dataset and DataFrames


Learning Objectives

In this lesson you will learn about:

  • Describe Spark’s primary data abstraction
  • Understand how to create parallelized collections and external datasets
  • Work with Resilient Distributed Dataset (RDD) operations and DataFrames
  • Utilize shared variables and key-value pairs

Hi. Welcome to the Spark Fundamentals course. This lesson will cover Resilient Distributed Dataset.

After completing this lesson, you should be able to understand and describe Spark's primary data abstraction, the RDD. You should know how to create parallelized collections from internal and external datasets. You should be able to use RDD opeartions such as Transformations and Actions. Finally, I will also show you how to take advantage of Spark's shared variables and key-value pairs. Resilient Distributed Dataset (RDD) is Spark's primary abstraction. RDD is a fault tolerant collection of elements that can be parallelized. In other words, they can be made to be operated on in parallel. They are also immutable. These are the fundamental primary units of data in Spark. When RDDs are created, a direct acyclic graph (DAG) is created.

This type of operation is called transformations. Transformations makes updates to that graph, but nothing actually happens until some action is called. Actions are another type of operations. We'll talk more about this shortly. The notion here is that the graphs can be replayed on nodes that need to get back to the state it was before it went offline - thus providing fault tolerance. The elements of the RDD can be operated on in parallel across the cluster. Remember, transformations return a pointer to the RDD created and actions return values that comes from the action. There are three methods for creating a RDD. You can parallelize an existing collection. This means that the data already resides within Spark and can now be operated on in parallel.

As an example, if you have an array of data, you can create a RDD out of it by calling the parallelized method. This method returns a pointer to the RDD. So this new distributed dataset can now be operated upon in parallel throughout the cluster. The second method to create a RDD, is to reference a dataset. This dataset can come from any storage source supported by Hadoop such as HDFS, Cassandra, HBase, Amazon S3, etc.

The third method to create a RDD is from transforming an existing RDD to create a new RDD. In other words, let's say you have the array of data that you parallelized earlier. Now you want to filter out strings that are shorter than 20 characters. A new RDD is created using the filter method.

A final point on this slide. Spark supports text files, SequenceFiles and any other Hadoop InputFormat. Here is a quick example of how to create an RDD from an existing collection of data. In the examples throughout the course, unless otherwise indicated, we're going to be using Scala to show how Spark works. In the lab exercises, you will get to work with Python and Java as well. So the first thing is to launch the Spark shell. This command is located under the $SPARK_HOME/bin directory. In the lab environment, SPARK_HOME is the path to where Spark was installed.

Once the shell is up, create some data with values from 1 to 10,000. Then, create an RDD from that data using the parallelize method from the SparkContext, shown as sc on the slide. This means that the data can now be operated on in parallel. We will cover more on the SparkContext, the sc object that is invoking the parallelized function, in our programming lesson, so for now, just know that when you initialize a shell, the SparkContext, sc, is initialized for you to use. The parallelize method returns a pointer to the RDD. Remember, transformations operations such as parallelize, only returns a pointer to the RDD. It actually won't create that RDD until some action is invoked on it. With this new RDD, you can perform additional transformations or actions on it such as the filter transformation.

Another way to create a RDD is from an external dataset. In the example here, we are creating a RDD from a text file using the textFile method of the SparkContext object. You will see plenty more examples of how to create RDD throughout this course. Here we go over some basic operations. You have seen how to load a file from an external dataset. This time, however, we are loading a file from the hdfs. Loading the file creates a RDD, which is only a pointer to the file. The dataset is not loaded into memory yet. Nothing will happen until some action is called. The transformation basically updates the direct acyclic graph (DAG).

So the transformation here is saying map each line s, to the length of that line. Then, the action operation is reducing it to get the total length of all the lines. When the action is called, Spark goes through the DAG and applies all the transformation up until that point, followed by the action and then a value is returned back to the caller. A common example is a MapReduce word count. You first split up the file by words and then map each word into a key value pair with the word as the key, and the value of 1. Then you reduce by the key, which adds up all the value of the same key, effectively, counting the number of occurrences of that key. Finally, you call the collect() function, which is an action, to have it print out all the words and its occurrences. Again, you will get to work with this in the lab exercises.

You heard me mention direct acyclic graph several times now. On this slide, you will see how to view the DAG of any particular RDD. A DAG is essentially a graph of the business logic and does not get executed until an action is called -- often called lazy evaluation.

To view the DAG of a RDD after a series of transformation, use the toDebugString method as you see here on the slide. It will display the series of transformation that Spark will go through once an action is called. You read it from the bottom up. In the sample DAG shown on the slide, you can see that it starts as a textFile and goes through a series of transformation such as map and filter, followed by more map operations. Remember, that it is this behavior that allows for fault tolerance. If a node goes offline and comes back on, all it has to do is just grab a copy of this from a neighboring node and rebuild the graph back to where it was before it went offline. In the next several slides, you will see at a high level what happens when an action is executed.

Let's look at the code first. The goal here is to analyze some log files. The first line you load the log from the hadoop file system. The next two lines you filter out the messages within the log errors. Before you invoke some action on it, you tell it to cache the filtered dataset - it doesn't actually cache it yet as nothing has been done up until this point. Then you do more filters to get specific error messages relating to mysql and php followed by the count action to find out how many errors were related to each of those filters. Now let's walk through each of the steps. The first thing that happens when you load in the text file is the data is partitioned into different blocks across the cluster.

Then the driver sends the code to be executed on each block. In the example, it would be the various transformations and actions that will be sent out to the workers. Actually, it is the executor on each workers that is going to be performing the work on each block. You will see a bit more on executors in a later lesson. Then the executors read the HDFS blocks to prepare the data for the operations in parallel. After a series of transformations, you want to cache the results up until that point into memory. A cache is created. After the first action completes, the results are sent back to the driver. In this case, we're looking for messages that relate to mysql. This is then returned back to the driver.

To process the second action, Spark will use the data on the cache -- it doesn't need to go to the HDFS data again. It just reads it from the cache and processes the data from there.

Finally the results go back to the driver and we have completed a full cycle. So a quick recap. This is a subset of some of the transformations available. The full list of them can be found on Spark's website. Remember that Transformations are essentially lazy evaluations. Nothing is executed until an action is called. Each transformation function basically updates the graph and when an action is called, the graph is executed. Transformation returns a pointer to the new RDD.

I'm not going to read through this as you can do so yourself. I'll just point out some things I think are important. The flatMap function is similar to map, but each input can be mapped to 0 or more output items. What this means is that the returned pointer of the func method, should return a sequence of objects, rather than a single item. It would mean that the flatMap would flatten a list of lists for the operations that follows. Basically this would be used for MapReduce operations where you might have a text file and each time a line is read in, you split that line up by spaces to get individual keywords. Each of those lines ultimately is flatten so that you can perform the map operation on it to map each keyword to the value of one.

The join function combines two sets of key value pairs and return a set of keys to a pair of values from the two initial set. For example, you have a K,V pair and a K,W pair. When you join them together, you will get a K, (V,W) set.

The reduceByKey function aggregates on each key by using the given reduce function. This is something you would use in a WordCount to sum up the values for each word to count its occurrences.

Action returns values. Again, you can find more information on Spark's website. This is just a subset.

The collect function returns all the elements of the dataset as an array of the driver program. This is usually useful after a filter or another operation that returns a significantly small subset of data to make sure your filter function works correctly.

The count function returns the number of elements in a dataset and can also be used to check and test transformations.

The take(n) function returns an array with the first n elements. Note that this is currently not executed in parallel. The driver computes all the elements.

The foreach(func) function run a function func on each element of the dataset.

Now I want to get a bit into RDD persistence. You have seen this used already. That is the cache function. The cache function is actually the default of the persist function with the MEMORY_ONLY storage.

One of the key capability of Spark is its speed through persisting or caching. Each node stores any partitions of the cache and computes it in memory. When a subsequent action is called on the same dataset, or a derived dataset, it uses it from memory instead of having to retrieve it again. Future actions in such cases are often 10 times faster. The first time a RDD is persisted, it is kept in memory on the node. Caching is fault tolerant because if it any of the partition is lost, it will automatically be recomputed using the transformations that originally created it.

There are two methods to invoke RDD persistence. persist() and cache(). The persist() method allows you to specify a different storage level of caching. For example, you can choose to persist the data set on disk, persist it in memory but as serialized objects to save space, etc. Again the cache() method is just the default way of using persistence by storing deserialized objects in memory. The table here shows the storage levels and what it means. Basically, you can choose to store in memory or memory and disk. If a partition does not fit in the specified cache location, then it will be recomputed on the fly. You can also decide to serialized the objects before storing this. This is space efficient, but will require the RDD to deserialized before it can be read, so it takes up more CPU workload. There's also the option to replicate each partition on two cluster nodes. Finally, there is an experimental storage level storing the serialized object in Tachyon. This level reduces garbage collection overhead and allows the executors to be smaller and to share a pool of memory. You can read more about this on Spark's website.

A lot of text on this page, but don't worry. It can be used as a reference when you have to decide the type of storage level. There are tradeoffs between the different storage levels. You should analyze your current situation to decide which level works best. You can find this information here on Spark's website.

Basically if your RDD fits within the default storage level, by all means, use that. It is the fastest option to fully take advantage of Spark's design. If not, you can serialized the RDD and use the MEMORY_ONLY_SER level. Just be sure to choose a fast serialization library to make the objects more space efficient and still reasonably fast to access. Don't spill to disk unless the functions that compute your datasets are expensive or it requires a large amount of space.

If you want fast recovery, use the replicated storage levels. All levels are fully fault tolerant, but would still require the recomputing of the data. If you have a replicated copy, you can continue to work while Spark is reconstruction a lost partition.

Finally, use Tachyon if your environment has high amounts of memory or multiple applications. It allows you to share the same pool of memory and significantly reduces garbage collection costs. Also, the cached data is not lost if the individual executors crash.

On these last two slides, I'll talk about Spark's shared variables and the type of operations you can do on key-value pairs.

Spark provides two limited types of shared variables for common usage patterns: broadcast variables and accumulators. Normally, when a function is passed from the driver to a worker, a separate copy of the variables are used for each worker. Broadcast variables allow each machine to work with a read-only variable cached on each machine. Spark attempts to distribute broadcast variables using efficient algorithms. As an example, broadcast variables can be used to give every node a copy of a large dataset efficiently.

The other shared variables are accumulators. These are used for counters in sums that works well in parallel. These variables can only be added through an associated operation. Only the driver can read the accumulators value, not the tasks. The tasks can only add to it. Spark supports numeric types but programmers can add support for new types. As an example, you can use accumulator variables to implement counters or sums, as in MapReduce. Last, but not least, key-value pairs are available in Scala, Python and Java. In Scala, you create a key-value pair RDD by typing val pair = ('a' , 'b'). To access each element, invoke the ._ notation. This is not zero-index, so the ._1 will return the value in the first index and ._2 will return the value in the second index. Java is also very similar to Scala where it is not zero-index. You create the Tuple2 object in Java to create a key-value pair. In Python, it is a zero-index notation, so the value of the first index resides in index 0 and the second index is 1.

There are special operations available to RDDs of key-value pairs. In an application, you must remember to import the SparkContext package to use PairRDDFunctions such as reduceByKey. The most common ones are those that perform grouping or aggregating by a key. RDDs containing the Tuple2 object represents the key-value pairs. Tuple2 objects are simply created by writing (a, b) as long as you import the library to enable Spark's implicit conversion.

If you have custom objects as the key inside your key-value pair, remember that you will need to provide your own equals() method to do the comparison as well as a matching hashCode() method.

So in the example, you have a textFile that is just a normal RDD. Then you perform some transformations on it and it creates a PairRDD which allows it to invoke the reduceByKey method that is part of the PairRDDFunctions API.

I want to spend a little bit of time here to explain some of the syntax that you see on the slide. Note that in the first reduceByKey example with the a,b => a + b. This simply means that for the values of the same key, add them up together. In the example on the bottom of the slide, reduceByKey(_+_) uses the shorthand for anonymous function taking two parameters (a and b in our case) and adding them together, or multiplying, or any other operations for that matter. Another thing I want to point is that for the goal of brevity, all the functions are concatenated on one line. When you actually code it yourself, you may want split each of the functions up. For example, do the flatMap operation and return that to a RDD. Then from that RDD, do the map operation to create another RDD. Then finally, from that last RDD, invoke the reduceByKey method. That would yield multiple lines, but you would be able to test each of the transformation to see if it worked properly. So having completed this lesson, you should now be able to describe pretty well, RDDs. You should also understand how to create RDDs using various methods including from existing
datasets, external datasets such as a textFile or from HDFS, or even just from existing RDDs.

You saw various RDD operations and saw how to work with shared variables and key-value pairs. Next steps. Complete lab exercise #2 Working with RDD operations. Then proceed to the next lesson in this course.

Spark Application Planning


Hi. Welcome to the Spark Fundamentals course. This lesson will cover Spark application programming.
After completing this lesson, you should be able to understand the purpose and usage of
the SparkContext.This lesson will show you how to can get started by programming your
own Spark application. First you will see how to link to Spark. Next you will see how
to run some Spark examples. You should also be able to understand how to pass functions
to Spark and be able to create and run a Spark standalone application. Finally, you should
be able to submit applications to the Spark cluster.
The SparkContext is the main entry point to everything Spark. It can be used to create
RDDs and shared variables on the cluster. When you start up the Spark Shell, the SparkContext
is automatically initialized for you with the variable sc. For a Spark application,
you must first import some classes and implicit conversions and then create the SparkContext
object. The three import statements for Scala are shown on the slide here.
Each Spark application you create requires certain dependencies. The next three slides
will show you how to link to those dependencies depending on which programming language you
decide to use.
To link with Spark using Scala, you must have a compatible version of Scala with the Spark
you choose to use. For example, Spark 1.1.1 uses Scala 2.10, so make sure that you have
Scala 2.10 if you wish to write applications for Spark 1.1.1.
To write a Spark application, you must add a Maven dependency on Spark. The information
is shown on the slide here. If you wish to access a Hadoop cluster, you need to add a
dependency to that as well.
In the lab environment, this is already set up for you. The information on this page shows
how you would set up for your own Spark cluster.
Spark 1.1.1 works with Python 2.6 or higher, but not Python 3. It uses the standard CPython
interpreter, so C libraries like NumPy can be used.
To run Spark applications in Python, use the bin/spark-submit script located in the Spark's
home directory. This script will load the Spark's Java/Scala libraries and allow you
to submit applications to a cluster. If you wish to use HDFS, you will have to link to
it as well. In the lab environment, you will not need to do this as Spark is bundled with
it. You also need to import some Spark classes shown here.
Spark 1.1.1 works with Java 6 and higher. If you are using Java 8, Spark supports lambda
expressions for concisely writing functions. Otherwise, you can use the org.apache.spark.api.java.function
package with older Java versions.
As with Scala, you need to a dependency on Spark, which is available through Maven Central.
If you wish to access an HDFS cluster, you must add the dependency there as well. Last,
but not least, you need to import some Spark classes.
Once you have the dependencies established, the first thing is to do in your Spark application
before you can initialize Spark is to build a SparkConf object. This object contains information
about your application.
For example, val conf = new SparkConf().setAppName(appName).setMaster(master).
You set the application name and tell it which is the master node. The master parameter can
be a standalone Spark distribution, Mesos, or a YARN cluster URL. You can also decide
to use the local keyword string to run it in local mode. In fact, you can run local[16]
to specify the number of cores to allocate for that particular job or Spark shell as
16.
For production mode, you would not want to hardcode the master path in your program.
Instead, launch it as an argument to the spark-submit command.
Once you have the SparkConf all set up, you pass it as a parameter to the SparkContext
constructor to create the SparkContext
Here's the information for Python. It is pretty much the same information as Scala. The syntax
here is slightly different, otherwise, you are required to set up a SparkConf object
to pass as a parameter to the SparkContext object. You are also recommended to pass the
master parameter as an argument to the spark-submit operation.
Here's the same information for Java. Same idea, you need to create the SparkConf object
and pass that to the SparkContext, which in this case, would be a JavaSparkContext. Remember,
when you imported statements in the program, you imported the JavaSparkContext libraries.

Passing functions to Spark. I wanted to touch a little bit on this before we move further.
This is important to understand as you begin to think about the business logic of your
application.
The design of Spark's API relies heavily on passing functions in the driver program to
run on the cluster. When a job is executed, the Spark driver needs to tell its worker
how to process the data.
There are three methods that you can use to pass functions.
The first method to do this is using an anonymous function syntax. You saw briefly what an anonymous
function is in the first lesson. This is useful for short pieces of code. For example, here
we define the anonymous function that takes in a particular parameter x of type Int and
add one to it. Essentially, anonymous functions are useful for one-time use of the function.
In other words, you don't need to explicitly define the function to use it. You define
it as you go. Again, the left side of the => are the parameters or the arguments. The
right side of the => is the body of the function.
Another method to pass functions around Spark is to use static methods in a global singleton
object. This means that you can create a global object, in the example, it is the object MyFunctions.
Inside that object, you basically define the function func1. When the driver requires that
function, it only needs to send out the object -- the worker will be able to access it. In
this case, when the driver sends out the instructions to the worker, it just has to send out the
singleton object.
It is possible to pass reference to a method in a class instance, as opposed to a singleton
object. This would require sending the object that contains the class along with the method.
To avoid this consider copying it to a local variable within the function instead of accessing
it externally.
Example, say you have a field with the string Hello. You want to avoid calling that directly
inside a function as shown on the slide as x => field + x.
Instead, assign it to a local variable so that only the reference is passed along and
not the entire object shown val field_ = this.field.
For an example such as this, it may seem trivial, but imagine if the field object is not a simple
text Hello, but is something much larger, say a large log file. In that case, passing
by reference will have greater value by saving a lot of storage by not having to pass the
entire file.
Back to our regularly scheduled program. At this point, you should know how to link
dependencies with Spark and also know how to initialize the SparkContext. I also touched
a little bit on passing functions with Spark to give you a better view of how you can program
your business logic. This course will not focus too much on how to program business
logics, but there are examples available for you to see how it is done. The purpose is
to show you how you can create an application using a simple, but effective example which
demonstrates Spark's capabilities.
Once you have the beginning of your application ready by creating the SparkContext object,
you can start to program in the business logic using Spark's API available in Scala, Java,
or Python. You create the RDD from an external dataset or from an existing RDD. You use transformations
and actions to compute the business logic. You can take advantage of RDD persistence,
broadcast variables and/or accumulators to improve the performance of your jobs.
Here's a sample Scala application. You have your import statement. After the beginning
of the object, you see that the SparkConf is created with the application name. Then
a SparkContext is created. The several lines of code after is creating the RDD from a text
file and then performing the Hdfs test on it to see how long the iteration through the
file takes. Finally, at the end, you stop the SparkContext by calling the stop() function.
Again, just a simple example to show how you would create a Spark application. You will
get to practice this in the lab exercise.
I mentioned that there are examples available which shows the various usage of Spark. Depending
on your programming language preference, there are examples in all three languages that work
with Spark. You can view the source code of the examples on the Spark website or within
the Spark distribution itself. I provided some screenshots here to show you some of
the examples available.
On the slide, I also listed the step to run these examples. To run Scala or Java examples,
you would execute the run-example script under the Spark's home/bin directory. So for example,
to run the SparkPi application, execute run-example SparkPi, where SparkPi would be the name of
the application. Substitute that with a different application name to run that other application.
To run the sample Python applications, use the spark-submit command and provide the path
to the application.

In the next three slides, you will see another example of creating a Spark application. First
you will see how to do this in Scala. The next following sets of slides will show Python
and Java.
The application shown here counts the number of lines with 'a' and the number of lines
with 'b'. You will need to replace the YOUR_SPARK_HOME with the directory where Spark is installed,
if you wish to code this application.
Unlike the Spark shell, you have to initialize the SparkContext in a program. First you must
create a SparkConf to set up your application's name. Then you create the SparkContext by
passing in the SparkConf object. Next, you create the RDD by loading in the textFile,
and then caching the RDD. Since we will be applying a couple of transformations on it,
caching will help speed up the process, especially if the logData RDD is large. Finally, you
get the values of the RDD by executing the count action on it. End the program by printing
it out onto the console.
In Python, this application does the exact same thing, that is, count the number of lines
with 'a' in it, and the number of lines with 'b' in it. You use a SparkContext object to
create the RDDs and cache it. Then you run the transformations and actions, follow by
a print to the console. Nothing entirely new here, just a difference in syntax.
Similar to Scala and Python, in Java you need to get a JavaSparkContext. RDDs are represented
by JavaRDD. Then you run the transformations and actions on them. The lambda expressions
of Java 8 allows you to concisely write functions. Otherwise, you can use the classes in the
org.apache.spark.api.java.function package for older versions of java. The business logic
is the same as the previous two examples to count the number of a's and b's from the Readme
file. Just a matter of difference in the syntax and library names.
Up until this point, you should know how to create a Spark application using any of the
supported programming languages. Now you get to see how to run the application. You will
need to first define the dependencies. Then you have to package the application together
using system build tools such as Ant, sbt, or Maven. The examples here show how you would
do it using the various tools. You can use any tool for any of the programming languages.
For Scala, the example is shown using sbt, so you would have a simple.sbt file. In Java,
the example shows using Maven so you would have the pom.xml file. In Python, if you need
to have dependencies that requires third party libraries, then you can use the --py-files
argument to handle that.
Again, shown here are examples of what a typical directory structure would look like for the
tool that you choose.
Finally, once you have the JAR packaged created, run the spark-submit to execute the application.
In the lab exercise, you will get to practice this.
In short, you package up your application into a JAR for Scala or Java or a set of .py
or .zip files for Python.
To submit your application to the Spark cluster, you use spark-submit command, which is located
under the $SPARK_HOME/bin directory.
The options shown on the slide are the commonly used options. To see other options, just invoke
spark-submit with the help argument.
Let's briefly go over what each of these options mean.
The class option is the main entry point to your class. If it is under a package name,
you must provide the fully qualified name. The master URL is where your cluster is located.
Remember that it is recommended approach to provide the master URL here, instead of hardcoding
it in your application code.
The deploy-mode is whether you want to deploy your driver on the worker nodes (cluster)
or locally as an external client (client). The default deploy-mode is client.
The conf option is any configuration property you wish to set in key=value format.
The application jar is the file that you packaged up using one of the build tools.
Finally, if the application has any arguments, you would supply it after the jar file.
Here's an actual example of running a Spark application locally on 8 cores. The class
is the org.apache.spark.examples.SparkPi. local[8] is saying to run it locally on 8
cores. The examples.jar is located on the given path with the argument 100 to be passed
into the SparkPi application.
Having completed this lesson, you should now know how to create a standalone Spark application
and run it by submitting it to the Spark Cluster. You saw briefly on the different methods on
how to pass functions in Spark. All three programming languages were shown in this lesson.
Next steps. Copmlete lab exercise #3, Creating a Spark application and then proceed to the
next lesson in this course.

  • No labels