系统通过服务网关提供实时接口,供外部进行模型计算的调用,输入json格式的输入数据, 输出为模型结果返回
以curl方式为例:
curl -X POST -H 'Content-Type: application/json' \ -d '{ "sepal.width": 1.2, "sepal.length": 2.5, "petal.width": 3.0, "petal.length": 1.2 }' \ http://localhost:11524/consumer/calc/test-01
{ "status":2000, "message":"计算成功", "data":{ "probability(Setosa)":0.0, "probability(Virginica)":0.125, "probability(Versicolor)":0.875, "variety":"Versicolor" } }
当前批量数据调用仅支持针对oracle、mysql数据库的批量调用,hive的批量调用 建议采用udf方式进行
需要使用eppdev-mlib-batch任务包,具体所需文件为两个:
上述两个文件需要在统一目录
需要准备模型计算所需数据宽表和结果回填表两个表,其中宽表数据中需要有唯一主键, 输出结果表需要有主键和create_time字段,便于进行库表数据对应,示例格式如下
-- -------------------------------------- -- tableName:test_model_input -- author: jinlong.hao -- date: 2019-11-24 -- desc: -- 1. 模型输入库表 -- 2. 主键为id -- 3. 用于进行模型测试 -- --------------------------------------- CREATE TABLE test_model_input( id CHAR(32) comment '主键' ,sepal_width DOUBLE comment 'sepal.width' ,sepal_height DOUBLE comment 'sepal.height' ,petal_width DOUBLE comment 'petal.width' ,petal_height DOUBLE comment 'petal.height' ,create_time DOUBLE comment '创建时间,用于进行增量计算' ) comment '模型测试输入表'; -- -------------------------------------- -- tableName:test_model_output -- author: jinlong.hao -- date: 2019-11-24 -- desc: -- 1. 模型输出结果表 -- 2. 主键为id -- --------------------------------------- CREATE TABLE test_model_output( id VARCHAR(32) comment '唯一主键' ,probability_setosa DOUBLE comment 'Setosa概率' ,probability_virginica DOUBLE comment 'Virginica概率' ,probability_versicolor DOUBLE comment 'Versicolor概率' ,variety VARCHAR(20) comment '模型预测结果' ,create_time datetime comment '模型计算时间' ) comment '模型测试输出表'; ~~~
修改application.properties文件,主要修改以下内容:
其中spring.datasource.*配置模式同普通springboot工程, eppdev.mlib相关配置示例如下:
eppdev.mlib.consumer.basic-url = http://localhost:11524/consumer eppdev.mlib.batch.model-code = test-01 eppdev.mlib.batch.input-table.name = test_model_input eppdev.mlib.batch.input-table.key = id eppdev.mlib.batch.input-table.columns = id, sepal_width as `sepal.width`, sepal_height as `sepal.height`, petal_width as `petal.width`, petal_height as `petal.height` eppdev.mlib.batch.input-table.where = create_time >= ${begin_time} and create_time <= ${end_time} eppdev.mlib.batch.fetch-size = 1000 eppdev.mlib.batch.output-table.name = test_model_output eppdev.mlib.batch.output-table.key = id eppdev.mlib.batch.output-table.column-maps = probability(Setosa) as probability_setosa, probability(Virginica) as probability_virginica, probability(Versicolor) as probability_versicolor, variety as variety
./eppdev-mlib-sdk-batch.jar -Dbegin_time="2019-11-20 11:24:32" -Dend_time="2019-11-21 12:23:24"
使用hive udf进行模型调用,有以下两种方案:
网关地址url、模型编码信息
目前eppdev-mlib提供的原生udf支持hive1.2, 2.3, 3.1三个版本,需要分别下载不同 的jar包来完成模型的调用:
以hive2.3为例:
hdfs dfs put eppdev-mlib-sdk-hive-udf23.jar /user/udf/hive/
CREATE FUNCTION eppdev_to_json AS 'cn.eppdev.mlib.sdk.hive.udf.EppdevMlibToJsonUDF' USING jar 'hdfs://user/udf/hive/eppdev-mlib-sdk-hive-udf23.jar'; CREATE FUNCTION eppdev_mlib_calc AS 'cn.eppdev.mlib.sdk.hive.udf.EppdevMlibCalcUDF' USING jar 'hdfs://user/udf/hive/eppdev-mlib-sdk-hive-udf23.jar';
进行hive调用可以有两种方式:
方法1: 三个参数获取结果json
SELECT eppdev_mlib_calc( 'http://localhost:11541/consumer', 'test-01', eppdev_to_json( 'sepal.width', sepal_with, 'sepal.height', sepal_height, 'petal.width', petal_width, 'petal.height', petal_height ) ) AS full_result FROM iris_data;
输出结果为全量的json:
{ "probability(Setosa)": 1.0, "probability(Virginica)": 0.0, "probability(Versicolor)": 0.0, "variety": "Setosa" }
方法2:输入4个参数,直接获取具体结果
SELECT eppdev_mlib_calc( 'http://localhost:11541/consumer', 'test-01', eppdev_to_json( 'sepal.width', sepal_with, 'sepal.height', sepal_height, 'petal.width', petal_width, 'petal.height', petal_height ), 'variety' ) AS variety FROM iris_data;
输出结果为veriety结果,如:Setosa