Spark: Create a simple Spark job

MapReduce is dead, long live Spark !

Following big data new trends, the logical next step for me is to start getting my head around apache Spark. Yarn is yet another resource negotiator indeed, but definitely not yet another big data application… Although you can still execute MapReduce on Yarn, writing a Yarn application is such a pain ! Have a look at below article. I reached out the same conclusion after yet another headache !

Spark runs on top of Yarn, but it does not require writing a new Client, ApplicationManager or Container classes. Anyway, let’s jump to Apache Spark and Scala. I’m describing here how to quickly install Spark / Scala on a Yarn cluster and to submit a Scala object that runs on Spark client.

Install Spark client / Scala dependencies

Install Scala:

tar xvf scala-2.10.1.tgz
mv scala-2.10.1 /usr/local/lib/
ln -s /usr/local/lib/scala-2.10.1/ /usr/local/lib/scala

Install Spark

Download a pre-built spark distribution matching your hadoop distro. Alternatively, build your own from source.

tar xvf spark-1.1.0-bin-hadoop2.4.tgz
mv spark-1.1.0-bin-hadoop2.4 /usr/local/lib/
ln -s /usr/local/lib/spark-1.1.0-bin-hadoop2.4 /usr/local/lib/spark

Update environment


Create a maven Spark application

Here are the maven dependencies I’m using. Simply replace hadoop.version with the right hadoop distribution / version. Remember to add the right maven repository to your settings.xml / artifactory / nexus should you want to use non apache distributions (such as Hortonworks, Cloudera)


Since this project will be scala based, we need to tell Maven to compile Scala objects. This can be done using below plugin

            <name>Scala-tools Maven2 Repository</name>


Project structure will be the following

-- main
--- scala
---- com
----- wordpress
------ hadoopi
------- spark
-------- MyFirstJob.scala
-- test
- target

My Simple Spark job is as follows:

package com.wordpress.hadoopi.spark
import org.apache.spark.{SparkConf, SparkContext}

object SparkGrepScala extends App {

  val sc = new SparkContext(new SparkConf().setAppName("Grep"))

  // Retrieve the key words to look for, comma separated values
  val keyWords = args(2).split(",")

  // Open file(s) in that HDFS directory
  val input = sc.textFile(args(0))

  // Filter lines containing at least one of these keywords
  val filteredLines = input.filter(line => keyWords.exists( word => line.contains(word)))

  // Output matching line to HDFS

What this simple app does is to retrieve command line arguments (keywords), read a set of input files from HDFS, filter out any line matching these comma separated keywords, and output the line back to HDFS.


Package this project as mvn clean package and upload it to your spark node (Hadoop node with Spark client installed)

Input data:

Create a new HDFS directory and put hadoop configuration for testing purpose

hadoop fs -mkdir input
hadoop fs -put /etc/hadoop/conf/*.xml input

Execute spark job using spark-submit

/usr/lib/spark/bin/spark-submit --class com.wordpress.hadoopi.spark.SparkGrepScala spark-1.0-SNAPSHOT.jar input output dfs,yarn

This will filter out any line matching either dfs or yarn keyword. Notice how fast spark is compare to MapReduce framework (Even with a tiny dataset).

Output is as follows

hadoop fs -cat output/*


And voila

It’s fast, simple to install, and it appears to work 🙂 Now, all I have to do is to learn Scala in order to be as efficient as I used to be on standard MapReduce !
Great. So if you followed this article, you created a first Scala project that runs on Spark (command line interface).



2 thoughts on “Spark: Create a simple Spark job

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s