I used the sbt-unidoc plugin (https://github.com/sbt/sbt-unidoc) to create a unified Scaladoc of our public packages, and generate Javadocs as well. One limitation is that I haven't found an easy way to exclude packages in the Javadoc; there is a SBT task that identifies Java sources to run javadoc on, but it's been very difficult to modify it from outside to change what is set in the unidoc package. Some SBT-savvy people should help with this. The Javadoc site also lacks package-level descriptions and things like that, so we may want to look into that. We may decide not to post these right now if it's too limited compared to the Scala one. Example of the built doc site: http://people.csail.mit.edu/matei/spark-unified-docs/ Author: Matei Zaharia <matei@databricks.com> This patch had conflicts when merged, resolved by Committer: Patrick Wendell <pwendell@gmail.com> Closes #457 from mateiz/better-docs and squashes the following commits: a63d4a3 [Matei Zaharia] Skip Java/Scala API docs for Python package 5ea1f43 [Matei Zaharia] Fix links to Java classes in Java guide, fix some JS for scrolling to anchors on page load f05abc0 [Matei Zaharia] Don't include java.lang package names 995e992 [Matei Zaharia] Skip internal packages and class names with $ in JavaDoc a14a93c [Matei Zaharia] typo 76ce64d [Matei Zaharia] Add groups to Javadoc index page, and a first package-info.java ed6f994 [Matei Zaharia] Generate JavaDoc as well, add titles, update doc site to use unified docs acb993d [Matei Zaharia] Add Unidoc plugin for the projects we want Unidoced
6.9 KiB
layout | title |
---|---|
global | Python Programming Guide |
The Spark Python API (PySpark) exposes the Spark programming model to Python. To learn the basics of Spark, we recommend reading through the Scala programming guide first; it should be easy to follow even if you don't know Scala. This guide will show how to use the Spark features described there in Python.
Key Differences in the Python API
There are a few key differences between the Python and Scala APIs:
- Python is dynamically typed, so RDDs can hold objects of multiple types.
- PySpark does not yet support a few API calls, such as
lookup
and non-text input files, though these will be added in future releases.
In PySpark, RDDs support the same methods as their Scala counterparts but take Python functions and return Python collection types.
Short functions can be passed to RDD methods using Python's lambda
syntax:
{% highlight python %} logData = sc.textFile(logFile).cache() errors = logData.filter(lambda line: "ERROR" in line) {% endhighlight %}
You can also pass functions that are defined with the def
keyword; this is useful for longer functions that can't be expressed using lambda
:
{% highlight python %} def is_error(line): return "ERROR" in line errors = logData.filter(is_error) {% endhighlight %}
Functions can access objects in enclosing scopes, although modifications to those objects within RDD methods will not be propagated back:
{% highlight python %} error_keywords = ["Exception", "Error"] def is_error(line): return any(keyword in line for keyword in error_keywords) errors = logData.filter(is_error) {% endhighlight %}
PySpark will automatically ship these functions to executors, along with any objects that they reference. Instances of classes will be serialized and shipped to executors by PySpark, but classes themselves cannot be automatically distributed to executors. The Standalone Use section describes how to ship code dependencies to executors.
In addition, PySpark fully supports interactive use---simply run ./bin/pyspark
to launch an interactive shell.
Installing and Configuring PySpark
PySpark requires Python 2.6 or higher. PySpark applications are executed using a standard CPython interpreter in order to support Python modules that use C extensions. We have not tested PySpark with Python 3 or with alternative Python interpreters, such as PyPy or Jython.
By default, PySpark requires python
to be available on the system PATH
and use it to run programs; an alternate Python executable may be specified by setting the PYSPARK_PYTHON
environment variable in conf/spark-env.sh
(or .cmd
on Windows).
All of PySpark's library dependencies, including Py4J, are bundled with PySpark and automatically imported.
Standalone PySpark applications should be run using the bin/pyspark
script, which automatically configures the Java and Python environment using the settings in conf/spark-env.sh
or .cmd
.
The script automatically adds the bin/pyspark
package to the PYTHONPATH
.
Interactive Use
The bin/pyspark
script launches a Python interpreter that is configured to run PySpark applications. To use pyspark
interactively, first build Spark, then launch it directly from the command line without any options:
{% highlight bash %} $ sbt/sbt assembly $ ./bin/pyspark {% endhighlight %}
The Python shell can be used explore data interactively and is a simple way to learn the API:
{% highlight python %}
words = sc.textFile("/usr/share/dict/words") words.filter(lambda w: w.startswith("spar")).take(5) [u'spar', u'sparable', u'sparada', u'sparadrap', u'sparagrass'] help(pyspark) # Show all pyspark functions {% endhighlight %}
By default, the bin/pyspark
shell creates SparkContext that runs applications locally on all of
your machine's logical cores.
To connect to a non-local cluster, or to specify a number of cores, set the MASTER
environment variable.
For example, to use the bin/pyspark
shell with a standalone Spark cluster:
{% highlight bash %} $ MASTER=spark://IP:PORT ./bin/pyspark {% endhighlight %}
Or, to use exactly four cores on the local machine:
{% highlight bash %} $ MASTER=local[4] ./bin/pyspark {% endhighlight %}
IPython
It is also possible to launch PySpark in IPython, the
enhanced Python interpreter. PySpark works with IPython 1.0.0 and later. To
use IPython, set the IPYTHON
variable to 1
when running bin/pyspark
:
{% highlight bash %} $ IPYTHON=1 ./bin/pyspark {% endhighlight %}
Alternatively, you can customize the ipython
command by setting IPYTHON_OPTS
. For example, to launch
the IPython Notebook with PyLab graphing support:
{% highlight bash %} $ IPYTHON_OPTS="notebook --pylab inline" ./bin/pyspark {% endhighlight %}
IPython also works on a cluster or on multiple cores if you set the MASTER
environment variable.
Standalone Programs
PySpark can also be used from standalone Python scripts by creating a SparkContext in your script and running the script using bin/pyspark
.
The Quick Start guide includes a complete example of a standalone Python application.
Code dependencies can be deployed by listing them in the pyFiles
option in the SparkContext constructor:
{% highlight python %} from pyspark import SparkContext sc = SparkContext("local", "App Name", pyFiles=['MyFile.py', 'lib.zip', 'app.egg']) {% endhighlight %}
Files listed here will be added to the PYTHONPATH
and shipped to remote worker machines.
Code dependencies can be added to an existing SparkContext using its addPyFile()
method.
You can set configuration properties by passing a SparkConf object to SparkContext:
{% highlight python %} from pyspark import SparkConf, SparkContext conf = (SparkConf() .setMaster("local") .setAppName("My app") .set("spark.executor.memory", "1g")) sc = SparkContext(conf = conf) {% endhighlight %}
API Docs
API documentation for PySpark is available as Epydoc. Many of the methods also contain doctests that provide additional usage examples.
Libraries
MLlib is also available in PySpark. To use it, you'll need NumPy version 1.4 or newer. The MLlib guide contains some example applications.
Where to Go from Here
PySpark also includes several sample programs in the python/examples
folder.
You can run them by passing the files to pyspark
; e.g.:
./bin/pyspark python/examples/wordcount.py
Each program prints usage help when run without arguments.