如何在javaudf中加载h20训练模型

jtw3ybtb  于 2021-05-29  发布在  Hadoop
关注(0)|答案(1)|浏览(388)

我正在尝试加载经过训练的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

x8goxv8g

x8goxv8g1#

在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));

相关问题