|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.ml.pmml |
| 19 | + |
| 20 | +import java.io.{File, OutputStream, StringWriter} |
| 21 | +import javax.xml.transform.stream.StreamResult |
| 22 | + |
| 23 | +import org.jpmml.model.JAXBUtil |
| 24 | + |
| 25 | +import org.apache.spark.SparkContext |
| 26 | +import org.apache.spark.annotation.{DeveloperApi, Experimental, Since} |
| 27 | +import org.apache.spark.mllib.pmml.export.PMMLModelExportFactory |
| 28 | + |
| 29 | +/** |
| 30 | + * :: DeveloperApi :: |
| 31 | + * Export model to the PMML format |
| 32 | + * Predictive Model Markup Language (PMML) is an XML-based file format |
| 33 | + * developed by the Data Mining Group (www.dmg.org). |
| 34 | + * Based on [[org.apache.spark.mllib.pmml.Exportable]] |
| 35 | + */ |
| 36 | +@DeveloperApi |
| 37 | +@Since("1.6.0") |
| 38 | +trait PMMLExportable { |
| 39 | + |
| 40 | + /** |
| 41 | + * Export the model to the stream result in PMML format. |
| 42 | + */ |
| 43 | + private[spark] def toPMML(streamResult: StreamResult): Unit |
| 44 | + |
| 45 | + /** |
| 46 | + * :: Experimental :: |
| 47 | + * Export the model to a local file in PMML format |
| 48 | + */ |
| 49 | + @Experimental |
| 50 | + @Since("1.6.0") |
| 51 | + def toPMML(localPath: String): Unit = { |
| 52 | + toPMML(new StreamResult(new File(localPath))) |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * :: Experimental :: |
| 57 | + * Export the model to a directory on a distributed file system in PMML format. |
| 58 | + * Models should override if they may contain more data than |
| 59 | + * is reasonable to store locally. |
| 60 | + */ |
| 61 | + @Experimental |
| 62 | + @Since("1.6.0") |
| 63 | + def toPMML(sc: SparkContext, path: String): Unit = { |
| 64 | + val pmml = toPMML() |
| 65 | + sc.parallelize(Array(pmml), 1).saveAsTextFile(path) |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * :: Experimental :: |
| 70 | + * Export the model to the OutputStream in PMML format |
| 71 | + */ |
| 72 | + @Experimental |
| 73 | + @Since("1.6.0") |
| 74 | + def toPMML(outputStream: OutputStream): Unit = { |
| 75 | + toPMML(new StreamResult(outputStream)) |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * :: Experimental :: |
| 80 | + * Export the model to a String in PMML format |
| 81 | + */ |
| 82 | + @Experimental |
| 83 | + @Since("1.6.0") |
| 84 | + def toPMML(): String = { |
| 85 | + val writer = new StringWriter |
| 86 | + toPMML(new StreamResult(writer)) |
| 87 | + writer.toString |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments