Skip to content

Commit

Permalink
Update docs for PySpark input formats
Browse files Browse the repository at this point in the history
  • Loading branch information
MLnick committed Jun 5, 2014
1 parent a985492 commit 5ebacfa
Showing 1 changed file with 26 additions and 22 deletions.
48 changes: 26 additions & 22 deletions docs/programming-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -381,32 +381,32 @@ Some notes on reading files with Spark:
Apart from reading files as a collection of lines,
`SparkContext.wholeTextFiles` lets you read a directory containing multiple small text files, and returns each of them as (filename, content) pairs. This is in contrast with `textFile`, which would return one record per line in each file.

## SequenceFile and Hadoop InputFormats
### SequenceFile and Hadoop InputFormats

In addition to reading text files, PySpark supports reading [SequenceFile](http://hadoop.apache.org/common/docs/current/api/org/apache/hadoop/mapred/SequenceFileInputFormat.html)
and any arbitrary [InputFormat](http://hadoop.apache.org/docs/current/api/org/apache/hadoop/mapred/InputFormat.html).

### Writable Support
#### Writable Support

PySpark SequenceFile support loads an RDD within Java, and pickles the resulting Java objects using
[Pyrolite](https://github.com/irmen/Pyrolite/). The following Writables are automatically converted:

<table class="table">
<tr><th>Writable Type</th><th>Scala Type</th><th>Python Type</th></tr>
<tr><td>Text</td><td>String</td><td>unicode str</td></tr>
<tr><td>IntWritable</td><td>Int</td><td>int</td></tr>
<tr><td>FloatWritable</td><td>Float</td><td>float</td></tr>
<tr><td>DoubleWritable</td><td>Double</td><td>float</td></tr>
<tr><td>BooleanWritable</td><td>Boolean</td><td>bool</td></tr>
<tr><td>BytesWritable</td><td>Array[Byte]</td><td>bytearray</td></tr>
<tr><td>NullWritable</td><td>null</td><td>None</td></tr>
<tr><td>ArrayWritable</td><td>Array[T]</td><td>list of primitives, or tuple of objects</td></tr>
<tr><td>MapWritable</td><td>java.util.Map[K, V]</td><td>dict</td></tr>
<tr><td>Custom Class</td><td>Custom Class conforming to Java Bean conventions</td>
<tr><th>Writable Type</th><th>Python Type</th></tr>
<tr><td>Text</td><td>unicode str</td></tr>
<tr><td>IntWritable</td><td>int</td></tr>
<tr><td>FloatWritable</td><td>float</td></tr>
<tr><td>DoubleWritable</td><td>float</td></tr>
<tr><td>BooleanWritable</td><td>bool</td></tr>
<tr><td>BytesWritable</td><td>bytearray</td></tr>
<tr><td>NullWritable</td><td>None</td></tr>
<tr><td>ArrayWritable</td><td>list of primitives, or tuple of objects</td></tr>
<tr><td>MapWritable</td><td>dict</td></tr>
<tr><td>Custom Class conforming to Java Bean conventions</td>
<td>dict of public properties (via JavaBean getters and setters) + __class__ for the class type</td></tr>
</table>

### Loading SequenceFiles
#### Loading SequenceFiles

Similarly to text files, SequenceFiles can be loaded by specifying the path. The key and value
classes can be specified, but for standard Writables it should work without requiring this.
Expand All @@ -420,10 +420,9 @@ classes can be specified, but for standard Writables it should work without requ
(3.0, u'cc'),
(2.0, u'bb'),
(1.0, u'aa')]
>>> help(sc.sequenceFile) # Show sequencefile documentation
{% endhighlight %}

### Loading Arbitrary Hadoop InputFormats
#### Loading Arbitrary Hadoop InputFormats

PySpark can also read any Hadoop InputFormat, for both 'new' and 'old' Hadoop APIs. If required,
a Hadoop configuration can be passed in as a Python dict. Here is an example using the
Expand All @@ -439,23 +438,28 @@ $ SPARK_CLASSPATH=/path/to/elasticsearch-hadoop.jar ./bin/pyspark
{u'field1': True,
u'field2': u'Some Text',
u'field3': 12345})
>>> help(sc.newAPIHadoopRDD) # Show help for new API Hadoop RDD
{% endhighlight %}

Note that, if the InputFormat simply depends on a Hadoop configuration and/or input path, and
the key and value classes can easily be converted according to the above table,
then this approach should work well for such cases.

If you have custom serialized binary data (like pulling data from Cassandra / HBase) or custom
classes that don't conform to the JavaBean requirements, then you will probably have to first
classes that don't conform to the JavaBean requirements, then you will first need to
transform that data on the Scala/Java side to something which can be handled by Pyrolite's pickler.
A [Converter](api/scala/index.html#org.apache.spark.api.python.Converter) trait is provided
for this. Simply extend this trait and implement your transformation code in the ```convert```
method. The ensure this class is packaged into your Spark job jar and included on the PySpark
classpath.

Future support for custom 'converter' functions for keys/values that allows this to be written in Java/Scala,
and called from Python, as well as support for writing data out as SequenceFileOutputFormat
and other OutputFormats, is forthcoming.
See the [Python examples]({{site.SPARK_GITHUB_URL}}/tree/master/examples/src/main/python) and
the [Converter examples]({{site.SPARK_GITHUB_URL}}/tree/master/examples/src/main/scala/pythonconverters)
for examples using HBase and Cassandra.

</div>
Future support for writing data out as SequenceFileOutputFormat and other OutputFormats,
is forthcoming.

</div>

</div>

Expand Down

0 comments on commit 5ebacfa

Please sign in to comment.