Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customize SparkContext using sparkConf.set(..) when using spark-shell

Tags:

In Spark, there are 3 primary ways to specify the options for the SparkConf used to create the SparkContext:

  1. As properties in the conf/spark-defaults.conf
    • e.g., the line: spark.driver.memory 4g
  2. As args to spark-shell or spark-submit
    • e.g., spark-shell --driver-memory 4g ...
  3. In your source code, configuring a SparkConf instance before using it to create the SparkContext:
    • e.g., sparkConf.set( "spark.driver.memory", "4g" )

However, when using spark-shell, the SparkContext is already created for you by the time you get a shell prompt, in the variable named sc. When using spark-shell, how do you use option #3 in the list above to set configuration options, if the SparkContext is already created before you have a chance to execute any Scala statements?

In particular, I am trying to use Kyro serialization and GraphX. The prescribed way to use Kryo with GraphX is to execute the following Scala statement when customizing the SparkConf instance:

GraphXUtils.registerKryoClasses( sparkConf ) 

How do I accomplish this when running spark-shell?

like image 279
rake Avatar asked Jul 14 '15 04:07

rake


People also ask

What is SparkContext and SparkConf?

Sparkcontext is the entry point for spark environment. For every sparkapp you need to create the sparkcontext object. In spark 2 you can use sparksession instead of sparkcontext. Sparkconf is the class which gives you the various option to provide configuration parameters.

What is the difference between SparkConf and SparkSession?

SparkContext is the primary point of entry for Spark capabilities. A SparkContext represents a Spark cluster's connection that is useful in building RDDs, accumulators, and broadcast variables on the cluster. It enables your Spark Application to connect to the Spark Cluster using Resource Manager.


1 Answers

Spark 2.0+

You should be able to use SparkSession.conf.set method to set some configuration option on runtime but it is mostly limited to SQL configuration.

Spark < 2.0

You can simply stop an existing context and create a new one:

import org.apache.spark.{SparkContext, SparkConf}  sc.stop() val conf = new SparkConf().set("spark.executor.memory", "4g") val sc = new SparkContext(conf) 

As you can read in the official documentation:

Once a SparkConf object is passed to Spark, it is cloned and can no longer be modified by the user. Spark does not support modifying the configuration at runtime.

So as you can see stopping the context it is the only applicable option once shell has been started.

You can always use configuration files or --conf argument to spark-shell to set required parameters which will be used be the default context. In case of Kryo you should take a look at:

  • spark.kryo.classesToRegister
  • spark.kryo.registrator

See Compression and Serialization in Spark Configuration.

like image 187
zero323 Avatar answered Sep 21 '22 17:09

zero323