Problem:
Install and set Up Apache Spark in Local (Windows Machine)
Solution Summary:
We will install and setup Spark in local (Windows machine) and run few commands from Spark’s own Quick Start guide to get started.
Prerequisites:
- Install Java and Scala, add java install location to JAVA_HOME. Make sure there are no spaces within path.
- Download winutils (https://github.com/steveloughran/winutils/blob/master/hadoop-2.6.0/bin/winutils.exe) and add folder path to HADOOP_HOME. Winutils.exe should be inside a bin folder within the HADOOP_HOME location.
- Run: C:\Dev\Tools\WINUTILS\bin\winutils.exe chmod 777 \tmp\hive
Solution Steps:
-
Download Spark from http://spark.apache.org/downloads.html
-
Select latest version, pre-built for latest version of Hadoop.
-
-
Follow the quick start guide to get familiar with Spark: http://spark.apache.org/docs/2.1.0/quick-start.html
-
Start spark shell: bin\spark-shell --conf spark.sql.warehouse.dir=file:///C:/tmp/spark-warehouse
-
Once started, you should see info similar to:
-
Spark context Web UI available at http://10.0.75.1:4040
-
Spark context available as 'sc' (master = local[*], app id = local-1496816307399).
-
Spark session available as 'spark'.
-
-
Run within spark shell:
-
Check type of the context object: sc
-
val textFile = sc.textFile("README.md")
-
textFile.count()
-
textFile.first()
-
val linesWithSpark = textFile.filter(line => line.contains("Spark"))
-
textFile.filter(line => line.contains("Spark")).count()
-
Exit: sys.exit
-
-
Run with Python:
-
bin\pyspark
-
Check type of the context object: sc
-
textFile = sc.textFile("README.md")
-
textFile.count()
-
textFile.first()
-
linesWithSpark = textFile.filter(lambda line: "Spark" in line)
-
textFile.filter(lambda line: "Spark" in line).count()
-
Use exit() or Ctrl-Z plus Return to exit
-
-
Note:
-
Every Spark application has a driver program. Driver program launches various parallel operations on the cluster. Driver program contains the main function and defines distributed datasets on the cluster, and applies operations to them.
-
In the above example, driver program was the spark shell itself.
-
-
Driver program access Spark through the SparkContext object. SparkContext represents a connection to a computing cluster.
-
In the spark shell, a SparkConext was automatically created with the name sc (see example above).
-
-
We express our computations through operations on RDDs. RDDs are distributed collections that are automatically parallelized across the cluster, and is Spark’s abstraction for distributed data and computation. We can build RDDs from a SparkContect.
-
In the above example, the variable called textFile is an RDD, which is created from a text file on our local machine.
-
-
Various parallel operations can be run on the RDD such as counting the number of elements in the dataset, printing the first one etc. For running operations, driver programs usually maintains a set of nodes called executors.
-
Here, since we ran the spark shell in local, all work was done on a single machine; we can connect the shell to a cluster to analyze data in parallel.
-
-
We pass functions to its operators (to run them on the cluster) using lambda or => syntax with python/scala. Java 8 also support a similar syntax using its new lambda syntax.
-
We can also define functions separately and pass it to functions, without using the lambda style syntax.
-
Recent comments