Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

API compatibility between scala and python?

I have read a dozen pages of docs, and it seems that:

  1. I can skip learning the scala part

  2. the API is completely implemented in python (I don't need to learn scala for anything)

  3. the interactive mode works as completely and as quickly as the scala shell and troubleshooting is equally easy

  4. python modules like numpy will still be imported (no crippled python environment)

Are there fall-short areas that will make it impossible?

like image 409
Jesvin Jose Avatar asked Jun 21 '13 13:06

Jesvin Jose


2 Answers

In recent Spark releases (1.0+), we've implemented all of the missing PySpark features listed below. A few new features are still missing, such as Python bindings for GraphX, but the other APIs have achieved near parity (including an experimental Python API for Spark Streaming).

My earlier answers are reproduced below:

Original answer as of Spark 0.9

A lot has changed in the seven months since my original answer (reproduced at the bottom of this answer):

  • Spark 0.7.3 fixed the "forking JVMs with large heaps" issue.
  • Spark 0.8.1 added support for persist(), sample(), and sort().
  • The upcoming Spark 0.9 release adds partial support for custom Python -> Java serializers.
  • Spark 0.9 also adds Python bindings for MLLib (docs).
  • I've implemented tools to help keep the Java API up-to-date.

As of Spark 0.9, the main missing features in PySpark are:

  • zip() / zipPartitions.
  • Support for reading and writing non-text input formats, like Hadoop SequenceFile (there's an open pull request for this).
  • Support for running on YARN clusters.
  • Cygwin support (Pyspark works fine under Windows powershell or cmd.exe, though).
  • Support for job cancellation.

Although we've made many performance improvements, there's still a performance gap between Spark's Scala and Python APIs. The Spark users mailing list has an open thread discussing its current performance.

If you discover any missing features in PySpark, please open a new ticket on our JIRA issue tracker.

Original answer as of Spark 0.7.2:

The Spark Python Programming Guide has a list of missing PySpark features. As of Spark 0.7.2, PySpark is currently missing support for sample(), sort(), and persistence at different StorageLevels. It's also missing a few convenience methods added to the Scala API.

The Java API was in sync with the Scala API when it was released, but a number of new RDD methods have been added since then and not all of them have been added to the Java wrapper classes. There's a discussion about how to keep the Java API up-to-date at https://groups.google.com/d/msg/spark-developers/TMGvtxYN9Mo/UeFpD17VeAIJ. In that thread, I suggested a technique for automatically finding missing features, so it's just a matter of someone taking the time to add them and submit a pull request.

Regarding performance, PySpark is going to be slower than Scala Spark. Part of the performance difference stems from a weird JVM issue when forking processes with large heaps, but there's an open pull request that should fix that. The other bottleneck comes from serialization: right now, PySpark doesn't require users to explicitly register serializers for their objects (we currently use binary cPickle plus some batching optimizations). In the past, I've looked into adding support for user-customizable serializers that would allow you to specify the types of your objects and thereby use specialized serializers that are faster; I hope to resume work on this at some point.

PySpark is implemented using a regular cPython interpreter, so libraries like numpy should work fine (this wouldn't be the case if PySpark was written in Jython).

It's pretty easy to get started with PySpark; simply downloading a pre-built Spark package and running the pyspark interpreter should be enough to test it out on your personal computer and will let you evaluate its interactive features. If you like to use IPython, you can use IPYTHON=1 ./pyspark in your shell to launch Pyspark with an IPython shell.

like image 78
Josh Rosen Avatar answered Nov 15 '22 08:11

Josh Rosen


I'd like to add some points about why many people who have used both APIs recommend the Scala API. It's very difficult for me to do this without pointing out just general weaknesses in Python vs Scala and my own distaste of dynamically typed and interpreted languages for writing production quality code. So here are some reasons specific to the use case:

  1. Performance will never be quite as good as Scala, not by orders, but by fractions, this is partly because python is interpreted. This gap may widen in future as Java 8 and JIT technology becomes part of the JVM and Scala.

  2. Spark is written in Scala, so debugging Spark applications, learning how Spark works, and learning how to use Spark is much easier in Scala because you can just quite easily CTRL + B into the source code and read the lower levels of Spark to suss out what is going on. I find this particularly useful for optimizing jobs and debugging more complicated applications.

  3. Now my final point may seem like just a Scala vs Python argument, but it's highly relevant to the specific use case - that is scale and parallel processing. Scala actually stands for Scalable Language and many interpret this to mean it was specifically designed with scaling and easy multithreading in mind. It's not just about lambda's, it's head to toe features of Scala that make it the perfect language for doing Big Data and parallel processing. I have some Data Science friends that are used to Python and don't want to learn a new language, but stick to their hammer. Python is a scripting language, it was not designed for this specific use case - it's an awesome tool, but the wrong one for this job. The result is obvious in the code - their code is often 2 - 5x longer than my Scala code as Python lacks a lot of features. Furthermore they find it harder to optimize their code as they are further away from the underlying framework.

Let me put it this way, if someone knows both Scala and Python, then they will nearly always choose to use the Scala API. The only people IME that use Python are those that simply do not want to learn Scala.

like image 31
samthebest Avatar answered Nov 15 '22 07:11

samthebest