Spark Configuration
Spark provides three main locations to configure the system:
- Java system properties, which control internal configuration parameters and can be set
either programmatically (by calling
System.setProperty
before creating aSparkContext
) or through JVM arguments. - Environment variables for configuring per-machine settings such as the IP address,
which can be set in the
conf/spark-env.sh
script. - Logging configuration, which is done through
log4j.properties
.
System Properties
To set a system property for configuring Spark, you need to either pass it with a -D flag to the JVM (for example java -Dspark.cores.max=5 MyProgram
) or call System.setProperty
in your code before creating your Spark context, as follows:
System.setProperty("spark.cores.max", "5")
val sc = new SparkContext(...)
Most of the configurable system properties control internal settings that have reasonable default values. However, there are at least five properties that you will commonly want to control:
Property Name | Default | Meaning |
---|---|---|
spark.executor.memory | 512m |
Amount of memory to use per executor process, in the same format as JVM memory strings (e.g. 512m , 2g ).
|
spark.serializer | org.apache.spark.serializer. JavaSerializer |
Class to use for serializing objects that will be sent over the network or need to be cached
in serialized form. The default of Java serialization works with any Serializable Java object but is
quite slow, so we recommend using org.apache.spark.serializer.KryoSerializer
and configuring Kryo serialization when speed is necessary. Can be any subclass of
org.apache.spark.Serializer .
|
spark.kryo.registrator | (none) |
If you use Kryo serialization, set this class to register your custom classes with Kryo.
It should be set to a class that extends
KryoRegistrator .
See the tuning guide for more details.
|
spark.local.dir | /tmp | Directory to use for "scratch" space in Spark, including map output files and RDDs that get stored on disk. This should be on a fast, local disk in your system. It can also be a comma-separated list of multiple directories on different disks. |
spark.cores.max | (infinite) | When running on a standalone deploy cluster or a Mesos cluster in "coarse-grained" sharing mode, how many CPU cores to request at most. The default will use all available cores offered by the cluster manager. |
Apart from these, the following properties are also available, and may be useful in some situations:
Property Name | Default | Meaning |
---|---|---|
spark.default.parallelism | 8 |
Default number of tasks to use for distributed shuffle operations (groupByKey ,
reduceByKey , etc) when not set by user.
|
spark.storage.memoryFraction | 0.66 | Fraction of Java heap to use for Spark's memory cache. This should not be larger than the "old" generation of objects in the JVM, which by default is given 2/3 of the heap, but you can increase it if you configure your own old generation size. |
spark.mesos.coarse | false | If set to "true", runs over Mesos clusters in "coarse-grained" sharing mode, where Spark acquires one long-lived Mesos task on each machine instead of one Mesos task per Spark task. This gives lower-latency scheduling for short queries, but leaves resources in use for the whole duration of the Spark job. |
spark.ui.port | 4040 | Port for your application's dashboard, which shows memory and workload data |
spark.ui.retained_stages | 1000 | How many stages the Spark UI remembers before garbage collecting. |
spark.shuffle.compress | true | Whether to compress map output files. Generally a good idea. |
spark.broadcast.compress | true | Whether to compress broadcast variables before sending them. Generally a good idea. |
spark.rdd.compress | false |
Whether to compress serialized RDD partitions (e.g. for StorageLevel.MEMORY_ONLY_SER ).
Can save substantial space at the cost of some extra CPU time.
|
spark.io.compression.codec | org.apache.spark.io. LZFCompressionCodec |
The compression codec class to use for various compressions. By default, Spark provides two
codecs: org.apache.spark.io.LZFCompressionCodec and org.apache.spark.io.SnappyCompressionCodec .
|
spark.io.compression.snappy.block.size | 32768 | Block size (in bytes) used in Snappy compression, in the case when Snappy compression codec is used. |
spark.scheduler.mode | FIFO |
The scheduling mode between
jobs submitted to the same SparkContext. Can be set to FAIR
to use fair sharing instead of queueing jobs one after another. Useful for
multi-user services.
|
spark.reducer.maxMbInFlight | 48 | Maximum size (in megabytes) of map outputs to fetch simultaneously from each reduce task. Since each output requires us to create a buffer to receive it, this represents a fixed memory overhead per reduce task, so keep it small unless you have a large amount of memory. |
spark.closure.serializer | org.apache.spark.serializer. JavaSerializer |
Serializer class to use for closures. Generally Java is fine unless your distributed functions (e.g. map functions) reference large objects in the driver program. |
spark.kryo.referenceTracking | true | Whether to track references to the same object when serializing data with Kryo, which is necessary if your object graphs have loops and useful for efficiency if they contain multiple copies of the same object. Can be disabled to improve performance if you know this is not the case. |
spark.kryoserializer.buffer.mb | 2 | Maximum object size to allow within Kryo (the library needs to create a buffer at least as large as the largest single object you'll serialize). Increase this if you get a "buffer limit exceeded" exception inside Kryo. Note that there will be one buffer per core on each worker. |
spark.broadcast.factory | org.apache.spark.broadcast. HttpBroadcastFactory |
Which broadcast implementation to use. |
spark.locality.wait | 3000 |
Number of milliseconds to wait to launch a data-local task before giving up and launching it
on a less-local node. The same wait will be used to step through multiple locality levels
(process-local, node-local, rack-local and then any). It is also possible to customize the
waiting time for each level by setting spark.locality.wait.node , etc.
You should increase this setting if your tasks are long and see poor locality, but the
default usually works well.
|
spark.locality.wait.process | spark.locality.wait | Customize the locality wait for process locality. This affects tasks that attempt to access cached data in a particular executor process. |
spark.locality.wait.node | spark.locality.wait | Customize the locality wait for node locality. For example, you can set this to 0 to skip node locality and search immediately for rack locality (if your cluster has rack information). |
spark.locality.wait.rack | spark.locality.wait | Customize the locality wait for rack locality. |
spark.worker.timeout | 60 | Number of seconds after which the standalone deploy master considers a worker lost if it receives no heartbeats. |
spark.akka.frameSize | 10 |
Maximum message size to allow in "control plane" communication (for serialized tasks and task
results), in MB. Increase this if your tasks need to send back large results to the driver
(e.g. using collect() on a large dataset).
|
spark.akka.threads | 4 | Number of actor threads to use for communication. Can be useful to increase on large clusters when the driver has a lot of CPU cores. |
spark.akka.timeout | 20 | Communication timeout between Spark nodes, in seconds. |
spark.driver.host | (local hostname) | Hostname or IP address for the driver to listen on. |
spark.driver.port | (random) | Port for the driver to listen on. |
spark.cleaner.ttl | (infinite) | Duration (seconds) of how long Spark will remember any metadata (stages generated, tasks generated, etc.). Periodic cleanups will ensure that metadata older than this duration will be forgetten. This is useful for running Spark for many hours / days (for example, running 24/7 in case of Spark Streaming applications). Note that any RDD that persists in memory for more than this duration will be cleared as well. |
spark.streaming.blockInterval | 200 | Duration (milliseconds) of how long to batch new objects coming from network receivers. |
spark.task.maxFailures | 4 | Number of individual task failures before giving up on the job. Should be greater than or equal to 1. Number of allowed retries = this value - 1. |
Environment Variables
Certain Spark settings can also be configured through environment variables, which are read from the conf/spark-env.sh
script in the directory where Spark is installed (or conf/spark-env.cmd
on Windows). These variables are meant to be for machine-specific settings, such
as library search paths. While Java system properties can also be set here, for application settings, we recommend setting
these properties within the application instead of in spark-env.sh
so that different applications can use different
settings.
Note that conf/spark-env.sh
does not exist by default when Spark is installed. However, you can copy
conf/spark-env.sh.template
to create it. Make sure you make the copy executable.
The following variables can be set in spark-env.sh
:
JAVA_HOME
, the location where Java is installed (if it’s not on your defaultPATH
)PYSPARK_PYTHON
, the Python binary to use for PySparkSPARK_LOCAL_IP
, to configure which IP address of the machine to bind to.SPARK_LIBRARY_PATH
, to add search directories for native libraries.SPARK_CLASSPATH
, to add elements to Spark’s classpath that you want to be present for all applications. Note that applications can also add dependencies for themselves throughSparkContext.addJar
– we recommend doing that when possible.SPARK_JAVA_OPTS
, to add JVM options. This includes Java options like garbage collector settings and any system properties that you’d like to pass with-D
(e.g.,-Dspark.local.dir=/disk1,/disk2
).- Options for the Spark standalone cluster scripts, such as number of cores to use on each machine and maximum memory.
Since spark-env.sh
is a shell script, some of these can be set programmatically – for example, you might
compute SPARK_LOCAL_IP
by looking up the IP of a specific network interface.
Configuring Logging
Spark uses log4j for logging. You can configure it by adding a log4j.properties
file in the conf
directory. One way to start is to copy the existing log4j.properties.template
located there.