Quick Start

This tutorial provides a quick introduction to using Spark. We will first introduce the API through Spark’s interactive Scala shell (don’t worry if you don’t know Scala – you will need much for this), then show how to write standalone jobs in Scala and Java. See the programming guide for a fuller reference.

To follow along with this guide, you only need to have successfully built Spark on one machine. Simply go into your Spark directory and run:

$ sbt/sbt package

Interactive Analysis with the Spark Shell

Basics

Spark’s interactive shell provides a simple way to learn the API, as well as a powerful tool to analyze datasets interactively.
Start the shell by running ./spark-shell in the Spark directory.

Spark’s primary abstraction is a distributed collection of items called a Resilient Distributed Dataset (RDD). RDDs can be created from Hadoop InputFormats (such as HDFS files) or by transforming other RDDs. Let’s make a new RDD from the text of the README file in the Spark source directory:

This first maps a line to an integer value, creating a new RDD. reduce is called on that RDD to find the largest line count. The arguments to map and reduce are Scala function literals (closures), and can use any language feature or Scala/Java library. For example, we can easily call functions declared elsewhere. We’ll use Math.max() function to make this code easier to understand:

Here, we combined the flatMap, map and reduceByKey transformations to compute the per-word counts in the file as an RDD of (String, Int) pairs. To collect the word counts in our shell, we can use the collect action:

Caching

Spark also supports pulling data sets into a cluster-wide in-memory cache. This is very useful when data is accessed repeatedly, such as when querying a small “hot” dataset or when running an iterative algorithm like PageRank. As a simple example, let’s mark our linesWithSpark dataset to be cached:

It may seem silly to use a Spark to explore and cache a 30-line text file. The interesting part is that these same functions can be used on very large data sets, even when they are striped across tens or hundreds of nodes. You can also do this interactively by connecting spark-shell to a cluster, as described in the programming guide.

A Standalone Job in Scala

Now say we wanted to write a standalone job using the Spark API. We will walk through a simple job in both Scala (with sbt) and Java (with maven). If you using other build systems, consider using the Spark assembly JAR described in the developer guide.

We’ll create a very simple Spark job in Scala. So simple, in fact, that it’s named SimpleJob.scala:

/*** SimpleJob.scala ***/importspark.SparkContextimportSparkContext._objectSimpleJobextendsApplication{vallogFile="/var/log/syslog"// Should be some file on your systemvalsc=newSparkContext("local","Simple Job","$YOUR_SPARK_HOME","target/scala-2.9.2/simple-project_2.9.2-1.0.jar")vallogData=sc.textFile(logFile,2).cache()valnumAs=logData.filter(line=>line.contains("a")).count()valnumBs=logData.filter(line=>line.contains("b")).count()println("Lines with a: %s, Lines with b: %s".format(numAs,numBs))}

This job simply counts the number of lines containing ‘a’ and the number containing ‘b’ in a system log file. Unlike the earlier examples with the Spark shell, which initializes its own SparkContext, we initialize a SparkContext as part of the job. We pass the SparkContext constructor four arguments, the type of scheduler we want to use (in this case, a local scheduler), a name for the job, the directory where Spark is installed, and a name for the jar file containing the job’s sources. The final two arguments are needed in a distributed setting, where Spark is running across several nodes, so we include them for completeness. Spark will automatically ship the jar files you list to slave nodes.

This file depends on the Spark API, so we’ll also include an sbt configuration file, simple.sbt which explains that Spark is a dependency. This file also adds two repositories which host Spark dependencies:

Of course, for sbt to work correctly, we’ll need to layout SimpleJob.scala and simple.sbt according to the typical directory structure. Once that is in place, we can create a JAR package containing the job’s code, then use sbt run to execute our example job.

This example only runs the job locally; for a tutorial on running jobs across several machines, see the Standalone Mode documentation, and consider using a distributed input source, such as HDFS.

A Standalone Job In Java

Now say we wanted to write a standalone job using the Java API. We will walk through doing this with Maven. If you using other build systems, consider using the Spark assembly JAR described in the developer guide.

We’ll create a very simple Spark job, SimpleJob.java:

/*** SimpleJob.java ***/importspark.api.java.*;importspark.api.java.function.Function;publicclassSimpleJob{publicstaticvoidmain(String[]args){StringlogFile="/var/log/syslog";// Should be some file on your systemJavaSparkContextsc=newJavaSparkContext("local","Simple Job","$YOUR_SPARK_HOME","target/simple-project-1.0.jar");JavaRDD<String>logData=sc.textFile(logFile).cache();longnumAs=logData.filter(newFunction<String,Boolean>(){publicBooleancall(Strings){returns.contains("a");}}).count();longnumBs=logData.filter(newFunction<String,Boolean>(){publicBooleancall(Strings){returns.contains("b");}}).count();System.out.println("Lines with a: "+numAs+", lines with b: "+numBs);}}

This job simply counts the number of lines containing ‘a’ and the number containing ‘b’ in a system log file. Note that like in the Scala example, we initialize a SparkContext, though we use the special JavaSparkContext class to get a Java-friendly one. We also create RDDs (represented by JavaRDD) and run transformations on them. Finally, we pass functions to Spark by creating classes that extend spark.api.java.function.Function. The Java programming guide describes these differences in more detail.

To build the job, we also write a Maven pom.xml file that lists Spark as a dependency. Note that Spark artifacts are tagged with a Scala version.