Skip to content

Spark session creation


You can create Spark Session either by using methods provided by Spark directly or utility classes from NXCALS. In either case you will have to provide some details (properties) in order to correctly configure the session. You will have to chose if the session is run locally (on your computer, Local Mode) or on Yarn (Hadoop cluster, Yarn Mode). In the later case you will have to select the number of executors, number of cores and memory sizes. If you chose to use our utilities some helper classes are provided to make the process a little bit easier. For Yarn we provide so-called Flavor that helps selecting the desired size of the resources. Those properties can also be overwritten if needed to match any specific case of yours.

Creating Spark Session with NXCALS utilities in Local Mode

//Defaults to local[*] mode
SparkProperties sparkProperties = SparkProperties.defaults("MY_APP");
SparkSession sparkSession = SparkUtils.createSparkSession(sparkProperties);
# Defaults to local[*] mode
from nxcals import spark_session_builder
spark_session = spark_session_builder.get_or_create(app_name='MY_APP')

Creating Spark Session with NXCALS utilities in Yarn Mode using predefined Flavor.

Predefined application size on Yarn can be selected using the Flavor class. Please note that any Spark property can be overwritten here according to the needs.

//Using SparkSessionFlavor.SMALL, all properties pre-set, running on Yarn
SparkProperties sparkProperties = SparkProperties.remoteSessionProperties("MY_APP", SparkSessionFlavor.SMALL, "pro");
//Can overwrite (or set) any property
sparkProperties.setProperty("spark.executor.instances", "6");
SparkSession sparkSession = SparkUtils.createSparkSession(sparkProperties);
from nxcals import spark_session_builder, Flavor
# Using Flavor.YARN_SMALL, all properties pre-set but can be overwritten, running on Yarn
spark_session = spark_session_builder.get_or_create(app_name='MY_APP', flavor=Flavor.YARN_SMALL,
                              conf={'spark.executor.instances': '6'})

Creating Spark Session using Spark API in Local Mode

SparkSession sparkSession = SparkSession.builder().getOrCreate();
from pyspark.sql import SparkSession
# local session
spark_session = SparkSession.builder.getOrCreate()

Creating Spark Session using Spark API in Yarn Mode

Please note that in this case you have to specify all the required properties.

SparkConf sparkConf =  new SparkConf()
        .set("spark.submit.deployMode", "client")
        .set("spark.yarn.appMasterEnv.JAVA_HOME",  "/var/nxcals/jdk1.11")
        .set("spark.executorEnv.JAVA_HOME", "/var/nxcals/jdk1.11")
        .set("spark.yarn.jars", "hdfs:////project/nxcals/lib/spark-3.2.1/*.jar,hdfs:////project/nxcals/nxcals_lib/nxcals_pro/*.jar\"")
        .set("spark.executor.instances", "4")
        .set("spark.executor.cores", "1")
        .set("spark.executor.memory", "1g")
        .set("sql.caseSensitive", "true")
        .set("spark.kerberos.access.hadoopFileSystems", "nxcals");

SparkSession sparkSession = SparkSession.builder().config(sparkConf).getOrCreate();
from pyspark.sql import SparkSession
import os
#must set this property if using Spark APIs directly for Yarn
os.environ['PYSPARK_PYTHON'] = "./environment/bin/python"
# Yarn session with executors on the cluster
spark_session = SparkSession.builder \
    .master("yarn") \
    .config("spark.submit.deployMode", "client") \
    .config("spark.yarn.appMasterEnv.JAVA_HOME",  "/var/nxcals/jdk1.11") \
    .config("spark.executorEnv.JAVA_HOME", "/var/nxcals/jdk1.11") \
    .config("spark.yarn.jars", "hdfs:////project/nxcals/lib/spark-3.2.1/*.jar,hdfs:////project/nxcals/nxcals_lib/nxcals_pro/*.jar\"") \
    .config("spark.executor.instances", "4") \
    .config("spark.executor.cores", "1") \
    .config("spark.executor.memory", "1g") \
    .config("sql.caseSensitive", "true") \
    .config("spark.kerberos.access.hadoopFileSystems", "nxcals")\


More information about used properties for YARN can be found in Spark Apache documentation pages


There are three different configurations (Flavors) available (for application resources on the Hadoop cluster):

Configuration type Spark executor cores Spark executor instances Spark executor memory
SMALL 2 4 2g
MEDIUM 4 8 4g
LARGE 4 16 4g