Public | Automated Build

Last pushed: 2 years ago
Short Description
Apache Spark 2.0 on CentOS 7 system using Oracle JDK 8
Full Description


This repository depends on Git Large Files

Create the parana/spark Docker Image

This Dockerfile is a Automated build of Docker Registry.

Spark Components

Building on boot2docker & Docker Machine

You need to configure swap space in boot2docker / Docker Machine prior the build:

  1. Log into boot2docker / Docker Machine: boot2docker ssh or docker-machine ssh default (replace default if needed).
  2. Create a file named in /var/lib/boot2docker/ with the following content:

     dd if=/dev/zero of=$SWAPFILE bs=1024 count=2097152
     mkswap $SWAPFILE && chmod 600 $SWAPFILE && swapon $SWAPFILE
  3. Make this file executable: chmod u+x /var/lib/boot2docker/

After restarting boot2docker / Docker Machine, it will have increased swap size.

How to use

docker pull parana/spark
docker run -i -t -h my-spark \
           -p 8080:8080 -p 7077:7077 -p 8888:8888 -p 6066:6066 \
           --rm \
           parana/spark bash

You can add -v $PWD/m2-repo:/usr/local/m2-repo in case of use the host directory to persist the Maven Local Repository. In this case you need alter Dockerfile properly.

The Container Bash shell will open and you can type:

cd /usr/local/spark/sbin
./ # starting org.apache.spark.deploy.master.Master, logging to /usr/local/spark/logs/
ps -ef | grep java 
ls -lAt /usr/local/spark/logs
cat /usr/local/spark/logs/* | grep "Starting Spark master at"
# Write the spark URL to remember (suppose: "spark://my-spark:7077")
cat /usr/local/spark/logs/* | grep port
# Use the URL of Master to start the Slave
./ spark://my-spark:7077
# Test the master’s web UI
sleep 5
curl http://localhost:8080

Using Spark

Most of this content is from Spark Documentation for 2.0.2 version avaiable in, but properly organized for Data Science professionals.

Open on WEB Browser in host computer


You will see something like this.

Interactive Analysis with the Spark Shell

Spark’s shell provides a simple way to learn the API, as well as a powerful tool
to analyze data interactively. It is available in Scala which runs on the Java VM
and is thus a good way to use existing Java libraries. Start it by running the
following 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:

val textFile = sc.textFile("../")
textFile: org.apache.spark.rdd.RDD[String] = MapPartitionsRDD[1] at textFile at <console>:25

RDDs have actions, which return values, and transformations, which return
pointers to new RDDs. Let’s start with a few actions:

textFile.count() // Number of items in this RDD
res0: Long = 126
textFile.first() // First item in this RDD
res1: String = # Apache Spark

Now let’s use a transformation. We will use the filter transformation to return
a new RDD with a subset of the items in the file.

val linesWithSpark = textFile.filter(line => line.contains("Spark"))
linesWithSpark: org.apache.spark.rdd.RDD[String] = MapPartitionsRDD[2] at filter at <console>:27

We can chain together transformations and actions:

textFile.filter(line => line.contains("Spark")).count() // How many lines contain "Spark"?
res3: Long = 15

More on RDD Operations

RDD actions and transformations can be used for more complex computations.

Let’s say we want to find the line with the most words: => line.split(" ").size).reduce((a, b) => if (a > b) a else b)

This first maps a line to an integer value, creating a new RDD. Method 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:

import java.lang.Math => line.split(" ").size).reduce((a, b) => Math.max(a, b))
res5: Int = 15

One common data flow pattern is MapReduce, as popularized by Hadoop. Spark
can implement MapReduce flows easily:

val wordCounts = textFile.flatMap(line => line.split(" ")).map(word => (word, 1)).reduceByKey((a, b) => a + b)
wordCounts: org.apache.spark.rdd.RDD[(String, Int)] = ShuffledRDD[8] at reduceByKey at <console>:28

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:

res6: Array[(String, Int)] = Array((means,1), (under,2), (this,3), (Because,1), (Python,2), (agree,1), (cluster.,1), ...)


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:

res7: linesWithSpark.type = MapPartitionsRDD[2] at filter at <console>:27
res8: Long = 19

It may seem silly to use Spark to explore and cache a 100-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 bin/spark-shell to a cluster, as
described in the
programming guide.

Self-Contained Java Applications

Suppose we wish to write a self-contained application using the Spark API. We
will walk through a simple application in Java (with Maven).

This example will use Maven to compile an application JAR, but any similar
build system will work.

We’ll create a very simple Spark application,

/* */
import org.apache.spark.SparkConf;

