This topic describes how to develop custom processors by using Java.
Syntax
To develop a custom processor by using Java, you need to define only one class. In addition to the constructor, this class requires only the Load() and Process() functions. The following information specifies the prototype of the class:
package com.alibaba.eas;
import java.util.*;
public class TestProcessor {
public TestProcessor(String modelEntry, String modelConfig) {
/* Pass the model file name for initialization. */
}
public void Load() {
/* Load model information based on the model name. */
}
public byte[] Process(byte[] input) {
/* Perform predictions based on the input data and return the prediction results. BYTE[] and STRING are supported. We recommend that you use BYTE[] to prevent coding issues. */
}
public static void main(String[] args) {
/* The main function is optional and its class functions can be verified on an on-premises standalone machine. */
}
}
If an exception occurs, the framework captures the exception and returns the message in the exception as an error message to the client. In addition, the HTTP status code 400 is returned. You can also capture exceptions and return corresponding error messages, as shown in the following example: try{
} catch (com.alibaba.fastjson.JSONException e) {
throw new RuntimeException("bad json format, " + e.getMessage());
}
Standalone development and debugging
The standalone debugging feature is designed for non-cluster scenarios. This feature allows you to develop and debug a model or processor in an on-premises environment. The development and call interfaces are fully compatible with the online cluster environment. This feature saves you from frequently deploying new services in the development and testing phases and reduces the resource costs for debugging. Perform the following steps to perform standalone debugging:
Note This feature relies on Docker. Therefore, you must pre-install Docker on the server where the EASCMD client is running. If the graphics processing unit (GPU) and CUDA are required, you must pre-install the CUDA and Nvidia-Docker on the on-premises server.
- Install Docker. For more information, see Install Docker.
- Download the EASCMD client from the following links. Different versions are provided.
- Create a service configuration file. Specify the model to be deployed and the processor to be compiled in the configuration file, as shown in the following example:
For information about the parameters, see Run commands to use the EASCMD client.{ "name": "diy_test", "generate_token": "true", "model_path": "http://examplebucket.oss-cn-hangzhou-zmf.aliyuncs.com/scorecard.pmml", #You can specify an HTTP path or a local path (only for local debugging). "processor_path": "./diy_processor_release.tar.gz", #You can specify an HTTP path or a local path (only for local debugging). The package is decompressed to a JAR file compiled by the Java processor. "processor_mainclass": "com.alibaba.eas.TestProcessor", #The name of the main class that is used to implement Load() and Process() in the Java processor. "processor_type": "java", "metadata": { "resource": "eas-r-9lkbl2jvdm0puv****", #The resource parameter indicates that the model is deployed in a dedicated resource group. "instance": 1, "cpu": 1, "memory": 2000 } }
- Deploy and debug the model.
sudo eascmd test service.json