How to serve a Spark MLlib model?

One option is to use MLeap to serve a Spark PipelineModel online with no dependencies on Spark/SparkContext. Not having to use the SparkContext is important as it will drop scoring time for a single record from ~100ms to single-digit microseconds.

In order to use it, you have to:

  • Serialize your Spark Model with MLeap utilities
  • Load the model in MLeap (does not require a SparkContext or any Spark dependencies)
  • Create your input record in JSON (not a DataFrame)
  • Score your record with MLeap

MLeap is well integrated with all the Pipeline Stages available in Spark MLlib (with the exception of LDA at the time of this writing). However, things might get a bit more complicated if you are using custom Estimators/Transformers.

Take a look at the MLeap FAQ for more info about custom transformers/estimators, performances, and integration.


From one hand, a machine learning model built with spark can't be served the way you serve in Azure ML or Amazon ML in a traditional manner.

Databricks claims to be able to deploy models using it's notebook but I haven't actually tried that yet.

On other hand, you can use a model in three ways :

  • Training on the fly inside an application then applying prediction. This can be done in a spark application or a notebook.
  • Train a model and save it if it implements an MLWriter then load in an application or a notebook and run it against your data.
  • Train a model with Spark and export it to PMML format using jpmml-spark. PMML allows for different statistical and data mining tools to speak the same language. In this way, a predictive solution can be easily moved among tools and applications without the need for custom coding. e.g from Spark ML to R.

Those are the three possible ways.

Of course, you can think of an architecture in which you have RESTful service behind which you can build using spark-jobserver per example to train and deploy but needs some development. It's not a out-of-the-box solution.

You might also use projects like Oryx 2 to create your full lambda architecture to train, deploy and serve a model.

Unfortunately, describing each of the mentioned above solution is quite broad and doesn't fit in the scope of SO.