<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>pyspark.conf — PySpark master documentation</title>
<link rel="stylesheet" href="../../_static/nature.css" type="text/css" />
<link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../../',
VERSION: 'master',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="../../_static/jquery.js"></script>
<script type="text/javascript" src="../../_static/underscore.js"></script>
<script type="text/javascript" src="../../_static/doctools.js"></script>
<link rel="top" title="PySpark master documentation" href="../../index.html" />
<link rel="up" title="Module code" href="../index.html" />
</head>
<body role="document">
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="nav-item nav-item-0"><a href="../../index.html">PySpark master documentation</a> »</li>
<li class="nav-item nav-item-1"><a href="../index.html" accesskey="U">Module code</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<h1>Source code for pyspark.conf</h1><div class="highlight"><pre>
<span class="c">#</span>
<span class="c"># Licensed to the Apache Software Foundation (ASF) under one or more</span>
<span class="c"># contributor license agreements. See the NOTICE file distributed with</span>
<span class="c"># this work for additional information regarding copyright ownership.</span>
<span class="c"># The ASF licenses this file to You under the Apache License, Version 2.0</span>
<span class="c"># (the "License"); you may not use this file except in compliance with</span>
<span class="c"># the License. You may obtain a copy of the License at</span>
<span class="c">#</span>
<span class="c"># http://www.apache.org/licenses/LICENSE-2.0</span>
<span class="c">#</span>
<span class="c"># Unless required by applicable law or agreed to in writing, software</span>
<span class="c"># distributed under the License is distributed on an "AS IS" BASIS,</span>
<span class="c"># WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span>
<span class="c"># See the License for the specific language governing permissions and</span>
<span class="c"># limitations under the License.</span>
<span class="c">#</span>
<span class="sd">"""</span>
<span class="sd">>>> from pyspark.conf import SparkConf</span>
<span class="sd">>>> from pyspark.context import SparkContext</span>
<span class="sd">>>> conf = SparkConf()</span>
<span class="sd">>>> conf.setMaster("local").setAppName("My app")</span>
<span class="sd"><pyspark.conf.SparkConf object at ...></span>
<span class="sd">>>> conf.get("spark.master")</span>
<span class="sd">u'local'</span>
<span class="sd">>>> conf.get("spark.app.name")</span>
<span class="sd">u'My app'</span>
<span class="sd">>>> sc = SparkContext(conf=conf)</span>
<span class="sd">>>> sc.master</span>
<span class="sd">u'local'</span>
<span class="sd">>>> sc.appName</span>
<span class="sd">u'My app'</span>
<span class="sd">>>> sc.sparkHome is None</span>
<span class="sd">True</span>
<span class="sd">>>> conf = SparkConf(loadDefaults=False)</span>
<span class="sd">>>> conf.setSparkHome("/path")</span>
<span class="sd"><pyspark.conf.SparkConf object at ...></span>
<span class="sd">>>> conf.get("spark.home")</span>
<span class="sd">u'/path'</span>
<span class="sd">>>> conf.setExecutorEnv("VAR1", "value1")</span>
<span class="sd"><pyspark.conf.SparkConf object at ...></span>
<span class="sd">>>> conf.setExecutorEnv(pairs = [("VAR3", "value3"), ("VAR4", "value4")])</span>
<span class="sd"><pyspark.conf.SparkConf object at ...></span>
<span class="sd">>>> conf.get("spark.executorEnv.VAR1")</span>
<span class="sd">u'value1'</span>
<span class="sd">>>> print(conf.toDebugString())</span>
<span class="sd">spark.executorEnv.VAR1=value1</span>
<span class="sd">spark.executorEnv.VAR3=value3</span>
<span class="sd">spark.executorEnv.VAR4=value4</span>
<span class="sd">spark.home=/path</span>
<span class="sd">>>> sorted(conf.getAll(), key=lambda p: p[0])</span>
<span class="sd">[(u'spark.executorEnv.VAR1', u'value1'), (u'spark.executorEnv.VAR3', u'value3'), \</span>
<span class="sd">(u'spark.executorEnv.VAR4', u'value4'), (u'spark.home', u'/path')]</span>
<span class="sd">"""</span>
<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span><span class="s">'SparkConf'</span><span class="p">]</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">import</span> <span class="nn">re</span>
<span class="k">if</span> <span class="n">sys</span><span class="o">.</span><span class="n">version</span> <span class="o">></span> <span class="s">'3'</span><span class="p">:</span>
<span class="nb">unicode</span> <span class="o">=</span> <span class="nb">str</span>
<span class="n">__doc__</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="s">r"(\W|^)[uU](['])"</span><span class="p">,</span> <span class="s">r'\1\2'</span><span class="p">,</span> <span class="n">__doc__</span><span class="p">)</span>
<div class="viewcode-block" id="SparkConf"><a class="viewcode-back" href="../../pyspark.html#pyspark.SparkConf">[docs]</a><span class="k">class</span> <span class="nc">SparkConf</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Configuration for a Spark application. Used to set various Spark</span>
<span class="sd"> parameters as key-value pairs.</span>
<span class="sd"> Most of the time, you would create a SparkConf object with</span>
<span class="sd"> C{SparkConf()}, which will load values from C{spark.*} Java system</span>
<span class="sd"> properties as well. In this case, any parameters you set directly on</span>
<span class="sd"> the C{SparkConf} object take priority over system properties.</span>
<span class="sd"> For unit tests, you can also call C{SparkConf(false)} to skip</span>
<span class="sd"> loading external settings and get the same configuration no matter</span>
<span class="sd"> what the system properties are.</span>
<span class="sd"> All setter methods in this class support chaining. For example,</span>
<span class="sd"> you can write C{conf.setMaster("local").setAppName("My app")}.</span>
<span class="sd"> Note that once a SparkConf object is passed to Spark, it is cloned</span>
<span class="sd"> and can no longer be modified by the user.</span>
<span class="sd"> """</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">loadDefaults</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">_jvm</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">_jconf</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Create a new Spark configuration.</span>
<span class="sd"> :param loadDefaults: whether to load values from Java system</span>
<span class="sd"> properties (True by default)</span>
<span class="sd"> :param _jvm: internal parameter used to pass a handle to the</span>
<span class="sd"> Java VM; does not need to be set by users</span>
<span class="sd"> :param _jconf: Optionally pass in an existing SparkConf handle</span>
<span class="sd"> to use its parameters</span>
<span class="sd"> """</span>
<span class="k">if</span> <span class="n">_jconf</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_jconf</span> <span class="o">=</span> <span class="n">_jconf</span>
<span class="k">else</span><span class="p">:</span>
<span class="kn">from</span> <span class="nn">pyspark.context</span> <span class="kn">import</span> <span class="n">SparkContext</span>
<span class="n">SparkContext</span><span class="o">.</span><span class="n">_ensure_initialized</span><span class="p">()</span>
<span class="n">_jvm</span> <span class="o">=</span> <span class="n">_jvm</span> <span class="ow">or</span> <span class="n">SparkContext</span><span class="o">.</span><span class="n">_jvm</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_jconf</span> <span class="o">=</span> <span class="n">_jvm</span><span class="o">.</span><span class="n">SparkConf</span><span class="p">(</span><span class="n">loadDefaults</span><span class="p">)</span>
<div class="viewcode-block" id="SparkConf.set"><a class="viewcode-back" href="../../pyspark.html#pyspark.SparkConf.set">[docs]</a> <span class="k">def</span> <span class="nf">set</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
<span class="sd">"""Set a configuration property."""</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_jconf</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="nb">unicode</span><span class="p">(</span><span class="n">value</span><span class="p">))</span>
<span class="k">return</span> <span class="bp">self</span>
</div>
<div class="viewcode-block" id="SparkConf.setIfMissing"><a class="viewcode-back" href="../../pyspark.html#pyspark.SparkConf.setIfMissing">[docs]</a> <span class="k">def</span> <span class="nf">setIfMissing</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
<span class="sd">"""Set a configuration property, if not already set."""</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">key</span><span class="p">)</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
<span class="k">return</span> <span class="bp">self</span>
</div>
<div class="viewcode-block" id="SparkConf.setMaster"><a class="viewcode-back" href="../../pyspark.html#pyspark.SparkConf.setMaster">[docs]</a> <span class="k">def</span> <span class="nf">setMaster</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
<span class="sd">"""Set master URL to connect to."""</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_jconf</span><span class="o">.</span><span class="n">setMaster</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
<span class="k">return</span> <span class="bp">self</span>
</div>
<div class="viewcode-block" id="SparkConf.setAppName"><a class="viewcode-back" href="../../pyspark.html#pyspark.SparkConf.setAppName">[docs]</a> <span class="k">def</span> <span class="nf">setAppName</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
<span class="sd">"""Set application name."""</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_jconf</span><span class="o">.</span><span class="n">setAppName</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
<span class="k">return</span> <span class="bp">self</span>
</div>
<div class="viewcode-block" id="SparkConf.setSparkHome"><a class="viewcode-back" href="../../pyspark.html#pyspark.SparkConf.setSparkHome">[docs]</a> <span class="k">def</span> <span class="nf">setSparkHome</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
<span class="sd">"""Set path where Spark is installed on worker nodes."""</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_jconf</span><span class="o">.</span><span class="n">setSparkHome</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
<span class="k">return</span> <span class="bp">self</span>
</div>
<div class="viewcode-block" id="SparkConf.setExecutorEnv"><a class="viewcode-back" href="../../pyspark.html#pyspark.SparkConf.setExecutorEnv">[docs]</a> <span class="k">def</span> <span class="nf">setExecutorEnv</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">pairs</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
<span class="sd">"""Set an environment variable to be passed to executors."""</span>
<span class="k">if</span> <span class="p">(</span><span class="n">key</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span> <span class="ow">and</span> <span class="n">pairs</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">)</span> <span class="ow">or</span> <span class="p">(</span><span class="n">key</span> <span class="ow">is</span> <span class="bp">None</span> <span class="ow">and</span> <span class="n">pairs</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">):</span>
<span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s">"Either pass one key-value pair or a list of pairs"</span><span class="p">)</span>
<span class="k">elif</span> <span class="n">key</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_jconf</span><span class="o">.</span><span class="n">setExecutorEnv</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
<span class="k">elif</span> <span class="n">pairs</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
<span class="k">for</span> <span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span> <span class="ow">in</span> <span class="n">pairs</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_jconf</span><span class="o">.</span><span class="n">setExecutorEnv</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span>
<span class="k">return</span> <span class="bp">self</span>
</div>
<div class="viewcode-block" id="SparkConf.setAll"><a class="viewcode-back" href="../../pyspark.html#pyspark.SparkConf.setAll">[docs]</a> <span class="k">def</span> <span class="nf">setAll</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">pairs</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Set multiple parameters, passed as a list of key-value pairs.</span>
<span class="sd"> :param pairs: list of key-value pairs to set</span>
<span class="sd"> """</span>
<span class="k">for</span> <span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span> <span class="ow">in</span> <span class="n">pairs</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_jconf</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span>
<span class="k">return</span> <span class="bp">self</span>
</div>
<div class="viewcode-block" id="SparkConf.get"><a class="viewcode-back" href="../../pyspark.html#pyspark.SparkConf.get">[docs]</a> <span class="k">def</span> <span class="nf">get</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">defaultValue</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
<span class="sd">"""Get the configured value for some key, or return a default otherwise."""</span>
<span class="k">if</span> <span class="n">defaultValue</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span> <span class="c"># Py4J doesn't call the right get() if we pass None</span>
<span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_jconf</span><span class="o">.</span><span class="n">contains</span><span class="p">(</span><span class="n">key</span><span class="p">):</span>
<span class="k">return</span> <span class="bp">None</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_jconf</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_jconf</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">defaultValue</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="SparkConf.getAll"><a class="viewcode-back" href="../../pyspark.html#pyspark.SparkConf.getAll">[docs]</a> <span class="k">def</span> <span class="nf">getAll</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="sd">"""Get all values as a list of key-value pairs."""</span>
<span class="n">pairs</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">elem</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_jconf</span><span class="o">.</span><span class="n">getAll</span><span class="p">():</span>
<span class="n">pairs</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">elem</span><span class="o">.</span><span class="n">_1</span><span class="p">(),</span> <span class="n">elem</span><span class="o">.</span><span class="n">_2</span><span class="p">()))</span>
<span class="k">return</span> <span class="n">pairs</span>
</div>
<div class="viewcode-block" id="SparkConf.contains"><a class="viewcode-back" href="../../pyspark.html#pyspark.SparkConf.contains">[docs]</a> <span class="k">def</span> <span class="nf">contains</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">):</span>
<span class="sd">"""Does this configuration contain a given key?"""</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_jconf</span><span class="o">.</span><span class="n">contains</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="SparkConf.toDebugString"><a class="viewcode-back" href="../../pyspark.html#pyspark.SparkConf.toDebugString">[docs]</a> <span class="k">def</span> <span class="nf">toDebugString</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Returns a printable version of the configuration, as a list of</span>
<span class="sd"> key=value pairs, one per line.</span>
<span class="sd"> """</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_jconf</span><span class="o">.</span><span class="n">toDebugString</span><span class="p">()</span>
</div></div>
<span class="k">def</span> <span class="nf">_test</span><span class="p">():</span>
<span class="kn">import</span> <span class="nn">doctest</span>
<span class="p">(</span><span class="n">failure_count</span><span class="p">,</span> <span class="n">test_count</span><span class="p">)</span> <span class="o">=</span> <span class="n">doctest</span><span class="o">.</span><span class="n">testmod</span><span class="p">(</span><span class="n">optionflags</span><span class="o">=</span><span class="n">doctest</span><span class="o">.</span><span class="n">ELLIPSIS</span><span class="p">)</span>
<span class="k">if</span> <span class="n">failure_count</span><span class="p">:</span>
<span class="nb">exit</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span>
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">"__main__"</span><span class="p">:</span>
<span class="n">_test</span><span class="p">()</span>
</pre></div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<p class="logo"><a href="../../index.html">
<img class="logo" src="../../_static/spark-logo-hd.png" alt="Logo"/>
</a></p>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<form class="search" action="../../search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="nav-item nav-item-0"><a href="../../index.html">PySpark master documentation</a> »</li>
<li class="nav-item nav-item-1"><a href="../index.html" >Module code</a> »</li>
</ul>
</div>
<div class="footer" role="contentinfo">
© Copyright .
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.3.1.
</div>
</body>
</html>