将模型导入 应用程序 Import models into your application

本文介绍DATABRICKS ML 模型导出工作流的导入和评分部分;请参阅导出 APACHE SPARK ML 模型和管道,以获得工作流的导出部分。This article discusses the import and scoring parts of a Databricks ML Model Export workflow; see Export Apache Spark ML models and pipelines for the export part of the workflow.

若要使用通过 Databricks ML 模型导出导出的模型,请在库中调用 Api dbml-localTo use models exported via Databricks ML Model Export, you call APIs in the library dbml-local. 此库提供了 Scala 和 Java Api,用于导入模型并 (预测或推理) 执行低延迟计分。This library provides Scala and Java APIs for importing models and performing low latency scoring (prediction or inference).

在 Java 应用程序中使用已保存的模型Using saved models in Java applications

假设您导出了逻辑回归管道并将其保存在下 my_models/lr_pipelineSuppose that you have exported a logistic regression pipeline and saved it under my_models/lr_pipeline. 您可以使用 ModelFactory LocalModel 从已保存的模型的目录创建,并对新数据进行评分。You can use ModelFactory to create a LocalModel from the saved model’s directory and perform scoring on new data.

// Load exported model
String modelPath = "my_models/lr_pipeline";
LocalModel model = ModelFactory.loadModel(modelPath);

// The model input is a standard JSON string.
// The input schema here is: [origLabel: Double, features: Vector].
String input =
  "{\"origLabel\":-1.0," +
  "\"features\":{\"type\":0,\"size\":13," +
  "\"indices\":[0,2,3,4,6,7,8,9,10,11,12]," +
  "\"values\":[74.0,2.0,120.0,269.0,2.0,121.0,1.0,0.2,1.0,1.0,3.0]}" +
  "}";

// The model output is also a standard JSON string, with the expected output fields.
String output = model.transform(input);

输入接受 Apache Spark Dataset 和 DataFrames 使用方法生成的相同 JSON 格式 Dataset.toJSON (参阅数据集 API 文档) 。The input accepts the same JSON format produced by Apache Spark Datasets and DataFrames using the Dataset.toJSON method (see Dataset API docs). 有关更多详细信息,请参阅dbml-本地 API 文档See the dbml-local API docs for more details.

dbml-local在 Maven 中指定库依赖项Specifying the dbml-local library dependency in Maven

您可以 dbml-local 使用 Maven 坐标,为应用程序指定与任何其他依赖关系相同的库依赖项。You specify the dbml-local library dependency to an application just like any other dependency, with a Maven coordinate. 以下代码片段提供了一个示例,其中包含 dbml-local 在 Maven 项目 pom.xml 生成文件中。The following code snippet gives an example of including dbml-local in a Maven project pom.xml build file.

<!-- Add repository for dbml-local dependency -->
 <repositories>
   <repository>
     <snapshots>
       <enabled>false</enabled>
     </snapshots>
     <id>bintray-databricks-maven</id>
     <name>bintray</name>
     <url>https://dl.bintray.com/databricks/maven</url>
   </repository>
 </repositories>

 <dependencies>
   <!-- Main dependency for Model Scoring -->
   <dependency>
     <groupId>com.databricks</groupId>
     <artifactId>dbml-local</artifactId>
     <version>0.2.2-spark2.2</version>
   </dependency>
 </dependencies>

下载 dbml-local jarDownloading dbml-local JARs

dbml-local可以从bintray获取 jar。The dbml-local JARs are available from bintray.

dbml-localdbml-local license

dbml-local库在MIT 许可证下发布。The dbml-local library is published under the MIT license.

示例应用程序Example application

可以查看一个非常简单的示例应用程序,该应用程序演示如何 dbml-local 在 Databricks 的示例Github 存储库中使用 Databricks ML 模型导出辅助库。You can view a very simple example application that shows how to use the Databricks ML Model Export companion library dbml-local in the databricks-ml-examples Github repository. 此演示包含用于定型和导出 MLlib 模型的 Databricks 笔记本。This demo includes Databricks notebooks for training and exporting MLlib models. 这些笔记本与简单的 Java 应用程序配对,它们显示了如何导入模型并进行预测。These notebooks are paired with simple Java applications that show how to import models and make predictions.