running spark in mesosphere

In this article i will illustrate how to install spark package and run a spark application in mesosphere. I will be using dcos cli to illustrate the same and below is the link for installing and configuring dcos cli

Lets start with a simple spark program which we will be deploying in mesosphere. Below is the code

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.RowFactory;
import org.apache.spark.sql.SparkSession;
import org.apache.spark.sql.types.DataTypes;
import org.apache.spark.sql.types.StructType;

public class Test {

public static void main(String[] args) {

SparkSession spark = SparkSession.builder().appName("mesosphere_example").master("local").getOrCreate();
List<Row> list = new ArrayList<Row>();

List<org.apache.spark.sql.types.StructField> listOfStructField = new ArrayList<org.apache.spark.sql.types.StructField>();
listOfStructField.add(DataTypes.createStructField("test", DataTypes.StringType, true));
StructType structType = DataTypes.createStructType(listOfStructField);
Dataset<Row> data = spark.createDataFrame(list, structType);;

//Lets create the dataset of row using the Arrays asList Function

Dataset<Row> test = spark
.createDataFrame(Arrays.asList(new Movie("movie1", 2323d, "1212"), new Movie("movie2", 2323d, "1212"),
new Movie("movie3", 2323d, "1212"), new Movie("movie4", 2323d, "1212")), Movie.class);;

Below is the movie class which is used in the above code


public class Movie implements Serializable {

private String name;
private Double rating;
private String timestamp;

public Movie(String name, Double rating, String timestamp) {
super(); = name;
this.rating = rating;
this.timestamp = timestamp;

public Movie() {


public String getName() {
return name;

public void setName(String name) { = name;

public Double getRating() {
return rating;

public void setRating(Double rating) {
this.rating = rating;

public String getTimestamp() {
return timestamp;

public void setTimestamp(String timestamp) {
this.timestamp = timestamp;


Below is the pom file which can be used to create jar file

<project xmlns=""


<!-- -->

<!-- -->
<!-- -->
<!-- -->


<!-- Maven shade plug-in that creates uber JARs -->


<!-- Additional configuration. -->




Lets deploy the jar file created above into mesosphere, login to the cluster using the below command

dcos.exe cluster setup <cluster url> --username=<username> --password=<password>

If we have ssl certificates we can set the same or we can disable the ssl verification using below command

dcos.exe config set core.ssl_verify false

Below is the command to install spark package in mesosphere

dcos package install spark --options <file location of json to install the spark package>

Below is the an example of json file that can be used

"service": {
"name": "/sandbox/username/spark",
"cpus": 1,
"mem": 1024,
"role": "*",
"service_account": "",
"service_account_secret": "",
"user": "root",
"docker-image": "",
"log-level": "INFO",
"virtual_network_enabled": false,
"virtual_network_name": "dcos",
"virtual_network_plugin_labels": [],
"UCR_containerizer": true,
"docker_user": "root",
"use_bootstrap_for_IP_detect": false
"security": {
"kerberos": {
"enabled": false,
"kdc": {},
"krb5conf": ""
"hdfs": {}

Once we have a spark service running we can submit spark job to the service using the below command

dcos spark run --submit-args="--class com.spark.driver.example.Test https://<artifactory-ip>/spark-example.jar" --name=/sandbox/username/spark

To verify whether the spark job is running as expected, we can login to the mesosphere ui and navigate to the spark service and verify the spark job that was submitted using the logs in mesosphere service ui.