public class SimpleApp {
  public static void main(String[] args) {
    // logFile Should be some file on your system
    String logFile = "/usr/local/spark/"; 
    SparkConf conf = new SparkConf().setAppName("Simple Application");
    JavaSparkContext sc = new JavaSparkContext(conf);
    JavaRDD<String> logData = sc.textFile(logFile).cache();

    long numAs = logData.filter(new Function<String, Boolean>() {
      public Boolean call(String s) { return s.contains("a"); }

    long numBs = logData.filter(new Function<String, Boolean>() {
      public Boolean call(String s) { return s.contains("b"); }

    System.out.println("Lines with a: " + numAs + ", lines with b: " + numBs);

This program just counts the number of lines containing ‘a’ and the number
containing ‘b’ in a text file. Note that you’ll need to replace
/usr/local/spark with the location where Spark is installed (if you will
run this code in another environment).

As with 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 The Spark programming guide describes these
differences in more detail.

To build the program, we also write a Maven pom.xml file that lists Spark as a

<project xmlns="" 
    <dependency> <!-- Spark dependency -->

This project is already created at /desenv/java/myspark, so now you can run
this Java Program using :

cd /desenv/java/myspark
/usr/local/spark/bin/spark-submit \
    --class "spark.SimpleApp" \
    --master local[4] \
    target/myspark-1.0-SNAPSHOT.jar 2> /dev/null 

See Dockerfile for details about where this files are located in host
computer. For example:

COPY test /desenv/java/

Where to Go from Here

Congratulations on running your first Spark application!

For an in-depth overview of the API, start with the
Spark programming guide,
or see “Programming Guides” menu for other components like : MLlib
for Machine Learning API.

For running applications on a cluster, head to the deployment overview.

Finally, Spark includes several samples in the examples directory (Scala,
Java, Python, R). You can run them as follows:

# For Scala and Java, use run-example:
/usr/local/spark/bin/run-example SparkPi

This shell run-example delegate the execution to
/usr/local/spark/bin/spark-class which invoke the class
org.apache.spark.deploy.SparkSubmit passing all parameters.

In the other hand this shell spark-class ensure the environment is set
running, find Spark jars, set the LAUNCH_CLASSPATH variable
and build the command to be executed. Then start JVM to run the command.

In this case of running SparkPi the command build is something like this:

/opt/jdk1.8.0_91/bin/java \
    -cp /usr/local/spark/conf/:/usr/local/spark/jars/* \
    -Xmx1g \
    org.apache.spark.deploy.SparkSubmit \
    --jars /usr/local/spark/examples/jars/scopt_2.11-3.3.0.jar,/usr/local/spark/examples/jars/spark-examples_2.11-2.0.2.jar \
    --class org.apache.spark.examples.SparkPi spark-internal

So, you can use /desenv/java/ SparkPi to run SparkPi example.

Launching Spark jobs from Java or Scala

The org.apache.spark.launcher package provides classes for launching Spark
jobs as child processes using a simple Java API.

Unit Testing

Spark is friendly to unit testing with any popular unit test framework.
Simply create a SparkContext in your test with the master URL set to local,
run your operations, and then call SparkContext.stop() to tear it down.
Make sure you stop the context within a finally block or the test
framework’s tearDown method, as Spark does not support two contexts
running concurrently in the same program.

Some examples:

package spark;

import static org.junit.Assert.*;
import java.util.*;
import org.apache.spark.SparkConf;
import org.junit.*;

public class SimpleTest {
  private JavaSparkContext sparkCtx;

  public void init() throws IllegalArgumentException, IOException {
    // ctxtBuilder = new ContextBuilder(tempFolder);
    SparkConf conf = new SparkConf();
    this.sparkCtx = new JavaSparkContext(conf);

  public void testSimpleRdd() {
    final List<Integer> nums = new ArrayList<Integer>();
    JavaRDD<Integer> rdd = this.sparkCtx.parallelize(nums, 1);
    assertEquals(3, rdd.count());

Using SparkSession (please, add spark-sql_2.11 dependency in your pom)

package spark;

import static org.apache.spark.sql.functions.*;
import java.util.List;
import org.apache.spark.sql.*;
import org.junit.*;

public class CsvTest {
  private SparkSession sparkSession;

  public void init() throws IllegalArgumentException, IOException {
    this.sparkSession = SparkSession.builder().master("local").appName("spark session example").getOrCreate();

  public void tesCsv() {
    Dataset<Row> dataset ="csv").option("header", "true").option("", "")

    List<Row> l = dataset.collectAsList();
    String columns[] = { "Nome", "Idade" };
    for (Row row : l) {
      int s = row.length();
      for (int i = 0; i < s; i++) {
        System.out.println(columns[i] + " : " + row.getString(i));

In this examples I'm using some Functions available for DataFrame.
See this link
for details.

package spark;

import static org.apache.spark.sql.functions.*;
import java.util.List;
import org.apache.spark.sql.*;
import org.junit.*;

public class CsvTest2 {
  private SparkSession sparkSession;

  public void init() throws IllegalArgumentException, IOException {
    this.sparkSession = SparkSession.builder().master("local").appName("spark session example").getOrCreate();

  public void tesCsv() {
    Dataset<Row> dataset ="csv").option("header", "true").option("", "")

    List<Row> l = dataset.collectAsList();
    // String title[] = { "Nome", "Nota" };
    for (Row row : l) {
      int s = row.length();
      for (int i = 0; i < s; i++) {
        // System.out.println(columns[i] + " : " + row.getString(i));
    String title[] = { "Nome", "stdDev(Nota)" };
    Dataset<Row> stddev = dataset.groupBy("Nome").agg(stddev_pop("Nota"));
    l = stddev.collectAsList();
    for (Row row : l) {
      int s = row.length();
      for (int i = 0; i < s; i++) {
        Object value;
        try {
          value = row.getDouble(i);
        } catch (Exception e) {
          value = row.getString(i);
        System.out.println(title[i] + " : " + value);

Spark SQL

Spark SQL is a Spark module for structured data processing. Unlike the basic
Spark RDD API, the interfaces provided by Spark SQL provide Spark with more
information about the structure of both the data and the computation being
performed. Internally, Spark SQL uses this extra information to perform
extra optimizations. There are several ways to interact with Spark SQL
including SQL and the Dataset API. When computing a result the same execution
engine is used, independent of which API/language you are using to express
the computation. This unification means that developers can easily switch
back and forth between different APIs based on which provides the most
natural way to express a given transformation.

One use of Spark SQL is to execute SQL queries. Spark SQL can also be used
to read data from an existing Apache Hive installation (see section bellow).
When running SQL from within another programming language, like Java, the
results will be returned as a Dataset/DataFrame. You can also interact with
the SQL interface using the command-line or over JDBC/ODBC.

Running SQL Queries Programmatically

The sql function on a SparkSession enables applications to run SQL queries
programmatically and returns the result as a Dataset<Row>.

import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;

// Start with JSON Dataset
Dataset<Row> df ="examples/src/main/resources/people.json");
// Print the schema in a tree format
// root
// |-- age: long (nullable = true)
// |-- name: string (nullable = true)

// Now the same Schema on SQL Database.

// Register the DataFrame as a SQL temporary view

Dataset<Row> sqlDF = spark.sql("SELECT * FROM people");;

// +----+-------+
// | age|   name|
// +----+-------+
// |null|Michael|
// |  30|   Andy|
// |  19| Justin|
// +----+-------+

// Count people by age
// +----+-----+
// | age|count|
// +----+-----+
// |  19|    1|
// |null|    1|
// |  30|    1|
// +----+-----+

Find full example code at "examples/src/main/java/org/apache/spark/examples/sql/" in the Spark Github repo.

Persistence with Apache Hive

Spark use Apache Hive SQL Persistence layer.

The Apache Hive data warehouse software facilitates reading, writing, and
managing large datasets residing in distributed storage using SQL.
Hive is built on top of Apache Hadoop.

See distributed sql engine
for details.

More on Spark 2.0

To convert the code from Scala to Java use this pom.xml dependencies :

    <dependency> <!-- Spark dependency -->
    Others useful dependencies:

    <dependency> <!-- Hadoop dependency -->
    . . . 

How to build

If you plan to change the Spark version on the pom.xml file you will need
to re-create the m2-repo directory again. To do this use the command
below to start the container.

mv m2-repo m2-repo-old

docker run -i -t -h my-spark --rm \
       --name my-spark \
       -v $PWD/m2-repo:/root/.m2/repository \
       -p 8080:8080 \
       -p 7077:7077 \
       parana/spark bash

rm -rf m2-repo-old

Doing this m2-repo will be updated on Host and a next build will be much
faster because it will use the artifacts in the Local Maven Repository
and will not need to download again.

Using Jupyter Notebook

Run this comand below on Container Bash prompt.

jupyter notebook --no-browser --ip spark.local

and open http://spark.local:8888 on host computer.

Then, you can see on host computer something like this:

Docker Pull Command
Source Repository