如何在Java UDF中加载H20训练有素的模型

时间:2019-05-27 09:15:30

标签: hadoop hdfs xgboost

我正在尝试加载经过训练的xgboost模型,以用于用Java编写的自定义UDF中。文件为zip格式,并存储在hdfs中。

我尝试使用Path类读取它,但是它不起作用。

import org.apache.hadoop.fs.Path;

public EasyPredictModelWrapper loadModel(String xgBoostModelFile) {
        if (model == null) {

            synchronized (_lockObject) {
                if (model == null) {
                    log.info("Model has not been loaded, loading ...");
                    try {
                        Path path = new Path(xgBoostModelFile);
                        model = new EasyPredictModelWrapper(MojoModel.load(path)); // Doesn't compile since MojoModel only takes string as an input.
                    } catch (IOException e) {
                        log.error("Got an exception while trying to load xgBoostModel \n", e);
                    }
                }
            }
        }
        return model;
    }

我要成功加载model.zip

1 个答案:

答案 0 :(得分:0)

在H20闲散社区中得到答案。

FileSystem fs = FileSystem.get(new Configuration());
Path path = new Path(xgBoostModelFile);
FSDataInputStream inputStream = fs.open(path);
MojoReaderBackend mojoReaderBackend = MojoReaderBackendFactory.createReaderBackend(inputStream,CachingStrategy.MEMORY);
model = new EasyPredictModelWrapper(MojoModel.load(mojoReaderBackend));