Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
644665a
use tuple and namedtuple for schemardd
davies Jul 25, 2014
2c8debc
Merge branch 'master' into nested
davies Jul 30, 2014
a435b5a
add docs and code refactor
davies Jul 30, 2014
547bf3e
Merge branch 'master' into nested
davies Jul 30, 2014
bc6e9e1
switch to new Schema API
davies Jul 30, 2014
182fb46
refactor
davies Jul 30, 2014
2cc2d45
refactor
davies Jul 30, 2014
d69d397
refactor
davies Jul 30, 2014
7f6f251
address all comments
davies Jul 31, 2014
c4ddc30
fix conflict between name of fields and variables
davies Jul 31, 2014
b3559b4
use generated Row instead of namedtuple
davies Jul 31, 2014
0eaaf56
fix doc tests
davies Jul 31, 2014
84679b3
Merge branch 'master' of github.com:apache/spark into nested
davies Jul 31, 2014
9d9af55
use arrry to applySchema and infer schema in Python
davies Jul 31, 2014
f5df97f
refactor, address comments
davies Jul 31, 2014
9d8447c
apply schema provided by string of names
davies Jul 31, 2014
6b258b5
fix pep8
davies Aug 1, 2014
c79ca67
fix serialization of nested data
davies Aug 1, 2014
63de8f8
fix typo
davies Aug 1, 2014
353a3f2
fix code style
davies Aug 1, 2014
e9c0d5c
remove string typed schema
davies Aug 1, 2014
51aa135
use Row to infer schema
davies Aug 1, 2014
1e5b801
improve cache of classes
davies Aug 1, 2014
61b2292
add @deprecated to pythonToJavaMap
davies Aug 1, 2014
abe9e6e
address comments
davies Aug 1, 2014
8852aaf
check type of schema
davies Aug 1, 2014
f1d15b6
verify schema with the first few rows
davies Aug 1, 2014
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 49 additions & 20 deletions core/src/main/scala/org/apache/spark/api/python/PythonRDD.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import java.util.{List => JList, ArrayList => JArrayList, Map => JMap, Collectio
import scala.collection.JavaConversions._
import scala.language.existentials
import scala.reflect.ClassTag
import scala.util.Try
import scala.util.{Try, Success, Failure}

import net.razorvine.pickle.{Pickler, Unpickler}

Expand Down Expand Up @@ -536,25 +536,6 @@ private[spark] object PythonRDD extends Logging {
file.close()
}

/**
* Convert an RDD of serialized Python dictionaries to Scala Maps (no recursive conversions).
* It is only used by pyspark.sql.
* TODO: Support more Python types.
*/
def pythonToJavaMap(pyRDD: JavaRDD[Array[Byte]]): JavaRDD[Map[String, _]] = {
pyRDD.rdd.mapPartitions { iter =>
val unpickle = new Unpickler
iter.flatMap { row =>
unpickle.loads(row) match {
// in case of objects are pickled in batch mode
case objs: java.util.ArrayList[JMap[String, _] @unchecked] => objs.map(_.toMap)
// not in batch mode
case obj: JMap[String @unchecked, _] => Seq(obj.toMap)
}
}
}
}

private def getMergedConf(confAsMap: java.util.HashMap[String, String],
baseConf: Configuration): Configuration = {
val conf = PythonHadoopUtil.mapToConf(confAsMap)
Expand Down Expand Up @@ -701,6 +682,54 @@ private[spark] object PythonRDD extends Logging {
}
}


/**
* Convert an RDD of serialized Python dictionaries to Scala Maps (no recursive conversions).
* This function is outdated, PySpark does not use it anymore
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this function is outdated should we mark it @deprecated?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, this was never public API maybe we should just delete it instead of deprecating it?

*/
@deprecated
def pythonToJavaMap(pyRDD: JavaRDD[Array[Byte]]): JavaRDD[Map[String, _]] = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps it is too late if this was already public API... but should this be marked private[spark]?

pyRDD.rdd.mapPartitions { iter =>
val unpickle = new Unpickler
iter.flatMap { row =>
unpickle.loads(row) match {
// in case of objects are pickled in batch mode
case objs: JArrayList[JMap[String, _] @unchecked] => objs.map(_.toMap)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, it's actually fine to check for a list in order to detect batching because we're not expecting to find an RDD of lists.

// not in batch mode
case obj: JMap[String @unchecked, _] => Seq(obj.toMap)
}
}
}
}

/**
* Convert an RDD of serialized Python tuple to Array (no recursive conversions).
* It is only used by pyspark.sql.
*/
def pythonToJavaArray(pyRDD: JavaRDD[Array[Byte]], batched: Boolean): JavaRDD[Array[_]] = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private[spark]?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The whole PythonRDD is private, so does it still need this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I did not realize that. It could still perhaps be marked protected (to prevent other spark users from depending on it directly), but thats not as big of a deal.


def toArray(obj: Any): Array[_] = {
obj match {
case objs: JArrayList[_] =>
objs.toArray
case obj if obj.getClass.isArray =>
obj.asInstanceOf[Array[_]].toArray
}
}

pyRDD.rdd.mapPartitions { iter =>
val unpickle = new Unpickler
iter.flatMap { row =>
val obj = unpickle.loads(row)
if (batched) {
obj.asInstanceOf[JArrayList[_]].map(toArray)
} else {
Seq(toArray(obj))
}
}
}.toJavaRDD()
}

/**
* Convert and RDD of Java objects to and RDD of serialized Python objects, that is usable by
* PySpark.
Expand Down
8 changes: 4 additions & 4 deletions python/pyspark/rdd.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,9 @@ def map(self, f, preservesPartitioning=False):
>>> sorted(rdd.map(lambda x: (x, 1)).collect())
[('a', 1), ('b', 1), ('c', 1)]
"""
def func(split, iterator):
def func(_, iterator):
return imap(f, iterator)
return PipelinedRDD(self, func, preservesPartitioning)
return self.mapPartitionsWithIndex(func, preservesPartitioning)

def flatMap(self, f, preservesPartitioning=False):
"""
Expand Down Expand Up @@ -1184,7 +1184,7 @@ def func(split, iterator):
if not isinstance(x, basestring):
x = unicode(x)
yield x.encode("utf-8")
keyed = PipelinedRDD(self, func)
keyed = self.mapPartitionsWithIndex(func)
keyed._bypass_serializer = True
keyed._jrdd.map(self.ctx._jvm.BytesToString()).saveAsTextFile(path)

Expand Down Expand Up @@ -1382,7 +1382,7 @@ def add_shuffle_key(split, iterator):
yield pack_long(split)
yield outputSerializer.dumps(items)

keyed = PipelinedRDD(self, add_shuffle_key)
keyed = self.mapPartitionsWithIndex(add_shuffle_key)
keyed._bypass_serializer = True
with _JavaStackTrace(self.context) as st:
pairRDD = self.ctx._jvm.PairwiseRDD(
Expand Down
Loading