Java library and command-line application for converting Apache Spark ML pipelines to PMML
Java
Permalink
Failed to load latest commit information.
src Cleaned up code Jan 26, 2017
LICENSE.txt Import May 5, 2016
README.md Updated version information Jan 27, 2017
pom.xml [maven-release-plugin] prepare for next development iteration Jan 27, 2017

README.md

JPMML-SparkML

Java library and command-line application for converting Apache Spark ML pipelines to PMML.

Features

Prerequisites

  • Apache Spark version 1.5.X, 1.6.X or 2.0.X.

Installation

Library

JPMML-SparkML library JAR file (together with accompanying Java source and Javadocs JAR files) is released via Maven Central Repository.

The current version is 1.1.6 (27 January, 2017).

<dependency>
    <groupId>org.jpmml</groupId>
    <artifactId>jpmml-sparkml</artifactId>
    <version>1.1.6</version>
</dependency>

Compatibility matrix:

JPMML-SparkML version Apache Spark version PMML version
1.0.0 through 1.0.8 1.5.X and 1.6.X 4.2
1.1.0 2.0.X 4.2
1.1.1 through 1.1.6 2.0.X 4.3

JPMML-SparkML depends on the latest and greatest version of the JPMML-Model library, which is in conflict with the legacy version that is part of the Apache Spark distribution.

Excluding the legacy version of JPMML-Model library from the application classpath:

<dependency>
    <groupId>org.apache.spark</groupId>
    <artifactId>spark-mllib_2.11</artifactId>
    <version>${spark.version}</version>
    <scope>provided</scope>
    <exclusions>
        <exclusion>
            <groupId>org.jpmml</groupId>
            <artifactId>pmml-model</artifactId>
        </exclusion>
    </exclusions>
</dependency>

Using the Maven Shade Plugin for "shading" all the affected org.dmg.pmml.* and org.jpmml.* classes during the packaging of the application:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>${maven.shade.version}</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <relocations>
                    <relocation>
                        <pattern>org.dmg.pmml</pattern>
                        <shadedPattern>org.shaded.dmg.pmml</shadedPattern>
                    </relocation>
                    <relocation>
                        <pattern>org.jpmml</pattern>
                        <shadedPattern>org.shaded.jpmml</shadedPattern>
                    </relocation>
                </relocations>
            </configuration>
        </execution>
    </executions>
</plugin>

For a complete example, please see the JPMML-SparkML-Bootstrap project.

Example application

Enter the project root directory and build using Apache Maven:

mvn clean install

The build produces two JAR files:

  • target/jpmml-sparkml-1.1-SNAPSHOT.jar - Library JAR file.
  • target/converter-executable-1.1-SNAPSHOT.jar - Example application JAR file.

Usage

Library

Fitting a Spark ML pipeline that only makes use of supported Transformer types:

DataFrame irisData = ...;

StructType schema = irisData.schema();

RFormula formula = new RFormula()
    .setFormula("Species ~ .");

DecisionTreeClassifier classifier = new DecisionTreeClassifier()
    .setLabelCol(formula.getLabelCol())
    .setFeaturesCol(formula.getFeaturesCol());

Pipeline pipeline = new Pipeline()
    .setStages(new PipelineStage[]{formula, classifier});

PipelineModel pipelineModel = pipeline.fit(irisData);

Converting the Spark ML pipeline to PMML using the org.jpmml.sparkml.ConverterUtil#toPMML(StructType, PipelineModel) utility method:

PMML pmml = ConverterUtil.toPMML(schema, pipelineModel);

// Viewing the result
JAXBUtil.marshalPMML(pmml, new StreamResult(System.out));

Example application

The example application JAR file contains an executable class org.jpmml.sparkml.Main, which can be used to convert a pair of serialized org.apache.spark.sql.types.StructType and org.apache.spark.ml.PipelineModel objects to PMML.

The example application JAR file does not include Apache Spark runtime libraries. Therefore, this executable class must be executed using Apache Spark's spark-submit helper script.

For example, converting a pair of Spark ML schema and pipeline serialization files src/test/resources/ser/Iris.ser and src/test/resources/ser/DecisionTreeIris.ser, respectively, to a PMML file DecisionTreeIris.pmml:

spark-submit --master local --class org.jpmml.sparkml.Main target/converter-executable-1.1-SNAPSHOT.jar --ser-schema-input src/test/resources/ser/Iris.ser --ser-pipeline-input src/test/resources/ser/DecisionTreeIris.ser --pmml-output DecisionTreeIris.pmml

Getting help:

spark-submit --master local --class org.jpmml.sparkml.Main target/converter-executable-1.1-SNAPSHOT.jar --help

License

JPMML-SparkML is licensed under the GNU Affero General Public License (AGPL) version 3.0. Other licenses are available on request.

Additional information

Please contact info@openscoring.io