Picking up the h2o MOJO model (zip file) in a Spring boot App

Viewed 1064

I am writing a Spring boot app (java class) which calls the H2o predict method which is inside another java class.

I have tested this java class independently and I can read the MOJO model if it is in the same place from where java is invoked and I can make predictions.

With the Spring boot App, This time my resultant jar of the maven project compilation cannot read that zip. I use the standard way to read the MOJO zip file using the Wrapper h2o provides.

EasyPredictModelWrapper model = new EasyPredictModelWrapper(MojoModel.load("DRF_model_python_1504558159175_1.zip"));

My Maven project structure looks like this:

enter image description here

The generated jar of the Spring boot App also contains the zip as shown below:

enter image description here

I don't understand why it gives me the error (it is an IOexception) that it cannot find the MOJO zip file.

File DRF_model_python_1504558159175_1.zip cannot be found.

I think the solutions could be: 1. Adding something in the Maven pom file so that the resultant jar knows where to pick up the model from. OR 2. If the MojoModel.load method accepts a path to the file rather than just the file name. But I think this doesn't work.

Any thoughts?

1 Answers

You need to change the way you are loading the MOJO zip file in your project, instead you need to load MOJO file as stream from resource. You can follow this example where you can see how to MOJO is placed into resource folder and then addMOJOsFromJARResource is called to include MOJO.

Project: https://github.com/h2oai/h2o-tutorials/tree/f67765bc6c68c2058d4b2786d1bbc627d3b70539/tutorials/hive_udf_template/hive_udf_mojo_template

  1. MOJO zip is stored at src/main/resources/model
  2. You would need to reference h2o-genmodel classes as below to

```

 import hex.genmodel.MojoReaderBackendFactory;
 import static hex.genmodel.MojoReaderBackendFactory.CachingStrategy;
 import hex.genmodel.MojoReaderBackend;
 import hex.genmodel.ModelMojoReader;
  1. Here is the function which includes all MOJO.zip (yes you can add multiple MOJO) into your project

```

  public void addMOJOsFromJARResource() {
      try {
        String[] mojo_names = this.getMOJONames();
        for (int i = 0; i < mojo_names.length; i++) {
            MojoReaderBackend reader =
                MojoReaderBackendFactory.createReaderBackend(
                  getClass().getResourceAsStream(
                     "/models/"+mojo_names[i]), 
                      MojoReaderBackendFactory.CachingStrategy.MEMORY);
            MojoModel model = ModelMojoReader.readFrom(reader);
            this.addModel(model);
        }
       } catch (Exception e) {
         e.printStackTrace();
        throw new RuntimeException();
        }
    }

``` This will work.

Related